#!/usr/bin/env bash # Nightly encrypted backup of the server's important data with restic. # Installed on the server as /usr/local/sbin/backup-server.sh and run by /etc/cron.d/server-backup (as root). # # Backs up: ~/stack (compose file, Caddyfile, generated site), the Caddy certificate volume, SSH/fail2ban/iptables/docker # configs, and a small "system facts" folder (package list, containers, firewall, sshd settings). # Skips: big, rebuildable folders (see the --exclude flag below). # Keeps: 7 daily, 4 weekly, 6 monthly snapshots (pruned on Sundays; 10% of the data is verified every Sunday). # Status: /var/lib/backup-status/last_success (unix time) and last_failure; log in /var/log/server-backup.log set -uo pipefail umask 027 export RESTIC_REPOSITORY=/home/ubuntu/backups/restic-repo export RESTIC_PASSWORD_FILE=/root/.restic-password STATE=/var/lib/backup-status META=/root/backup-meta mkdir -p "$STATE" "$META" "$(dirname "$RESTIC_REPOSITORY")" log() { echo "[$(date -u +%FT%TZ)] $*"; } fail() { log "FAILED: $*"; date -u +%s > "$STATE/last_failure"; exit 1; } exec 9>/var/lock/backup-server.lock flock -n 9 || { log "another backup is already running, exiting"; exit 1; } [ -s "$RESTIC_PASSWORD_FILE" ] || fail "password file $RESTIC_PASSWORD_FILE is missing" command -v restic >/dev/null || fail "restic is not installed" log "collecting system facts" { echo "date: $(date -u)"; uname -a; lsb_release -ds 2>/dev/null echo; echo "== disk"; df -h echo; echo "== memory"; free -m } > "$META/system.txt" 2>&1 dpkg --get-selections > "$META/packages.txt" 2>&1 docker ps -a --format '{{.Names}} | {{.Image}} | {{.Status}}' > "$META/containers.txt" 2>&1 docker volume ls > "$META/volumes.txt" 2>&1 systemctl list-unit-files --state=enabled > "$META/enabled-units.txt" 2>&1 iptables-save > "$META/iptables-live.txt" 2>&1 sshd -T 2>/dev/null | grep -Ev '^(hostkey|authorizedkeysfile)' > "$META/sshd-effective.txt" [ -f "$RESTIC_REPOSITORY/config" ] || { log "initialising repository"; restic init || fail "restic init"; } PATHS=() for p in /home/ubuntu/stack /var/lib/docker/volumes/stack_caddy_data/_data /var/lib/docker/volumes/stack_caddy_config/_data \ /etc/ssh/sshd_config.d /etc/fail2ban /etc/iptables /etc/docker /etc/fstab /etc/cron.d /etc/crontab /etc/logrotate.d/server-backup \ /usr/local/sbin/backup-server.sh /home/ubuntu/.ssh/authorized_keys "$META"; do [ -e "$p" ] && PATHS+=("$p") done log "backing up ${#PATHS[@]} locations" restic backup --tag nightly --exclude-caches --exclude /home/ubuntu/stack/site/img/big-rebuildable-folder "${PATHS[@]}" rc=$? if [ $rc -eq 3 ]; then log "WARNING: some files could not be read (snapshot still saved)"; rc=0; fi [ $rc -eq 0 ] || fail "restic backup exited with $rc" if [ "$(date +%u)" = "7" ]; then log "weekly: pruning old snapshots" restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune || fail "forget/prune" log "weekly: verifying 10% of the stored data" restic check --read-data-subset=10% || fail "restic check" fi # let the ubuntu user read (not change) the repository so your PC can pull a copy over SSH chgrp -R ubuntu "$RESTIC_REPOSITORY" && chmod -R g+rX,o-rwx "$RESTIC_REPOSITORY" date -u +%s > "$STATE/last_success" log "OK: $(restic snapshots --compact 2>/dev/null | tail -1)"