Verified Commit 053341eb authored by Sven-Hendrik Haase's avatar Sven-Hendrik Haase
Browse files

Fix backup script to be able to handle nested btrfs mounts

Earlier it would only snapshot the top dir which resulted in some awkward moments when another btrfs drive that was mounted to /home was empty in the backups because snapshots only snapshot the topmost device in the tree.
parent 6f0dc601
......@@ -3,7 +3,8 @@
set -e
src="/"
snapshotdir="/backup-snap"
snapshotdir="backup-snapshot"
backup_mountdir="/backup"
##
# usage : is_btrfs( $path )
......@@ -11,19 +12,32 @@ snapshotdir="/backup-snap"
# source: makechrootpkg from devtools
##
is_btrfs() {
[[ -e "$1" && "$(stat -f -c %T "$1")" == btrfs ]]
[[ -e "$1" && "$(stat -f -c %T "$1")" == btrfs ]]
}
##
# usage : delete_snapshot( $path )
##
delete_snapshot() {
btrfs subvolume delete --commit-after "$snapshotdir"
btrfs subvolume delete --commit-after "$1"
}
if is_btrfs "$src"; then
if [[ -d "$snapshotdir" ]]; then
delete_snapshot
fi
btrfs subvolume snapshot -r "$src" "$snapshotdir"
src="$snapshotdir"
list_of_btrfs_submounts=$(findmnt -Rl -o target,fstype,options / | grep btrfs | grep -v "subvol=\/[[:alnum:]]" | cut -d ' ' -f1)
if [[ -d "$backup_mountdir" ]]; then
umount -R "$backup_mountdir"
rmdir "$backup_mountdir"
fi
mkdir "$backup_mountdir"
for vol in $list_of_btrfs_submounts; do
if [[ -d "$vol/$snapshotdir" ]]; then
delete_snapshot "$vol/$snapshotdir"
fi
btrfs subvolume snapshot -r "$vol" "$vol/$snapshotdir"
mount -o bind "$vol/$snapshotdir" "$backup_mountdir/$vol"
done
fi
systemctl is-active postgresql && /usr/local/bin/backup-postgres.sh || true
......@@ -44,11 +58,13 @@ borg create -v --stats -C lz4 \
-e "$snapshotdir/var/lib/archbuild" \
-e "$snapshotdir/var/lib/archbuilddest" \
-e "$snapshotdir/srv/archive" \
{{ backup_host }}:{{ backup_dir }}::$(date "+%Y%m%d-%H%M%S") "$src"
{{ backup_host }}:{{ backup_dir }}::$(date "+%Y%m%d-%H%M%S") "$backup_mountdir"
borg prune -v {{ backup_host }}:{{ backup_dir }} --keep-daily=7 --keep-weekly=4 --keep-monthly=6
if [[ -d "$snapshotdir" ]]; then
delete_snapshot
fi
umount -R "$backup_mountdir"
for vol in $list_of_btrfs_submounts; do
delete_snapshot "$vol/$snapshotdir"
done
rmdir "$backup_mountdir"
/usr/bin/vendor_perl/borg-restore.pl --update-cache
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment