2022-05-10 01:25:47 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
2022-06-11 23:30:42 +02:00
|
|
|
set -e
|
2022-05-10 01:25:47 +02:00
|
|
|
|
2022-06-11 23:30:42 +02:00
|
|
|
# This script is based on the script at:
|
|
|
|
# https://borgbackup.readthedocs.io/en/stable/quickstart.html#automating-backups
|
2022-05-10 01:25:47 +02:00
|
|
|
|
2022-06-11 23:30:42 +02:00
|
|
|
# The repo path is relative to the directory the server `cd`s to before calling `borg serve`, i.e. `repos/<hostname>/`
|
|
|
|
# https://borgbackup.readthedocs.io/en/stable/usage/general.html#repository-urls
|
|
|
|
export BORG_REPO=ssh://borg@borg.caspervk.net:22222/./auto
|
2022-05-10 01:25:47 +02:00
|
|
|
|
2022-06-11 23:30:42 +02:00
|
|
|
# Use stdout of the following command to answer the passphrase question for encrypted repositories
|
2022-05-10 01:25:47 +02:00
|
|
|
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'
|
|
|
|
|
2022-06-11 23:30:42 +02:00
|
|
|
# Initialize remote repository (doesn't matter if it already has been)
|
2022-05-10 01:25:47 +02:00
|
|
|
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 \
|
|
|
|
--show-rc \
|
2022-06-11 23:30:42 +02:00
|
|
|
--progress \
|
2022-05-10 01:25:47 +02:00
|
|
|
--stats \
|
|
|
|
--compression zstd \
|
|
|
|
--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' \
|
2022-06-11 23:30:42 +02:00
|
|
|
--pattern '! /var/lib/docker' \
|
2022-05-10 01:25:47 +02:00
|
|
|
\
|
|
|
|
--pattern '! /home/*/.steam' \
|
|
|
|
--pattern '! /home/*/GOG Games' \
|
|
|
|
--pattern '! /home/*/.cache' \
|
|
|
|
--pattern '! /home/*/Downloads' \
|
|
|
|
--pattern '! /home/*/.local/share/Trash' \
|
2022-06-11 23:30:42 +02:00
|
|
|
--pattern '! /home/*/infrastructure/*/*/data' \
|
2022-05-10 01:25:47 +02:00
|
|
|
\
|
|
|
|
--pattern '+ /media/caspervk/C/Users/Casper/Desktop' \
|
2022-06-11 23:30:42 +02:00
|
|
|
--pattern '+ /media/caspervk/C/Users/Casper/Documents' \
|
2022-05-10 01:25:47 +02:00
|
|
|
--pattern '+ /media/caspervk/C/Program Files (x86)/World of Warcraft/_classic_' \
|
2022-06-11 23:30:42 +02:00
|
|
|
--pattern '+ /media/caspervk/Backup/borg' \
|
|
|
|
--pattern '! /media' \
|
2022-05-10 01:25:47 +02:00
|
|
|
\
|
|
|
|
::'{hostname}-{utcnow}' \
|
|
|
|
/
|
|
|
|
backup_exit=$?
|
|
|
|
|
2022-06-11 23:30:42 +02:00
|
|
|
# Prunes repository by deleting all archives not matching any of the specified retention options. Repository disk space
|
|
|
|
# is NOT freed until `borg compact` is run.
|
|
|
|
# See https://borgbackup.readthedocs.io/en/stable/usage/prune.html for an explanation of the 'keep' options.
|
|
|
|
borg prune \
|
|
|
|
--show-rc \
|
|
|
|
--list \
|
|
|
|
--keep-daily 14 \
|
|
|
|
--keep-weekly 6 \
|
|
|
|
--keep-monthly 12
|
2022-05-10 01:25:47 +02:00
|
|
|
prune_exit=$?
|
|
|
|
|
2022-06-11 23:30:42 +02:00
|
|
|
# _Actually_ free repository disk space by compacting segments
|
|
|
|
borg compact
|
|
|
|
compact_exit=$?
|
|
|
|
|
2022-05-10 01:25:47 +02:00
|
|
|
|
2022-06-11 23:30:42 +02:00
|
|
|
# Use highest exit code as global exit code
|
2022-05-10 01:25:47 +02:00
|
|
|
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
|
2022-06-11 23:30:42 +02:00
|
|
|
global_exit=$(( compact_exit > global_exit ? compact_exit : global_exit ))
|
2022-05-10 01:25:47 +02:00
|
|
|
exit ${global_exit}
|