151 lines
4.4 KiB
Bash
Executable File
151 lines
4.4 KiB
Bash
Executable File
#!/bin/bash
|
|
# James W. Barnett
|
|
|
|
# Takes snapshots of each snapper configuration. It then sends the snapshot to
|
|
# a location on an external drive. After the initial transfer, it does
|
|
# incremental snapshots on later calls. It's important not to delete the
|
|
# snapshot created on your system since that will be used to determine the
|
|
# difference for the next incremental snapshot.
|
|
|
|
set -e
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
key="$1"
|
|
case $key in
|
|
-d|--description)
|
|
description="$2"
|
|
shift
|
|
;;
|
|
#TODO -u|--UUID)
|
|
# uuid_cmdline="$2"
|
|
# shift
|
|
# ;;
|
|
# TODO: add more
|
|
-h|--help)
|
|
echo "Usage:"
|
|
echo " snap-sync -d 'Snapshot description'"
|
|
shift
|
|
exit 1
|
|
;;
|
|
*)
|
|
echo "Error: unknown option."
|
|
exit 1
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
description=${description:-"latest incremental backup"}
|
|
|
|
if [[ $EUID -ne 0 ]]; then
|
|
printf "Script must be run as root.\n"
|
|
exit
|
|
fi
|
|
|
|
# It's important not to change this userdata in the snapshots, since that's how
|
|
# we find the previous one.
|
|
|
|
TARGETS="$(findmnt -n -v -t btrfs -o TARGET --list)"
|
|
UUIDS="$(findmnt -n -v -t btrfs -o UUID --list)"
|
|
|
|
declare -a TARGETS_ARRAY
|
|
declare -a UUIDS_ARRAY
|
|
|
|
i=0
|
|
for x in $TARGETS; do
|
|
TARGETS_ARRAY[$i]=$x
|
|
i=$((i+1))
|
|
done
|
|
i=0
|
|
for x in $UUIDS; do
|
|
UUIDS_ARRAY[$i]=$x
|
|
i=$((i+1))
|
|
done
|
|
printf "Select a mounted BTRFS device to backup to.\n"
|
|
disk=-1
|
|
while [[ $disk -lt 0 || $disk -gt $i ]]; do
|
|
for x in "${!TARGETS_ARRAY[@]}"; do
|
|
printf "%s) %s (%s)\n" "$((x+1))" "${UUIDS_ARRAY[$x]}" "${TARGETS_ARRAY[$x]}"
|
|
done
|
|
printf "0) Exit\n"
|
|
read -r -p "Enter a number: " disk
|
|
done
|
|
if [[ $disk == 0 ]]; then
|
|
exit 0
|
|
fi
|
|
selected_uuid="${UUIDS_ARRAY[$((disk-1))]}"
|
|
selected_mnt="${TARGETS_ARRAY[$((disk-1))]}"
|
|
printf "You selected the disk with UUID %s.\n" "$selected_uuid"
|
|
|
|
if [[ -f /etc/conf.d/snapper ]]; then
|
|
source /etc/conf.d/snapper
|
|
else
|
|
printf "ERROR: /etc/conf.d/snapper does not exist!\n"
|
|
exit 1
|
|
fi
|
|
|
|
for x in $SNAPPER_CONFIGS; do
|
|
|
|
source /etc/snapper/configs/$x
|
|
|
|
printf "At '%s' configuration\n" "$x"
|
|
|
|
old_number=$(snapper -c "$x" list -t single | awk '/'"$selected_uuid"'/ {print $1}')
|
|
old_snapshot=$SUBVOLUME/.snapshots/$old_number/snapshot
|
|
|
|
if [[ -z "$old_number" ]]; then
|
|
printf "No backups have been performed for '%s' on this disk.\n" "$x"
|
|
read -r -p "Enter name of directory to store backups, relative to $selected_mnt (to be created if not existing): " mybackupdir
|
|
printf "This will be the initial backup for snapper configuration '%s' to this disk. This could take awhile.\n" "$x"
|
|
BACKUPDIR="$selected_mnt/$mybackupdir"
|
|
mkdir -p "$BACKUPDIR"
|
|
else
|
|
mybackupdir=$(snapper -c root list -t single | awk -F"|" '/'"$selected_uuid"'/ {print $5}' | awk -F "," '{print $1}' | awk -F"=" '{print $2}')
|
|
BACKUPDIR="$selected_mnt/$mybackupdir"
|
|
if [[ ! -d $BACKUPDIR ]]; then
|
|
printf "ERROR: %s is not a directory on %s.\n" "$BACKUPDIR" "$selected_uuid"
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
new_number=$(snapper -c "$x" create --print-number)
|
|
new_snapshot=$SUBVOLUME/.snapshots/$new_number/snapshot
|
|
new_info=$SUBVOLUME/.snapshots/$new_number/info.xml
|
|
sync
|
|
backup_location=$BACKUPDIR/$x/$new_number/
|
|
printf "Backup location: %s\n" "$backup_location"
|
|
|
|
read -r -p "Continue [Y/n]? " cont_backup
|
|
printf "\n"
|
|
if [[ "$cont_backup" != [Yy]"es" && "$cont_backup" != [Yy] ]]; then
|
|
printf "Skipping this configuration.\n"
|
|
continue
|
|
fi
|
|
|
|
mkdir -p "$backup_location"
|
|
|
|
|
|
if [[ -z "$old_number" ]]; then
|
|
|
|
btrfs send "$new_snapshot" | btrfs receive "$backup_location" &>/dev/null
|
|
|
|
else
|
|
|
|
# Sends the difference between the new snapshot and old snapshot to the
|
|
# backup location. Using the -c flag instead of -p tells it that there
|
|
# is an identical subvolume to the old snapshot at the receiving
|
|
# location where it can get its data. This helps speed up the transfer.
|
|
btrfs send "$new_snapshot" -c "$old_snapshot" | btrfs receive "$backup_location" &>/dev/null
|
|
cp "$new_info" "$backup_location"
|
|
snapper -c "$x" delete "$old_number"
|
|
fi
|
|
|
|
userdata="backupdir=$mybackupdir, uuid=$selected_uuid"
|
|
|
|
# Tag new snapshot as the latest
|
|
snapper -v -c "$x" modify -d "$description" -u "$userdata" "$new_number"
|
|
|
|
done
|
|
|
|
printf "Done!\n"
|