Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Install restic

# Debian and Ubuntu
sudo apt install restic
# AlmaLinux and Rocky Linux (EPEL)
sudo dnf install epel-release
sudo dnf install restic

Run backups as root so every file can be read. Check the version with restic version.

Step 1: Create the Password File

sudo install -m 600 /dev/null /root/.restic-password
sudo nano /root/.restic-password    # one line: STRONG_PASSPHRASE

restic encrypts everything with this password, and there is no recovery without it: lose it and the backups are unreadable. Store a copy in a password manager or another place off the server.

Step 2: Initialize a Repository over SFTP

export RESTIC_REPOSITORY='sftp:user@your_server_ip:/srv/restic/web1'
export RESTIC_PASSWORD_FILE=/root/.restic-password
restic init

restic logs in with the system ssh client, so key login must work for root first, with no prompt in between. Create /srv/restic on the backup host beforehand, writable by the SSH user.

Or Use S3-Compatible Object Storage

Put the settings in an environment file with mode 600, not on a command line:

# /etc/restic/env  (chmod 600)
RESTIC_REPOSITORY=s3:https://S3_ENDPOINT/BUCKET/web1
RESTIC_PASSWORD_FILE=/root/.restic-password
AWS_ACCESS_KEY_ID=ACCESS_KEY
AWS_SECRET_ACCESS_KEY=SECRET_KEY
set -a; . /etc/restic/env; set +a
restic init

The variable names come from the S3 protocol and work with any S3-compatible service. Give the key access to this one bucket only.

Step 3: Back Up

restic backup /etc /home /root /var/www /var/backups \
  --exclude-caches --exclude '/home/*/.cache'

Each run creates a snapshot. Only new data is uploaded. Dump databases into /var/backups first: see mysqldump and pg_dump.

Step 4: List Snapshots and Files

restic snapshots
restic ls latest /etc/nginx

latest stands for the newest snapshot; you can also give a snapshot ID from the list. To find which snapshots contain a file, use restic find nginx.conf.

Where the Repository Should Live

Not on the server you back up, and not on the same disk. An SFTP repository on a second server is one copy; an S3-compatible bucket at another provider, with its own account and keys, is the off-site copy. A snapshot of the server at the same provider is a useful extra layer, not a replacement for either. The difference is explained in snapshot vs backup.

Step 5: Restore and Compare

Restore into a scratch directory, never over live files, and compare:

restic restore latest --target /tmp/restore-test --include /etc/nginx/nginx.conf
diff /tmp/restore-test/etc/nginx/nginx.conf /etc/nginx/nginx.conf
restic restore latest --target /tmp/restore-test --include /var/www
diff -r /tmp/restore-test/var/www /var/www

Differences in files that changed since the snapshot are expected. Do this test every month.

Step 6: Forget and Prune

restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --dry-run
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune

forget removes snapshots from the list; --prune then deletes the data no longer used and gives the disk space back. Both are permanent, so read the dry run first. Then check the repository:

restic check
restic check --read-data-subset=5%

The second form reads a sample of the stored data as well.

Schedule It

Use a systemd service with EnvironmentFile=/etc/restic/env and a timer, the same pattern as in the BorgBackup guide. With cron, call a root-owned script that loads the environment file; never put the password or keys in the crontab line. For where this copy belongs in a full plan, see the 3-2-1 backup rule.

Was this answer helpful?
Loading...