VPS & servers2 min read

Encrypted backups with restic: set up, automate, restore

Back up your data encrypted and space-efficiently to another server or S3 storage with restic, prune old snapshots automatically and restore them in an emergency.

A backup stored on the same server does not help you when the server is gone or an attacker deletes everything. restic backs up your data encrypted to another location, stores every file only once (deduplication) and can restore any older snapshot.

The backups in the TryNxt panel cover the whole server. restic complements them: you back up specific data, restore individual files and keep a copy with another provider.

Install

sudo apt update
sudo apt install restic
restic version

Where to back up to?

restic supports many targets. The most common:

Target Address (repository)
another server via SFTP sftp:user@backup.example.com:/backups/myserver
S3-compatible storage s3:https://s3.example.com/my-bucket
local disk (only as an extra) /mnt/backup/restic

For SFTP you need an SSH key without a passphrase for root on the target server, see Create SSH keys.

Store the credentials

So that backups can later run automatically, store the address and password in a file only root can read:

sudo mkdir -p /root/.restic
sudo nano /root/.restic/env
export RESTIC_REPOSITORY="sftp:backup@backup.example.com:/backups/myserver"
export RESTIC_PASSWORD_FILE="/root/.restic/password"
openssl rand -base64 32 | sudo tee /root/.restic/password
sudo chmod 600 /root/.restic/*

Keep the password in a safe place outside the server as well, for example in your password manager. Without the password the backup cannot be decrypted, not even by you.

Create the repository and the first backup

The following commands run as root (sudo -i):

source /root/.restic/env
restic init
restic backup /etc /var/www /home --exclude-caches --exclude='*.log'
restic snapshots

The first backup takes longer; every following one only transfers the changes.

Include databases

Do not back up databases as running files but as a dump. restic can take it in directly:

mariadb-dump --all-databases --single-transaction | restic backup --stdin --stdin-filename all-databases.sql

Prune old snapshots

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

This keeps the last 7 days, 4 weeks and 6 months and frees the space of the rest.

Automatically every night

Create a script:

sudo nano /usr/local/bin/restic-backup.sh
#!/bin/bash
set -euo pipefail
source /root/.restic/env
restic backup /etc /var/www /home --exclude-caches --exclude='*.log'
mariadb-dump --all-databases --single-transaction | restic backup --stdin --stdin-filename all-databases.sql
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
restic check
sudo chmod 700 /usr/local/bin/restic-backup.sh

How to start it every night and get notified on errors is described in Cron jobs and systemd timers. Leave out the mariadb-dump line if no database runs on the server.

Restore

A single file from the latest snapshot:

source /root/.restic/env
restic restore latest --target /tmp/restore --include /var/www/example.com/wp-config.php

A whole older snapshot into its own folder:

restic snapshots
restic restore 4f2a9c1b --target /tmp/restore

Restore the database (--path picks the latest snapshot of the database dump):

restic dump --path /all-databases.sql latest /all-databases.sql | mariadb

Test the restore

A backup only counts if you can restore it. Restore a file once a month and check it. restic check in the script makes sure the repository itself is intact.

Written by TryNxt team

We build and run TryNxt ourselves and write down here what we learn along the way.