Fix backup issues
This commit is contained in:
parent
243b7dacc8
commit
6b1078057a
2 changed files with 21 additions and 17 deletions
|
@ -18,7 +18,7 @@ usage() {
|
||||||
while [[ $# -gt 0 ]]; do
|
while [[ $# -gt 0 ]]; do
|
||||||
case "${1}" in
|
case "${1}" in
|
||||||
-c | --compression_level)
|
-c | --compression_level)
|
||||||
if ! [[ "${2}" =~ -[[:digit:]] ]]; then
|
if ! [[ "${2}" =~ [[:digit:]] ]]; then
|
||||||
>&2 printf "Error: Invalid compression level: '%s'\n" "${2}"
|
>&2 printf "Error: Invalid compression level: '%s'\n" "${2}"
|
||||||
usage
|
usage
|
||||||
fi
|
fi
|
||||||
|
@ -73,12 +73,16 @@ elif ! [ -d "${destination}" ]; then
|
||||||
usage
|
usage
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Set defaults
|
||||||
|
compression_level="${compression_level:=1}"
|
||||||
|
max_size="${max_size:=2G}"
|
||||||
|
|
||||||
# Working snapshots
|
# Working snapshots
|
||||||
|
|
||||||
# Find snapshots
|
# Find snapshots
|
||||||
snapshot_location="/mnt/${dataset}/.zfs/snapshot"
|
snapshot_location="/mnt/${dataset}/.zfs/snapshot"
|
||||||
latest_auto="$( find "${snapshot_location}"/* -maxdepth 0 -name 'auto*' -type d | sort -n | tail -n1 | xargs -n1 basename >2 /dev/null )"
|
latest_auto="$( find "${snapshot_location}"/* -maxdepth 0 -name 'auto*' -type d | sort -n | tail -n1 | xargs -n1 basename )"
|
||||||
latest_manual="$( find "${snapshot_location}"/* -maxdepth 0 -name 'manual*' -type d | sort -n | tail -n1 | xargs -n1 basename >2 /dev/null )"
|
latest_manual="$( find "${snapshot_location}"/* -maxdepth 0 -name 'manual*' -type d | sort -n | tail -n1 | xargs -n1 basename )"
|
||||||
|
|
||||||
# Check snapshots existance
|
# Check snapshots existance
|
||||||
if ! [ -n "${latest_manual}" ]; then
|
if ! [ -n "${latest_manual}" ]; then
|
||||||
|
@ -90,7 +94,7 @@ if ! [ -n "${latest_auto}" ]; then
|
||||||
exit 2
|
exit 2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
printf "Latest auto snapshot: %s\nLatest manual snapshot: %s\n" "${latest_auto}" "${latest_manual}"
|
printf "Latest manual snapshot: %s\nLatest auto snapshot: %s\n" "${latest_manual}" "${latest_auto}"
|
||||||
|
|
||||||
# Abort entire script if anything fails.
|
# Abort entire script if anything fails.
|
||||||
set -e
|
set -e
|
||||||
|
@ -99,21 +103,20 @@ set -e
|
||||||
|
|
||||||
# Base backup.
|
# Base backup.
|
||||||
output_filename="${destination}/${latest_manual}.gz"
|
output_filename="${destination}/${latest_manual}.gz"
|
||||||
existing_backup="$( find "${destination}" -type f -name "${output_filename}.part.[a-z][a-z]" -print )" # -quit if you don't need to know every file.
|
existing_backup="$( find "${destination}" -type f -name "${latest_manual}.gz.part.[a-z][a-z]" -print -quit )"
|
||||||
if ! [ ${existing_backup} ]; then
|
if [ -z ${existing_backup} ]; then
|
||||||
printf "Info: If you've manually created a new snapshot, you might want to remove the old backups.\n"
|
printf "Info: If you've manually created a new snapshot, you might want to remove the old backups.\n"
|
||||||
printf "Latest manual snapshot was not yet backed up, backing up now.\n"
|
printf "Latest manual snapshot was not yet backed up, backing up now.\n"
|
||||||
sudo zfs send --verbose "${dataset}@${latest_manual}" \
|
sudo zfs send --verbose "${dataset}@${latest_manual}" \
|
||||||
| gzip "${compression_level:='-1'}" --verbose --rsyncable \
|
| gzip "-${compression_level}" --verbose --rsyncable \
|
||||||
| split - --verbose -b "${max_size}" "${output_filename}.part."
|
| split - --verbose -b "${max_size}" "${output_filename}.part."
|
||||||
printf "Written manual backup to: %s\n" "${output_filename}"
|
printf "Written manual backup to: %s\n" "${output_filename}"
|
||||||
elif [ "${force_create_manual_backup}" ]; then
|
elif [ "${force_create_manual_backup}" ]; then
|
||||||
# TODO What if the new backup is smaller than the previous?
|
|
||||||
printf "Removing previous backup files.\n"
|
printf "Removing previous backup files.\n"
|
||||||
rm "${existing_backup}"
|
find "${destination}" -type f -name "${latest_manual}.gz.part.[a-z][a-z]" -print -delete
|
||||||
printf "Backing up manual snapshot.\n"
|
printf "Backing up manual snapshot.\n"
|
||||||
sudo zfs send --verbose "${dataset}@${latest_manual}" \
|
sudo zfs send --verbose "${dataset}@${latest_manual}" \
|
||||||
| gzip "${compression_level:='-1'}" --verbose --rsyncable \
|
| gzip "-${compression_level}" --verbose --rsyncable \
|
||||||
| split - --verbose -b "${max_size}" "${output_filename}.part."
|
| split - --verbose -b "${max_size}" "${output_filename}.part."
|
||||||
printf "Written manual backup to: %s\n" "${output_filename}"
|
printf "Written manual backup to: %s\n" "${output_filename}"
|
||||||
else
|
else
|
||||||
|
@ -123,9 +126,9 @@ fi
|
||||||
# Incremental incremental backup.
|
# Incremental incremental backup.
|
||||||
printf "Creating incremental backup between %s and %s\n" "${latest_manual}" "${latest_auto}"
|
printf "Creating incremental backup between %s and %s\n" "${latest_manual}" "${latest_auto}"
|
||||||
output_filename="${destination}/${latest_manual}-${latest_auto}.gz"
|
output_filename="${destination}/${latest_manual}-${latest_auto}.gz"
|
||||||
sudo zfs send -i "@${latest_manual}" "${dataset}@${latest_auto}" \
|
sudo zfs send --verbose -i "@${latest_manual}" "${dataset}@${latest_auto}" \
|
||||||
| gzip "${compression_level}" --verbose \
|
| gzip "-${compression_level}" --verbose \
|
||||||
| split - --verbose -b "${max_size:='2G'}" "${output_filename}.part."
|
| split - --verbose -b "${max_size}" "${output_filename}.part."
|
||||||
printf "Written incremental backup to: %s\n" "${output_filename}"
|
printf "Written incremental backup to: %s\n" "${output_filename}"
|
||||||
|
|
||||||
# TODO Cleanup
|
# TODO Cleanup
|
||||||
|
|
|
@ -25,6 +25,7 @@ while getopts ":e:" option; do
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
shift $(( OPTIND - 1 ))
|
||||||
|
|
||||||
# Check arguments.
|
# Check arguments.
|
||||||
|
|
||||||
|
@ -59,11 +60,11 @@ docker exec "${base_container}" php occ maintenance:mode --on
|
||||||
# Database backup
|
# Database backup
|
||||||
echo 'Backing up database'
|
echo 'Backing up database'
|
||||||
host_database_backupfile="${destination}/${database_backupfile}"
|
host_database_backupfile="${destination}/${database_backupfile}"
|
||||||
docker exec --env-file "${env_file:='./nextcloud.env'}" "${database_container}" pg_dump 'nextcloud' -cwv -h 'localhost' -U 'nextcloud' > "${host_database_backupfile}"
|
docker exec --env-file "${env_file:=.env}" "${database_container}" pg_dump 'nextcloud' -cwv -h 'localhost' -U 'nextcloud' > "${host_database_backupfile}"
|
||||||
|
|
||||||
# Files backup
|
# Files backup
|
||||||
for file in 'config' 'themes'; do
|
for file in 'config' 'themes'; do
|
||||||
printf 'Copying %s\n' "${file}"
|
printf "Copying %s\n" "${file}"
|
||||||
docker cp -a "${base_container}":"/var/www/html/${file}" "${destination}"
|
docker cp -a "${base_container}":"/var/www/html/${file}" "${destination}"
|
||||||
done
|
done
|
||||||
|
|
||||||
|
@ -72,7 +73,7 @@ docker exec "${base_container}" php occ maintenance:mode --off
|
||||||
|
|
||||||
# Backup cleanup
|
# Backup cleanup
|
||||||
# Only keep 30 days of backups
|
# Only keep 30 days of backups
|
||||||
printf 'Clean up old database backups'
|
printf "Clean up old database backups.\n"
|
||||||
find "${destination}" -name '*sqlbkp*' -type f -mtime +30 -print -delete
|
find "${destination}" -name '*sqlbkp*' -type f -mtime +30 -print -delete
|
||||||
|
|
||||||
printf 'Done'
|
printf "Done\n"
|
||||||
|
|
Loading…
Reference in a new issue