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

94 lines
5.8 KiB
Bash
Raw Permalink Normal View History

2022-05-10 01:25:47 +02:00
#!/bin/bash
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-12 03:08:48 +02:00
\
--pattern '! /var/lib/apport/coredump' \
2022-06-11 23:30:42 +02:00
--pattern '! /var/lib/docker' \
2022-11-13 21:02:13 +01:00
--pattern '! /var/lib/libvirt/images' \
2022-06-12 03:08:48 +02:00
--pattern '! /var/lib/snapd/snaps' \
2022-11-13 21:02:13 +01:00
--pattern '! /var/local/borg/repos' \
2022-06-12 03:08:48 +02:00
--pattern '! /**/.cache' \
--pattern '! /**/Cache' \
--pattern '! /**/cache' \
2022-11-13 21:02:13 +01:00
--pattern '! /**/.terraform' \
2022-06-12 03:08:48 +02:00
--pattern '- *.tmp' \
2022-05-10 01:25:47 +02:00
\
--pattern '! /home/*/.steam' \
2022-11-13 21:02:13 +01:00
--pattern '! /home/*/Android/Sdk' \
2022-05-10 01:25:47 +02:00
--pattern '! /home/*/GOG Games' \
--pattern '! /home/*/Downloads' \
2022-11-13 21:02:13 +01:00
--pattern '! /home/*/go' \
--pattern '! /home/*/.local/share/JetBrains/Toolbox/apps' \
2022-05-10 01:25:47 +02:00
--pattern '! /home/*/.local/share/Trash' \
\
--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' \
2022-06-12 03:08:48 +02:00
--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}