Restic is a fast, open-source backup tool that encrypts everything on the client and stores each file chunk only once, no matter how many snapshots reference it. This guide takes a Linux server from zero to automated, verified, offsite backups: install, repository setup, a backup script, a systemd timer, retention, integrity checks, and the restore drill most guides skip.

What you'll build: a nightly backup of /etc, /home, /var/www, and database dumps to an encrypted Restic repository, pruned to 7 daily, 4 weekly, and 12 monthly snapshots, with a weekly integrity check. Plan on about 30 minutes.

Why Restic

  • Client-side encryption. Data is encrypted with AES-256 (with Poly1305 authentication) before it leaves the server. The storage provider only ever sees ciphertext.
  • Deduplication. Files are split into content-defined chunks, so a 1 GB log file that grows by 10 MB only uploads the new 10 MB.
  • Snapshots. Every run creates a point-in-time snapshot you can browse, diff, or mount.
  • Many backends. Local disk, SFTP, S3 and S3-compatible storage (Backblaze B2, Wasabi, MinIO), Azure Blob Storage, Google Cloud Storage, Restic's own REST server, and anything rclone supports.
  • One static binary. No agent, no database, nothing to keep running between backups.

Prerequisites

  • Ubuntu 22.04/24.04, Debian 12, or RHEL 9 family (commands below use Ubuntu)
  • sudo access
  • A backup destination: a second disk, a NAS reachable over SFTP, or an S3-compatible / Azure Blob bucket

Step 1: Install Restic

sudo apt update
sudo apt install restic
restic version

Distribution packages lag behind upstream. For the newest release, download the binary from the Restic GitHub releases page into /usr/local/bin; after that, sudo restic self-update keeps it current. (The Debian/Ubuntu package disables self-update.) On RHEL-family systems, Restic is in EPEL: sudo dnf install restic.

Step 2: Create a protected config and password

Keep repository settings in one root-only file so the backup script, the systemd unit, and your interactive shell all use the same values.

sudo mkdir -p /etc/restic
sudo sh -c 'openssl rand -base64 32 > /etc/restic/password'
sudo chmod 600 /etc/restic/password

Copy that password somewhere safe now, such as a password manager or a sealed envelope. Restic has no recovery mechanism. Lose the password and the backups are permanently unreadable, which is exactly the point of the encryption.

Now create /etc/restic/restic.env with the variables for your backend. Pick one block:

# Local disk or mounted NAS
RESTIC_REPOSITORY=/mnt/backup/restic

# SFTP to a NAS or another server (uses root's SSH key)
RESTIC_REPOSITORY=sftp:backup@nas.lan:/srv/restic/web01

# S3 or S3-compatible (Backblaze B2's S3 endpoint, Wasabi, MinIO)
RESTIC_REPOSITORY=s3:https://s3.us-west-004.backblazeb2.com/my-backup-bucket/web01
AWS_ACCESS_KEY_ID=your-key-id
AWS_SECRET_ACCESS_KEY=your-application-key

# Azure Blob Storage
RESTIC_REPOSITORY=azure:restic-backups:/web01
AZURE_ACCOUNT_NAME=mystorageaccount
AZURE_ACCOUNT_KEY=your-storage-account-key

# Always required
RESTIC_PASSWORD_FILE=/etc/restic/password
sudo chmod 600 /etc/restic/restic.env

Step 3: Initialize the repository

sudo -i
set -a; . /etc/restic/restic.env; set +a
restic init

set -a exports every variable in the file, so Restic sees them. You should see created restic repository ... at .... Run this once per repository.

Step 4: Run and inspect your first backup

Create an exclude list so caches and temporary files don't waste space:

cat > /etc/restic/excludes <<'EOF'
/home/*/.cache
/var/cache
/var/tmp
*.tmp
node_modules
EOF
restic backup /etc /home /var/www \
    --exclude-file=/etc/restic/excludes \
    --exclude-caches \
    --one-file-system \
    --tag nightly

The first run uploads everything. Later runs only upload changed chunks, so they're usually done in seconds to minutes. Check what you have:

restic snapshots                  # list snapshots
restic ls latest /etc/nginx       # browse a snapshot
restic diff <old-id> <new-id>     # what changed between two runs
restic stats --mode raw-data      # real space used after dedup

Step 5: Back up databases the right way

Copying the live files of a running database gives you a backup that may not restore. Dump first, then let Restic back up the dump:

# PostgreSQL
sudo -u postgres pg_dumpall | restic backup --stdin --stdin-filename postgres.sql --tag db

# MySQL / MariaDB (InnoDB)
mysqldump --all-databases --single-transaction --routines --events \
    | restic backup --stdin --stdin-filename mysql.sql --tag db

Streaming with --stdin means the dump never touches local disk, and deduplication still works between nights.

Step 6: Write the backup script

Save as /usr/local/sbin/restic-backup.sh:

#!/usr/bin/env bash
set -euo pipefail

set -a; . /etc/restic/restic.env; set +a

# 1. Files
restic backup /etc /home /var/www \
    --exclude-file=/etc/restic/excludes \
    --exclude-caches --one-file-system \
    --tag nightly

# 2. Databases (delete the lines you don't need)
if command -v pg_dumpall >/dev/null; then
    sudo -u postgres pg_dumpall | restic backup --stdin --stdin-filename postgres.sql --tag db
fi

# 3. Retention: 7 daily, 4 weekly, 12 monthly, then reclaim space
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 12 --prune

# 4. Integrity: read 5% of the data on Sundays, metadata only otherwise
if [ "$(date +%u)" -eq 7 ]; then
    restic check --read-data-subset=5%
else
    restic check
fi
sudo chmod 700 /usr/local/sbin/restic-backup.sh
sudo /usr/local/sbin/restic-backup.sh

set -euo pipefail makes the script stop on the first failure, including a failed pg_dumpall in a pipe, so a broken dump never quietly becomes "last night's backup".

Step 7: Schedule it with a systemd timer

A systemd timer beats cron here. It logs to the journal, catches up after the server was off (Persistent=true), and won't start a second run while one is still going.

/etc/systemd/system/restic-backup.service:

[Unit]
Description=Restic backup
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/restic-backup.sh
Nice=10
IOSchedulingClass=idle

/etc/systemd/system/restic-backup.timer:

[Unit]
Description=Nightly Restic backup

[Timer]
OnCalendar=*-*-* 02:00:00
RandomizedDelaySec=15m
Persistent=true

[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable --now restic-backup.timer
systemctl list-timers restic-backup.timer
journalctl -u restic-backup.service -n 50   # read the last run

Prefer cron? 0 2 * * * /usr/local/sbin/restic-backup.sh >> /var/log/restic.log 2>&1 in root's crontab does the same job without the catch-up behavior.

Step 8: Practice a restore (don't skip this)

A backup you've never restored is a hope, not a backup. Restore into a scratch directory, never on top of live data:

# Restore one directory from the latest snapshot
restic restore latest --tag nightly --target /tmp/restore --include /etc/nginx

# Restore a database dump
restic dump latest --tag db /postgres.sql > /tmp/postgres.sql

# Browse every snapshot as a filesystem (needs fuse)
mkdir -p /mnt/restic && restic mount /mnt/restic

The --tag filter matters. Files and database dumps are separate snapshots, so plain latest means whichever ran last. Put a monthly restore test on your calendar. Restore a random directory and one database dump, and confirm the files open.

Hardening and best practices

  • Follow 3-2-1. Three copies, two kinds of media, one offsite. A local repository plus a cloud repository is easy: run the script twice with two env files, or use restic copy.
  • Protect against ransomware. If an attacker gets root, they get your repository credentials. Use an append-only REST server (rest-server --append-only) or object lock / immutability on the bucket, and run forget --prune from a separate trusted machine.
  • Alert on failure. Add OnFailure= to the service, or ping a dead-man's-switch monitor such as Healthchecks.io at the end of the script. Silent failures are how backups die.
  • One repository, many hosts. Several servers can share a repository, and deduplication works across them. Filter with restic snapshots --host web01.
  • Keep the password offline too. If the server dies, the password file dies with it.

Where to store the repository

DestinationGood forWatch out for
Second local diskFast restores, no egress costNot offsite; dies in the same fire or ransomware event
NAS over SFTPHome labs and small officesUse a dedicated SSH key restricted to the backup path
Backblaze B2 / Wasabi (S3 API)Cheap offsite storageCheck minimum retention and egress terms before you choose
Azure Blob StorageTeams already in AzureUse the Cool tier; enable immutability policies for ransomware protection
AWS S3Teams already in AWSRestores incur egress charges; consider S3 Object Lock

Frequently Asked Questions

How long does a Restic backup take?

The first backup is limited by disk read speed and upload bandwidth. After that, only changed chunks are uploaded, so nightly runs on a typical web server finish in under a few minutes.

How secure are Restic backups?

Data is encrypted and authenticated on the client with AES-256 and Poly1305, using a key derived from your password with scrypt. The storage backend never sees plaintext or your password.

What happens if I forget the repository password?

The data can't be recovered. Store the password in at least two places, and never only on the server being backed up.

Restic vs Borg: which should I use?

Both deduplicate and encrypt. Borg is very efficient but works best over SSH to a host running Borg. Restic writes directly to cloud object storage with no server component, which makes it the easier choice for S3, B2, or Azure Blob.

Next, protect the files you're backing up. See how to set up a secure Samba file server on Ubuntu, or monitor the server with the CLI tools every sysadmin should know.