dotfiles/.local/bin/backup.sh

85 lines
1.8 KiB
Bash
Executable File

#!/bin/bash
REPO_SERVER_SSH="spacedock-backups"
REPO_SERVER_HOSTNAME="spacedock.servers.ezri.dev"
export BORG_REPO=ssh://${REPO_SERVER_SSH}/~/$(</etc/hostname)/${USER}
# Extract the key from the secret service
export BORG_PASSPHRASE=$(secret-tool lookup service borgbackup account ezri@normandy)
exclude_dirs=(
"$HOME/.cache"
"$HOME/.local/share/Steam"
"$HOME/Downloads"
"$HOME/.local/share/Trash"
"$HOME/.local/share/CKAN"
"$HOME/.local/state"
"/games/steamapps/downloading/"
"/secondary/SteamLibrary/steamapps/downloading/"
"/games/steamapps/shadercache/"
"/secondary/SteamLibrary/steamapps/shadercache/"
"/secondary/lost+found/"
)
# Back up home directory and steam library
backup_dirs=(
"$HOME"
"/games"
"/secondary"
)
trap 'echo Backup interrupted >&2; exit 2' INT TERM
# Ensure we can connect to the remote host
while true; do
ping -c 1 -W 1 ${REPO_SERVER_HOSTNAME} > /dev/null
if [ $? -eq 0 ]; then
break
fi
echo "Could not connect to ${REPO_SERVER_HOSTNAME}, retrying in 5 seconds..."
sleep 5
done
echo "Starting backup..."
borg create \
--verbose \
--filter AME \
--list \
--stats \
--show-rc \
--compression lz4 \
--sparse \
--one-file-system \
--exclude-caches \
--exclude-from /home/ezri/.borgignore \
::'{now}' \
"${backup_dirs[@]}"
backup_exit=$?
echo "Backup complete, pruning repository..."
borg prune \
--verbose \
--list \
--show-rc \
--keep-hourly 48 \
--keep-daily 12 \
--keep-weekly 6 \
--keep-monthly 10
prune_exit=$?
echo "Prune complete, compacting repository..."
borg compact
compact_exit=$?
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
global_exit=$(( compact_exit > global_exit ? compact_exit : global_exit ))
exit ${global_exit}