dotfiles/bin/borg.sh

96 lines
3.2 KiB
Bash
Raw Normal View History

2020-08-10 10:13:31 +00:00
#!/bin/bash
2020-10-14 08:44:01 +00:00
set -aeou pipefail
2020-08-10 10:13:31 +00:00
2020-10-14 08:43:37 +00:00
[[ ! -f "$HOME"/.borgbackup.env ]] && printf "\$HOME/.borgbackup.env doesn't exist!\n" && exit 1
2020-08-10 10:13:31 +00:00
2020-10-14 08:43:37 +00:00
if [[ "$(stat -c %A "$HOME"/.borgbackup.env)" != "-rw-------" ]]; then
2020-08-10 10:13:31 +00:00
printf "WARNING!!!! \$HOME/.borgbackup.env has the wrong permissions!\n"
printf "\nrun : chmod 0600 \$HOME/.borgbackup.env\n"
exit 1
fi
2020-10-14 08:48:58 +00:00
# shellcheck disable=SC1090
2020-10-13 07:13:20 +00:00
source "${HOME}/.borgbackup.env"
2020-08-10 10:13:31 +00:00
2020-08-10 12:24:19 +00:00
# some helpers and error handling:
info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
2020-08-10 10:13:31 +00:00
info "Starting backup"
# Backup the most important directories into an archive named after
# the machine this script is currently running on:
borg create \
--verbose \
--filter AME \
--list \
--stats \
--show-rc \
--compression lz4 \
--exclude-caches \
--exclude '/home/iso/*' \
--exclude '/home/VMs/*' \
2020-08-10 10:13:31 +00:00
--exclude '/var/cache/*' \
--exclude '/var/tmp/*' \
--exclude '/*/lost\+found/*' \
--exclude '/home/*/.cache/*' \
--exclude '/home/*/go/*' \
--exclude '/home/*/.wine/*' \
--exclude '/home/*/snap/*' \
--exclude '/home/*/Downloads/*' \
--exclude '/home/*/Nextcloud/*' \
--exclude '/home/*/.config/Rocket.Chat/Cache//*' \
--exclude '/home/*/.config/discord/Cache/*' \
--exclude '/home/*/.mozilla/firefox/*/Cache/*' \
--exclude '/home/*/.mozilla/firefox/*/minidumps/*' \
--exclude '/home/*/.mozilla/firefox/*/extensions.sqlite' \
--exclude '/home/*/.mozilla/firefox/*/urlclassifier3.sqlite' \
--exclude '/home/*/.config/google-chrome/Default/Local Storage/*' \
--exclude '/home/*/.config/google-chrome/Default/Session Storage/*' \
--exclude '/home/*/.config/google-chrome/Default/Application Cache/*' \
--exclude '/home/*/.config/google-chrome/Default/History/*' \
--exclude '/home/*/.config/google-chrome/Default/History-journal/*' \
--exclude '/home/*/.config/google-chrome/Default/History Provider Cache/*' \
--exclude '/home/*/.local/share/flatpak/*' \
--exclude '/home/*/.local/share/virtualenv/*' \
--exclude '/home/*/.local/share/virtualenvs/*' \
2020-08-10 12:24:47 +00:00
--exclude '/home/*/code/*' \
2021-02-05 07:16:33 +00:00
--exclude '/home/*/VirtualBox VMs/*' \
2020-08-10 10:13:31 +00:00
::'{hostname}-{now}' \
/home \
backup_exit=$?
info "Pruning repository"
# Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 monthly
# archives of THIS machine. The '{hostname}-' prefix is very important to
# limit prune's operation to this machine's archives and not apply to
# other machines' archives also:
borg prune \
--list \
--prefix '{hostname}-' \
--show-rc \
--keep-daily 14 \
--keep-weekly 8 \
--keep-monthly 6 \
prune_exit=$?
2020-10-13 07:13:20 +00:00
# use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
if [ ${global_exit} -eq 0 ]; then
info "Backup and Prune finished successfully"
elif [ ${global_exit} -eq 1 ]; then
info "Backup and/or Prune finished with warnings"
else
info "Backup and/or Prune finished with errors"
fi
exit ${global_exit}