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
2020-07-09 00:50:28 +02:00

87 lines
2.3 KiB
Django/Jinja

#!/usr/bin/env bash
set -e
src="/"
snapshotdir="backup-snapshot"
backup_mountdir="/backup"
##
# 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"
}
if is_btrfs "$src"; then
list_of_btrfs_submounts=$(findmnt -Rl -o target,fstype,options / | grep btrfs | 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
systemctl is-active postgresql && /usr/local/bin/backup-postgres.sh || true
if systemctl is-active mysqld || systemctl is-active mariadb; then
/usr/local/bin/backup-mysql.sh || true
fi
{% if inventory_hostname == "gitlab.archlinux.org" %}
systemctl is-active docker && /usr/local/bin/backup-gitlab.sh
{% endif %}
borg 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"
borg prune -v {{ item['host'] }}/{{ item['dir'] }} --keep-daily=7 --keep-weekly=4 --keep-monthly=6
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
/usr/bin/vendor_perl/borg-restore.pl --update-cache