diff options
author | Tharre <tharre3@gmail.com> | 2018-03-27 07:02:36 +0200 |
---|---|---|
committer | Tharre <tharre3@gmail.com> | 2018-03-27 07:17:08 +0200 |
commit | b8a806dfc701118ce1f10d5332093ac1f710d808 (patch) | |
tree | 264129ebbf2db959ffbd4dfa97c6034222b1434c /arch-system/borgbackup | |
parent | 3cf436534fc2c3886184d7bbf441f45791bdc4a0 (diff) | |
download | pkgbuilds-b8a806dfc701118ce1f10d5332093ac1f710d808.tar.gz pkgbuilds-b8a806dfc701118ce1f10d5332093ac1f710d808.tar.xz pkgbuilds-b8a806dfc701118ce1f10d5332093ac1f710d808.zip |
arch-system: add backup-system package
This implements fully automated, hourly backups of my /home via
borgbackup, btrfs and systemd timers.
Note that `setup_borgbackup`, a related function that is part of my
dotfiles, is needed to setup backups for the first time, as well as a
call to `borg init` with the proper environment variables.
Diffstat (limited to 'arch-system/borgbackup')
-rwxr-xr-x | arch-system/borgbackup | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/arch-system/borgbackup b/arch-system/borgbackup new file mode 100755 index 0000000..5cc635d --- /dev/null +++ b/arch-system/borgbackup @@ -0,0 +1,65 @@ +#!/bin/bash + +set -o errexit -o errtrace + +export SNAPSHOT_TARGET="/.snapshots/home-$(date "+%Y-%m-%dT%H:%M:%S")" +export BORG_REPO="borg@borg1.th73.ovh:." +export BORG_PASSPHRASE="$(< ~/.borg_secret)" +export BORG_RSH="ssh -o IdentitiesOnly=yes -i ~/.ssh/borg_key" +export LC_COLLATE=C + +cleanup() { + cd / + sudo -n /usr/bin/backup-sudo cleanup +} + +handle_failure() { + notify-send -u critical "Backup failed!" + echo "Backup failed!" >&2 + cleanup + exit 2 +} + +trap cleanup EXIT +trap handle_failure INT TERM ERR + +notify-send "Backup started." + +cleanup + +backup() { + # borg's caching of 'known' files is really dumb - it takes the full + # canonical path to check if it has already seen a file. This means that the + # path of files must never change if you want fast backups. + # As symlinks won't work, we use mount(8) to ensure the paths stay the same. + sudo -n backup-sudo mount "$1" + + cd /backup + # TODO: break lock when necessary? + borg create --stats \ + --exclude-from ~/.borg_exclude \ + --show-rc \ + ::"$(basename "$1")" . + + cleanup + sudo -n backup-sudo delete_snapshot "$1" +} + +if [ ! -d "$SNAPSHOT_TARGET" ]; then + sudo -n backup-sudo snapshot "$SNAPSHOT_TARGET" +fi + +for d in /.snapshots/*/ ; do + echo "Now working on: $d" + backup "$d" +done + +echo "Pruning repository." + +borg prune --show-rc --list \ + --keep-hourly 24 \ + --keep-daily 7 \ + --keep-weekly 4 \ + --keep-monthly 6 + +echo "Backup finished." |