borg local backup script works

This commit is contained in:
Christoph J. Scherr 2023-06-11 17:01:35 +02:00
parent 4c7a08dd34
commit a1ee59ba09
Signed by: PlexSheep
GPG Key ID: 25B4ACF7D88186CC
1 changed files with 28 additions and 11 deletions

View File

@ -1,16 +1,28 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# run as root
export euid=$(id -u)
if [ ${euid} -eq 0 ]; then
echo "Running as root"
else
echo "This script should be run as root"
exit
fi
# check if password was set # check if password was set
if [[ -z "${BORG_PASSPHRASE}" ]]; then if [[ -z "${BORG_PASSPHRASE}" ]]; then
echo 'BORG_PASSPHRASE is not set, `export BORG_PASSPHRASE=PASS`' echo 'BORG_PASSPHRASE is not set, `export BORG_PASSPHRASE=PASS`'
exit
fi fi
export BORG_REPO=fileserver@homeserver.box:/srv/data/backup/borg/theseus export REMOTE_USER=fileserver@homeserver.box
export REMOTE_PATH=/srv/data/backup/borg/theseus
export BORG_REPO=$REMOTE_USER:$REMOTE_PATH
export BORG_BIN="$(which borg)" export BORG_BIN="$(which borg)"
# some helpers and error handling: # some helpers and error handling:
info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; } info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
info "Starting backup" echo "Starting backup"
# Backup the most important directories into an archive named after # Backup the most important directories into an archive named after
# the machine this script is currently running on: # the machine this script is currently running on:
@ -24,19 +36,24 @@ $BORG_BIN create \
--compression zstd,11 \ --compression zstd,11 \
--exclude-caches \ --exclude-caches \
--exclude 'home/*/.cache/*' \ --exclude 'home/*/.cache/*' \
--exclude 'home/Download/*' \ --exclude 'home/*/Documents/code/compile/*' \
--exclude 'home/\.local/share/baloo/*' \ --exclude '*Cache*' \
--exclude 'home/*/Download/*' \
--exclude 'home/*/.local/*' \
--exclude '*JetBrains/Toolbox/apps*' \
--exclude '*/.Trash*' \ --exclude '*/.Trash*' \
--exclude '*/Trash*' \ --exclude '*/Trash*' \
--exclude '*/.Steam*' \ --exclude '*/build/*' \
--exclude '*Steam*' \
\ \
${BORG_REPO}::'{hostname}-{now}' \ ${BORG_REPO}::'{hostname}-{now}' \
/home \ /home \
/root \
/etc /etc
backup_exit=$? backup_exit=$?
info "Pruning repository" echo "Pruning repository"
# Use the `prune` subcommand to maintain 3 daily, 3 weekly and 6 monthly # Use the `prune` subcommand to maintain 3 daily, 3 weekly and 6 monthly
# archives of THIS machine. The '{hostname}-*' matching is very important to # archives of THIS machine. The '{hostname}-*' matching is very important to
@ -55,7 +72,7 @@ prune_exit=$?
# actually free repo disk space by compacting segments # actually free repo disk space by compacting segments
info "Compacting repository" echo "Compacting repository"
$BORG_BIN compact $BORG_BIN compact
@ -66,13 +83,13 @@ global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
global_exit=$(( compact_exit > global_exit ? compact_exit : global_exit )) global_exit=$(( compact_exit > global_exit ? compact_exit : global_exit ))
if [ ${global_exit} -eq 0 ]; then if [ ${global_exit} -eq 0 ]; then
info "Backup, Prune, and Compact finished successfully" echo "Backup, Prune, and Compact finished successfully"
elif [ ${global_exit} -eq 1 ]; then elif [ ${global_exit} -eq 1 ]; then
info "Backup, Prune, and/or Compact finished with warnings" echo "Backup, Prune, and/or Compact finished with warnings"
else else
info "Backup, Prune, and/or Compact finished with errors" echo "Backup, Prune, and/or Compact finished with errors"
fi fi
du -hs $BORG_REPO ssh $REMOTE_USER du -hs $REMOTE_PATH
exit ${global_exit} exit ${global_exit}