1
1
mirror of https://gitlab.archlinux.org/archlinux/infrastructure.git synced 2025-01-18 08:06:16 +01:00
infrastructure/roles/borg_client/templates/borg-backup.sh.j2
Evangelos Foutras 268080569a
borg_client: stop ignoring errors during db dumps
Due to the "systemctl is-active foo && backup-foo || true" shorthand,
errors during database dumping were being ignored. Change the MariaDB
section to also be wrapped in a proper if statement. Finally, get rid
of "|| true" silencing statements + enable errexit in helper scripts.
2024-03-30 18:25:34 +02:00

97 lines
2.5 KiB
Django/Jinja

#!/usr/bin/env bash
set -e
src="/"
snapshotdir="backup-snapshot"
backup_mountdir="/backup"
declare -a list_of_btrfs_submounts
##
# usage : is_btrfs( $path )
# return : whether $path is on a btrfs
# source: makechrootpkg from devtools
##
is_btrfs() {
[[ -e "$1" && "$(stat -f -c %T "$1")" == btrfs ]]
}
##
# usage : delete_snapshot( $path )
##
delete_snapshot() {
btrfs subvolume delete --commit-after "$1"
}
##
# exit cleanup handler
##
cleanup() {
if is_btrfs "$src"; then
umount -R "$backup_mountdir"
for vol in $list_of_btrfs_submounts; do
delete_snapshot "$vol/$snapshotdir"
done
rmdir "$backup_mountdir"
fi
}
trap cleanup EXIT
# Dump databases to /root/backup-{postgres,mysql} before taking a btrfs snapshot
if systemctl is-active postgresql; then
/usr/local/bin/backup-postgres.sh
fi
if systemctl is-active mariadb; then
/usr/local/bin/backup-mysql.sh
fi
if is_btrfs "$src"; then
# List all btrfs submounts we want to backup, e.g. homedir.archlinux.org with "/ /home"
list_of_btrfs_submounts=$(findmnt -Rl -o target,fstype,options / | grep btrfs | grep -v docker | grep -v "subvol=\/[[:alnum:]]" | cut -d ' ' -f1)
if [[ -d "$backup_mountdir" ]]; then
if [[ $(findmnt -M "$backup_mountdir") ]]; then
umount -R "$backup_mountdir"
fi
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
else
backup_mountdir="$src"
fi
{{ item['borg_cmd'] }} create -v --stats -C zstd \
-e /proc \
-e /sys \
-e /dev \
-e /run \
-e /tmp \
-e /var/cache \
-e /var/lib/archbuild \
-e /var/lib/archbuilddest \
-e /var/lib/docker \
-e "$backup_mountdir/proc" \
-e "$backup_mountdir/sys" \
-e "$backup_mountdir/dev" \
-e "$backup_mountdir/run" \
-e "$backup_mountdir/tmp" \
-e "$backup_mountdir/var/cache" \
-e "$backup_mountdir/var/lib/archbuild" \
-e "$backup_mountdir/var/lib/archbuilddest" \
-e "$backup_mountdir/var/lib/docker" \
{{ item['host'] }}/{{ item['dir'] }}::$(date "+%Y%m%d-%H%M%S") "$backup_mountdir"
{{ item['borg_cmd'] }} prune -v {{ item['host'] }}/{{ item['dir'] }} --keep-daily=7 --keep-weekly=4 --keep-monthly=6
{{ item['borg_cmd'] }} compact -v {{ item['host'] }}/{{ item['dir'] }}