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

88 lines
5.3 KiB
Bash

#!/bin/bash
# This script is based on the script at:
# https://borgbackup.readthedocs.io/en/stable/quickstart.html#automating-backups
# 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
# Use stdout of the following command to answer the passphrase question for encrypted repositories
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'
# Initialize remote repository (doesn't matter if it already has been)
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 \
--progress \
--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' \
\
--pattern '! /var/lib/apport/coredump' \
--pattern '! /var/lib/docker' \
--pattern '! /var/lib/snapd/snaps' \
--pattern '! /**/.cache' \
--pattern '! /**/Cache' \
--pattern '! /**/cache' \
--pattern '- *.tmp' \
\
--pattern '! /home/*/.steam' \
--pattern '! /home/*/GOG Games' \
--pattern '! /home/*/Downloads' \
--pattern '! /home/*/.local/share/Trash' \
\
--pattern '+ /media/caspervk/C/Users/Casper/Desktop' \
--pattern '+ /media/caspervk/C/Users/Casper/Documents' \
--pattern '+ /media/caspervk/C/Program Files (x86)/World of Warcraft/_classic_' \
--pattern '+ /media/caspervk/Backup/borg' \
--pattern '- /media' \
\
::'{hostname}-{utcnow}' \
/
backup_exit=$?
# 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
prune_exit=$?
# _Actually_ free repository disk space by compacting segments
borg compact
compact_exit=$?
# Use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
global_exit=$(( compact_exit > global_exit ? compact_exit : global_exit ))
exit ${global_exit}