PortGate

Blueprints · Systemd & Agents

Production 25 min · restic

restic encrypted offsite backups

Installs restic and writes a backup of /etc, /var/www, and /var/backups. Retention is 7 daily, 4 weekly, and 6 monthly. The repository password and the run itself stay out of the bundle.

Live config

Baked-in protections

Step 1 · bash

Install restic

/root/portgate-blueprints/restic-install.sh

Uses apt or apk when one of them exists.

#!/bin/sh
set -eu
if command -v apt-get >/dev/null 2>&1; then
  apt-get update
  apt-get install -y restic
elif command -v apk >/dev/null 2>&1; then
  apk add --no-cache restic
else
  echo "Install restic with the distro package manager."
fi
install -d -m 700 /root/portgate-backups
if [ ! -f /root/portgate-backups/restic.pass ]; then
  install -m 600 /dev/null /root/portgate-backups/restic.pass
  echo "Write the repository password into /root/portgate-backups/restic.pass"
fi

Step 2 · bash

Backup script

/root/portgate-blueprints/restic-backup.sh

Export AWS or R2 credentials in the root environment that cron uses. Initialize the repository once with restic init.

#!/bin/sh
set -eu
export RESTIC_REPOSITORY="s3:s3.amazonaws.com/backups"
export RESTIC_PASSWORD_FILE=/root/portgate-backups/restic.pass
test -s "$RESTIC_PASSWORD_FILE"
restic backup /etc /var/www /var/backups
restic forget --prune --keep-daily 7 --keep-weekly 4 --keep-monthly 6
if [ "https://example.com/heartbeat" != "https://example.com/heartbeat" ]; then
  curl -fsS -m 15 "https://example.com/heartbeat"
fi