Files
coredesktop/system/backup
T
2026-10-26 16:21:27 -06:00

69 lines
2.4 KiB
Bash
Executable File

#!/bin/bash
#
# Purpose : Back up your home folder with restic to your Nextcloud files, in
# .backup-<hostname>. Run daily by coredesktop-backup.timer;
# `configure backup` sets it up.
#
# Keeps 7 daily, 4 weekly and 6 monthly backups. To leave out more, list
# paths or patterns in ~/.config/coredesktop/backup-exclude, one per line.
# Wrapped in { ...; exit; } so bash reads the whole script up front; update
# replaces this file (git pull) while a backup may be running.
{
COREDESKTOP="$HOME/.local/share/coredesktop"
USER_EXCLUDE="$HOME/.config/coredesktop/backup-exclude"
LOG_HINT="See: journalctl --user -u coredesktop-backup"
# Tell the desktop when a backup fails, as no one watches the timer
fail() {
echo "$1" >&2
notify-send --app-name=CoreDesktop --icon=dialog-error "Backup failed" "$1" 2> /dev/null
exit 1
}
source "$COREDESKTOP/system/backup-env.sh" || exit 1
[ -n "$RESTIC_REPOSITORY" ] || fail "Backups aren't set up. Run: configure backup"
[ -n "$RESTIC_PASSWORD_COMMAND" ] || fail "The backup's passphrase isn't in your keyring. Run: configure backup"
# Run at login, perhaps before the network is up; give it a few minutes
for try in {1..10}; do
curl -sf --max-time 10 -o /dev/null "${BACKUP_WEBDAV%%/remote.php/*}/status.php" && break
sleep 30
done
# Rebuilt by restic, or by reinstalling, so not worth the space
excludes=(
"$HOME/.cache"
"$HOME/.davfs2/cache"
"$HOME/.local/share/Trash"
"$HOME/.local/share/containers"
"$HOME/.local/share/flatpak"
"$HOME/.local/share/siblings"
"$HOME/.local/share/coredesktop"
"$HOME/.var/app/*/cache"
"$HOME/.npm"
"$HOME/.cargo/registry"
"$HOME/go/pkg/mod"
)
exclude_args=(--exclude-caches)
for path in "${excludes[@]}"; do
exclude_args+=(--exclude "$path")
done
[ -f "$USER_EXCLUDE" ] && exclude_args+=(--exclude-file "$USER_EXCLUDE")
echo "Backing up $HOME to $BACKUP_DIR in your Nextcloud files..."
# --one-file-system leaves out other mounts and btrfs subvolumes inside home
restic backup --one-file-system --host "$HOSTNAME" "${exclude_args[@]}" "$HOME"
status=$?
# 3 means some files couldn't be read; the rest were backed up
(( status == 0 || status == 3 )) || fail "restic couldn't back up $HOME (exit $status). $LOG_HINT"
echo "Removing old backups..."
restic forget --host "$HOSTNAME" --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune \
|| fail "restic couldn't remove old backups. $LOG_HINT"
echo "Backup finished."
exit 0
}