Fix backup issues

This commit is contained in:
Tibo De Peuter 2023-10-10 22:34:14 +02:00
parent 243b7dacc8
commit 6b1078057a
2 changed files with 21 additions and 17 deletions

View file

@ -18,7 +18,7 @@ usage() {
while [[ $# -gt 0 ]]; do
case "${1}" in
-c | --compression_level)
if ! [[ "${2}" =~ -[[:digit:]] ]]; then
if ! [[ "${2}" =~ [[:digit:]] ]]; then
>&2 printf "Error: Invalid compression level: '%s'\n" "${2}"
usage
fi
@ -73,12 +73,16 @@ elif ! [ -d "${destination}" ]; then
usage
fi
# Set defaults
compression_level="${compression_level:=1}"
max_size="${max_size:=2G}"
# Working snapshots
# Find snapshots
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_manual="$( find "${snapshot_location}"/* -maxdepth 0 -name 'manual*' -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 )"
# Check snapshots existance
if ! [ -n "${latest_manual}" ]; then
@ -90,7 +94,7 @@ if ! [ -n "${latest_auto}" ]; then
exit 2
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.
set -e
@ -99,21 +103,20 @@ set -e
# Base backup.
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.
if ! [ ${existing_backup} ]; then
existing_backup="$( find "${destination}" -type f -name "${latest_manual}.gz.part.[a-z][a-z]" -print -quit )"
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 "Latest manual snapshot was not yet backed up, backing up now.\n"
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."
printf "Written manual backup to: %s\n" "${output_filename}"
elif [ "${force_create_manual_backup}" ]; then
# TODO What if the new backup is smaller than the previous?
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"
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."
printf "Written manual backup to: %s\n" "${output_filename}"
else
@ -123,9 +126,9 @@ fi
# Incremental incremental backup.
printf "Creating incremental backup between %s and %s\n" "${latest_manual}" "${latest_auto}"
output_filename="${destination}/${latest_manual}-${latest_auto}.gz"
sudo zfs send -i "@${latest_manual}" "${dataset}@${latest_auto}" \
| gzip "${compression_level}" --verbose \
| split - --verbose -b "${max_size:='2G'}" "${output_filename}.part."
sudo zfs send --verbose -i "@${latest_manual}" "${dataset}@${latest_auto}" \
| gzip "-${compression_level}" --verbose \
| split - --verbose -b "${max_size}" "${output_filename}.part."
printf "Written incremental backup to: %s\n" "${output_filename}"
# TODO Cleanup

View file

@ -25,6 +25,7 @@ while getopts ":e:" option; do
;;
esac
done
shift $(( OPTIND - 1 ))
# Check arguments.
@ -59,11 +60,11 @@ docker exec "${base_container}" php occ maintenance:mode --on
# Database backup
echo 'Backing up database'
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
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}"
done
@ -72,7 +73,7 @@ docker exec "${base_container}" php occ maintenance:mode --off
# Backup cleanup
# 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
printf 'Done'
printf "Done\n"