2023-10-07 11:06:10 +02:00
|
|
|
#!/bin/bash
|
|
|
|
# Create archived backups of zfs datasets, with support for incremental backups from automated snapshots.
|
2023-10-07 11:20:27 +02:00
|
|
|
# Usage: backup-zfs-dataset [OPTIONS] <dataset> <destination>
|
2023-10-07 11:06:10 +02:00
|
|
|
|
|
|
|
usage() {
|
2023-10-07 11:20:27 +02:00
|
|
|
>&2 printf "Usage: %s [OPTIONS] [<dataset> <destination>]\n" "$0"
|
2023-10-07 11:06:10 +02:00
|
|
|
>&2 printf "Options:\n"
|
|
|
|
>&2 printf "\t-c --compression-level <level> \t Specify compression level (integer)\n"
|
|
|
|
>&2 printf "\t-s --dataset <dataset name> \t Specify dataset name\n"
|
|
|
|
>&2 printf "\t-d --destination <path to directory> \t Specify destination\n"
|
|
|
|
>&2 printf "\t-m --max-size <size> \t Specify maximum size of archive parts\n"
|
2023-10-07 11:20:27 +02:00
|
|
|
>&2 printf "\t-f --force \t Force overwriting existing backups if the backup already exists\n"
|
2023-10-07 11:06:10 +02:00
|
|
|
exit "${1:-1}"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get options.
|
|
|
|
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
|
|
case "${1}" in
|
2023-10-07 11:20:27 +02:00
|
|
|
-c | --compression_level)
|
2023-10-10 22:34:14 +02:00
|
|
|
if ! [[ "${2}" =~ [[:digit:]] ]]; then
|
2023-10-07 11:20:27 +02:00
|
|
|
>&2 printf "Error: Invalid compression level: '%s'\n" "${2}"
|
2023-10-07 11:06:10 +02:00
|
|
|
usage
|
|
|
|
fi
|
|
|
|
compression_level="${2}"
|
|
|
|
shift 2
|
|
|
|
;;
|
2023-10-07 11:20:27 +02:00
|
|
|
-s | --dataset)
|
2023-10-07 11:06:10 +02:00
|
|
|
if ! [ -n "${2}" ]; then
|
2023-10-07 11:20:27 +02:00
|
|
|
>&2 printf "Error: Invalid dataset: '%s'\n" "${2}"
|
2023-10-07 11:06:10 +02:00
|
|
|
usage
|
|
|
|
fi
|
|
|
|
dataset="${2}"
|
|
|
|
shift 2
|
|
|
|
;;
|
2023-10-07 11:20:27 +02:00
|
|
|
-d | --destination)
|
2023-10-07 11:06:10 +02:00
|
|
|
if ! [ -d "${2}" ]; then
|
2023-10-07 11:20:27 +02:00
|
|
|
>&2 printf "Error: Specified destination does not exist: '%s'\n" "${2}"
|
2023-10-07 11:06:10 +02:00
|
|
|
usage
|
|
|
|
fi
|
|
|
|
destination="${2}"
|
|
|
|
shift 2
|
|
|
|
;;
|
2023-10-07 11:20:27 +02:00
|
|
|
-m | --max-size)
|
2023-10-07 11:06:10 +02:00
|
|
|
if ! [[ "${2}" =~ [[:digit:]](K|M|G) ]]; then
|
2023-10-07 11:20:27 +02:00
|
|
|
>&2 printf "Error: Invalid maximum size: '%s'\n" "${2}"
|
2023-10-07 11:06:10 +02:00
|
|
|
usage
|
|
|
|
fi
|
|
|
|
max_size="${2}"
|
|
|
|
shift 2
|
|
|
|
;;
|
2023-10-07 11:20:27 +02:00
|
|
|
-f | --force)
|
|
|
|
force_create_manual_backup=1
|
|
|
|
shift 1
|
|
|
|
;;
|
2023-10-07 11:06:10 +02:00
|
|
|
*)
|
2023-10-07 11:20:27 +02:00
|
|
|
>&2 printf "Error: Invalid option: '%s'\n" "${1}"
|
2023-10-07 11:06:10 +02:00
|
|
|
usage
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
# Check arguments.
|
|
|
|
|
2023-10-07 11:20:27 +02:00
|
|
|
if [[ -z "${dataset:=${1}}" || -z "${destination:=${2}}" ]]; then
|
|
|
|
>&2 printf "Error: You need to specify a dataset and a destination.\n"
|
|
|
|
usage
|
|
|
|
elif [ -z "${dataset}" ]; then
|
|
|
|
>&2 printf "Error: Invalid dataset: '%s'\n" "${1}"
|
|
|
|
usage
|
|
|
|
elif ! [ -d "${destination}" ]; then
|
|
|
|
>&2 printf "Error: Specified destination does not exist: '%s'\n" "${2}"
|
2023-10-07 11:06:10 +02:00
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
2023-10-10 22:34:14 +02:00
|
|
|
# Set defaults
|
|
|
|
compression_level="${compression_level:=1}"
|
|
|
|
max_size="${max_size:=2G}"
|
|
|
|
|
2023-10-07 11:06:10 +02:00
|
|
|
# Working snapshots
|
|
|
|
|
|
|
|
# Find snapshots
|
|
|
|
snapshot_location="/mnt/${dataset}/.zfs/snapshot"
|
2023-10-10 22:34:14 +02:00
|
|
|
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 )"
|
2023-10-07 11:06:10 +02:00
|
|
|
|
|
|
|
# Check snapshots existance
|
|
|
|
if ! [ -n "${latest_manual}" ]; then
|
|
|
|
>&2 printf "Error: No manual snapshot could be found!\n"
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
if ! [ -n "${latest_auto}" ]; then
|
|
|
|
>&2 printf "Error: No automatic snapshot could be found!\n"
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
2023-10-10 22:34:14 +02:00
|
|
|
printf "Latest manual snapshot: %s\nLatest auto snapshot: %s\n" "${latest_manual}" "${latest_auto}"
|
2023-10-07 11:06:10 +02:00
|
|
|
|
|
|
|
# Abort entire script if anything fails.
|
|
|
|
set -e
|
|
|
|
|
|
|
|
# Backups
|
|
|
|
|
|
|
|
# Base backup.
|
|
|
|
output_filename="${destination}/${latest_manual}.gz"
|
2023-10-10 22:34:14 +02:00
|
|
|
existing_backup="$( find "${destination}" -type f -name "${latest_manual}.gz.part.[a-z][a-z]" -print -quit )"
|
|
|
|
if [ -z ${existing_backup} ]; then
|
2023-10-07 11:06:10 +02:00
|
|
|
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}" \
|
2023-10-10 22:34:14 +02:00
|
|
|
| gzip "-${compression_level}" --verbose --rsyncable \
|
2023-10-07 11:06:10 +02:00
|
|
|
| split - --verbose -b "${max_size}" "${output_filename}.part."
|
|
|
|
printf "Written manual backup to: %s\n" "${output_filename}"
|
|
|
|
elif [ "${force_create_manual_backup}" ]; then
|
|
|
|
printf "Removing previous backup files.\n"
|
2023-10-10 22:34:14 +02:00
|
|
|
find "${destination}" -type f -name "${latest_manual}.gz.part.[a-z][a-z]" -print -delete
|
2023-10-07 11:06:10 +02:00
|
|
|
printf "Backing up manual snapshot.\n"
|
|
|
|
sudo zfs send --verbose "${dataset}@${latest_manual}" \
|
2023-10-10 22:34:14 +02:00
|
|
|
| gzip "-${compression_level}" --verbose --rsyncable \
|
2023-10-07 11:06:10 +02:00
|
|
|
| split - --verbose -b "${max_size}" "${output_filename}.part."
|
|
|
|
printf "Written manual backup to: %s\n" "${output_filename}"
|
|
|
|
else
|
|
|
|
printf "Found existing backup of manual snapshot: %s\n" "${existing_backup}"
|
|
|
|
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"
|
2023-10-10 22:34:14 +02:00
|
|
|
sudo zfs send --verbose -i "@${latest_manual}" "${dataset}@${latest_auto}" \
|
|
|
|
| gzip "-${compression_level}" --verbose \
|
|
|
|
| split - --verbose -b "${max_size}" "${output_filename}.part."
|
2023-10-07 11:06:10 +02:00
|
|
|
printf "Written incremental backup to: %s\n" "${output_filename}"
|
|
|
|
|
|
|
|
# TODO Cleanup
|
|
|
|
|
|
|
|
printf "Done!\n"
|