Archived
1
0
Fork 0
This repository has been archived on 2023-08-09. You can view files and clone it, but cannot push or open issues or pull requests.
dotfiles/borg/backup.sh

95 lines
5.3 KiB
Bash
Raw Normal View History

2022-05-10 01:25:47 +02:00
#!/bin/bash
# Desktop notification
function notify() {
if [ -x "$(command -v notify-send)" ]; then
# https://stackoverflow.com/questions/28195805/running-notify-send-as-root/49533938#49533938
local display=":$(ls /tmp/.X11-unix/* | sed 's#/tmp/.X11-unix/X##' | head -n 1)"
local uid=$(id -u caspervk)
sudo -u caspervk DISPLAY=$display DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$uid/bus notify-send --expire-time=0 --icon=backup "$@"
fi
}
notify "Borgbackup: Started"
# Avoid UnicodeError
export LANG=en_US.UTF-8
# Ask an external program to supply the repository passphrase:
export BORG_PASSCOMMAND='cat /usr/local/etc/borg/passphrase.key'
# When running Borg using an automated script, ssh might still ask for a password, even if there is an SSH key for
# the target server. Use this to make scripts more robust:
export BORG_RSH='ssh -oBatchMode=yes'
# Set repository location
# Note: because of the way the server is set up, the repo resides in `/home/borg/repos/<hostname>/` on the server.
export BORG_REPO=ssh://borg@sigma.caspervk.net:222/./auto-full
# Initialize remote repository (doesn't matter if it already is).
borg init --encryption=repokey-blake2
# Backup directories into an archive named after the machine and current utc time.
# Patterns:
# A backup root (starting point) path starts with the prefix R, followed by a path.
# An include rule starts with the prefix +.
# An exclude rule starts with the prefix -.
# An exclude-norecurse rule starts with !.
borg create \
--filter AME \
--show-rc \
--stats \
--compression zstd \
--remote-ratelimit 0 \
--exclude-caches \
\
--pattern '! /dev' \
--pattern '! /proc' \
--pattern '! /sys' \
--pattern '! /var/run' \
--pattern '! /var/tmp' \
--pattern '! /run' \
--pattern '! /tmp' \
--pattern '! /lost+found' \
--pattern '! /var/cache' \
--pattern '! /**/found.000/*' \
--pattern '! /mnt' \
\
--pattern '! /home/*/.steam' \
--pattern '! /home/*/GOG Games' \
--pattern '! /home/*/.cache' \
--pattern '! /home/*/Downloads' \
--pattern '! /home/*/.local/share/Trash' \
\
--pattern '+ /media/caspervk/C/Users/Casper/Desktop' \
--pattern '+ /media/caspervk/C/Program Files (x86)/World of Warcraft/_classic_' \
--pattern '- /media' \
\
--pattern '- /media/caspervk/Backup/Downloads' \
--pattern '- /media/caspervk/Backup/monero' \
--pattern '- /media/caspervk/Backup/Programmer/VirtualBox' \
--pattern '+ /media/caspervk/Backup' \
::'{hostname}-{utcnow}' \
/
backup_exit=$?
# Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 monthly archives:
# Note that as the repo is append-only, prune wont free disk space, but merely tag data as deleted in a new
# transaction. As soon as we write to the repo in non-append-only mode (e.g. prune, delete or create archives from an
# admin machine), it will remove the deleted objects permanently (including the ones that were already marked as
# deleted, but not removed, in append-only mode).
borg prune \
--list \
--show-rc \
--keep-daily 7 \
--keep-weekly 4 \
--keep-monthly 6
prune_exit=$?
notify "Borgbackup: Finished"
# use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
exit ${global_exit}