Skip to main content

Linux and servers · Intermediate · 17 min

Back up a server to an encrypted repository with restic

By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · restic 0.20.0

You will install restic 0.20, back up /etc and /srv into an encrypted, deduplicated repository, compare and restore snapshots, prune them on a retention policy, check the data, and know how to add an off-site copy over SFTP or S3.

On this page

restic backs up directories into a repository where every file is split into chunks, deduplicated and encrypted with a key that only your password unlocks. A second backup of the same server only stores the chunks that changed, so you can keep many snapshots cheaply. The repository can sit on a local disk, on another server over SFTP, or in object storage such as S3.

In this tutorial you back up /etc (the server's configuration) and /srv (its site data) into a local repository on Ubuntu 26.04. You take two snapshots, compare them, restore a single file and a whole directory, apply a retention policy, and check that the stored data can be read back. The last step shows how the same commands point at an off-site repository. A copy on the same disk protects you from mistakes, not from a dead disk or a lost server, so treat the off-site step as part of the job.

Step 1 · Install restic 0.20 from the official release

Ubuntu 26.04's archive carries restic 0.18.1. This tutorial uses 0.20.0, the current release, which the restic project publishes as a single compressed binary with a list of SHA-256 checksums. Install the tools to fetch and unpack it:

Bash
sudo apt-get update
sudo apt-get install -y curl bzip2 ca-certificates

Download the binary for your CPU (amd64 or arm64, which dpkg reports) and the checksum list into a throwaway folder:

Bash
mkdir -p ~/restic-install
cd ~/restic-install
ARCH=$(dpkg --print-architecture)
curl -fsSLO "https://github.com/restic/restic/releases/download/v0.20.0/restic_0.20.0_linux_${ARCH}.bz2"
curl -fsSLO https://github.com/restic/restic/releases/download/v0.20.0/SHA256SUMS
sha256sum --check --ignore-missing SHA256SUMS

The last line must print restic_0.20.0_linux_amd64.bz2: OK (or arm64). If it prints FAILED, stop and download again; do not install a file whose checksum does not match.

Unpack it and install it to /usr/local/bin, then remove the download folder:

Bash
bunzip2 "restic_0.20.0_linux_${ARCH}.bz2"
sudo install -m 755 "restic_0.20.0_linux_${ARCH}" /usr/local/bin/restic
cd ~
rm -rf ~/restic-install
restic version

A binary installed by hand gets no updates from apt, including security fixes. restic can upgrade itself: this downloads the newest release, checks its signature and replaces /usr/local/bin/restic. Run it when a new release is announced on the project's GitHub releases page:

Bash
sudo restic self-update

Check it worked

restic version prints a line that starts with restic 0.20.0 compiled with go, followed by the Go version and on linux/amd64 (or linux/arm64). which restic prints /usr/local/bin/restic.

Step 2 · Create the password file and the repository

Everything in the repository is encrypted with a key that only your password unlocks. If you lose the password, the backups cannot be read by anyone, including you. Store a copy of it in your password manager before you rely on the repository.

restic needs to read files only root can read (such as /etc/shadow), so it runs as root. Keep its settings in a folder under /root, created private first, then create an empty password file with mode 600 and fill it with 32 random bytes in base64. The password never appears on the command line or in your shell history:

Bash
sudo install -d -m 700 /root/restic
sudo install -m 600 /dev/null /root/restic/password
head -c 32 /dev/urandom | base64 | sudo tee /root/restic/password > /dev/null

Print it once to copy it into your password manager, then clear the terminal:

Bash
sudo cat /root/restic/password

restic reads the repository location from RESTIC_REPOSITORY and the password file from RESTIC_PASSWORD_FILE. sudo drops your environment variables, so put both in a small wrapper script instead. Every command in this tutorial then runs as sudo restic-local ...:

Bash
sudo tee /usr/local/sbin/restic-local > /dev/null <<'EOF'
#!/bin/sh
# restic with this server's local repository and password file.
export RESTIC_REPOSITORY=/var/backups/restic
export RESTIC_PASSWORD_FILE=/root/restic/password
exec /usr/local/bin/restic "$@"
EOF
sudo chmod 755 /usr/local/sbin/restic-local

The repository lives in /var/backups/restic, outside the folders you back up, so a backup never tries to include itself. Create it:

Bash
sudo restic-local init

Check it worked

init prints created restic repository followed by an ID and the path /var/backups/restic. List the repository folder:

Bash
sudo ls /var/backups/restic

You see config, data, index, keys, locks and snapshots. The files inside are encrypted; sudo cat /var/backups/restic/config shows unreadable bytes, not settings.

Step 3 · Back up /etc and /srv with an excludes file

So the demo has some site data to work with, create a small folder in /srv. It has a cache folder and a temporary file, which are the kind of thing you leave out of backups. If /srv on your server already holds data, leave it as it is; the new folder sits beside it.

Bash
sudo mkdir -p /srv/demo-site/cache
echo '<h1>Demo site, version 1</h1>' | sudo tee /srv/demo-site/index.html > /dev/null
echo 'Notes kept for the site' | sudo tee /srv/demo-site/notes.txt > /dev/null
echo 'rebuilt on every request' | sudo tee /srv/demo-site/cache/page.html > /dev/null
echo 'half-written upload' | sudo tee /srv/demo-site/upload.tmp > /dev/null

Write the exclude patterns to a file, one per line. A pattern that starts with / matches from the root of the path; * matches within one folder name; a pattern without a / such as *.tmp matches a file name anywhere:

Bash
sudo tee /root/restic/excludes > /dev/null <<'EOF'
# Caches are rebuilt by the application.
/srv/*/cache
# Partial uploads and editor leftovers.
*.tmp
*.swp
EOF

Run the first backup:

Bash
sudo restic-local backup /etc /srv --exclude-file /root/restic/excludes

restic scans the folders, reports how many files are new and how much data it added, and finishes with snapshot and an eight-character ID, followed by saved. The first run stores everything; later runs only store what changed.

Check it worked

List the files in the snapshot you just made under the demo folder:

Bash
sudo restic-local ls latest /srv/demo-site

You see /srv/demo-site, index.html and notes.txt, with no cache folder and no upload.tmp. latest always means the newest snapshot.

Step 4 · Take a second snapshot and compare the two

Change the demo site: edit one file, add one and delete one. Then back up again:

Bash
echo '<h1>Demo site, version 2</h1>' | sudo tee /srv/demo-site/index.html > /dev/null
echo '<p>About us</p>' | sudo tee /srv/demo-site/about.html > /dev/null
sudo rm /srv/demo-site/notes.txt
sudo restic-local backup /etc /srv --exclude-file /root/restic/excludes

This time the summary shows a few changed or new files and only a small amount of data added. Unchanged files were not stored a second time.

List the snapshots:

Bash
sudo restic-local snapshots

The table has one row per snapshot, with the ID, the time, the host name and the paths. There are two rows. Copy both IDs, oldest first, and compare the snapshots. Replace the two example IDs with yours:

Bash
sudo restic-local diff 1a2b3c4d 5e6f7a8b

Check it worked

The diff output has one line per changed path, something like:

Text
M    /srv/demo-site/index.html
+    /srv/demo-site/about.html
-    /srv/demo-site/notes.txt

M is modified, + is added and - is removed. A summary below it counts the files and bytes added and removed. When something breaks after a change on the server, a diff between the last good snapshot and the current one shows which configuration files moved.

Step 5 · Restore one file, then a whole directory

Always restore into a separate folder first and copy back what you need. Restoring over live files replaces them without asking.

notes.txt only exists in the first snapshot. Restore that one file into /var/tmp/restic-one. Replace the example ID with the ID of your first snapshot:

Bash
sudo restic-local restore 1a2b3c4d --target /var/tmp/restic-one --include /srv/demo-site/notes.txt

restic recreates the full path under the target, so the file is at /var/tmp/restic-one/srv/demo-site/notes.txt. Read it, then put it back, keeping its owner and mode:

Bash
sudo cat /var/tmp/restic-one/srv/demo-site/notes.txt
sudo cp -a /var/tmp/restic-one/srv/demo-site/notes.txt /srv/demo-site/notes.txt

To read a file without restoring it, dump prints it to the terminal: sudo restic-local dump latest /srv/demo-site/index.html.

Now restore the whole of /srv from the newest snapshot into a second folder (on a server where /srv is large, check df -h /var/tmp first, or restore only /srv/demo-site), and compare it with the live copy. The cache is left out of the comparison because it was never backed up, and so is notes.txt, which you just put back after the snapshot was taken:

Bash
sudo restic-local restore latest --target /var/tmp/restic-restore --include /srv
sudo diff -r --exclude=cache --exclude=upload.tmp --exclude=notes.txt /srv /var/tmp/restic-restore/srv && echo "restore matches /srv"

Check it worked

The first restore prints Notes kept for the site, and ls /srv/demo-site lists notes.txt again. The second command prints restore matches /srv and no Only in or differ lines. sudo ls -l /var/tmp/restic-restore/srv/demo-site shows the same owner, mode and times as the live files, because restic stores them in each snapshot.

When you have finished looking, remove both restore folders:

Bash
sudo rm -rf /var/tmp/restic-one /var/tmp/restic-restore

Step 6 · Apply a retention policy with forget and prune

Without a policy the repository only grows. forget decides which snapshots to keep and removes the rest from the list; --prune then deletes the data that no remaining snapshot uses. Snapshots are grouped by host and backed-up paths, and the policy applies to each group.

On your server, a policy such as --keep-daily 7 --keep-weekly 4 keeps the newest snapshot of each of the last 7 days that have one, and the newest of each of the last 4 weeks. Removing snapshots cannot be undone, so preview a policy first with --dry-run:

Bash
sudo restic-local forget --keep-daily 7 --keep-weekly 4 --dry-run

The preview keeps both of today's snapshots. The newer one is the daily snapshot and weekly snapshot; the older one is kept as the oldest daily snapshot, because while a policy has fewer days or weeks of history than it asks for, restic also keeps the oldest snapshot rather than throw away the only copy of that period. On a server that has run for a few weeks, the same policy starts removing snapshots.

So you can watch a snapshot go in this demo, use a policy that keeps only the newest one. Preview it, then run it for real and prune. Do not use --keep-last 1 on a real server; it leaves you a single restore point:

Bash
sudo restic-local forget --keep-last 1 --dry-run
sudo restic-local forget --keep-last 1 --prune

The preview lists one snapshot to keep and, under a line like remove 1 snapshots, the older one.

Check it worked

Bash
sudo restic-local snapshots

One snapshot is left, the second one. In the middle of the --prune output, restic prints a summary of what it found; its total prune: line shows what was removed, here only a few blobs and a few KiB, since most chunks are shared with the snapshot that stayed. The output ends with done.

Step 7 · Check the repository and read back part of the data

check confirms the repository's structure is consistent: every snapshot points to trees and data that exist. With --read-data-subset, it also downloads and decrypts a share of the stored data, which catches silent corruption on the disk or at the storage provider. With a percentage, restic reads a random tenth of the data packs on each run, so a regular check catches corruption statistically over time without reading the whole repository at once. A random sample does not guarantee that every pack is read in ten runs:

Bash
sudo restic-local check --read-data-subset 10%

If you want every pack read over a fixed number of runs, use the n/t form instead: --read-data-subset 1/10 on the first run, 2/10 on the next, and so on up to 10/10. Each run reads a different tenth, and ten runs cover the whole repository.

Check it worked

The output ends with no errors were found. Anything else, such as Fatal: repository contains errors, needs attention before you trust the repository; the restic docs on check and repair explain the options.

Step 8 · Add an off-site copy over SFTP or S3

The local repository is gone if the disk or the whole server is. Keep a second repository somewhere else and copy snapshots to it. restic speaks to several kinds of storage with the same commands; only the repository URL changes.

For another server you can reach over SSH, the URL is sftp: followed by the user, host and path. Root runs restic, so root needs an SSH key that logs in to that host (see /tutorials/log-in-to-a-server-with-ssh-keys):

Text
sftp:backup@backup.example.com:/srv/restic/web01

For S3 or an S3-compatible service, the URL names the endpoint and bucket, and the access keys come from the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables. Keep those in a file under /root/restic created with mode 600, never on the command line:

Text
s3:https://s3.eu-west-1.amazonaws.com/example-backups/web01

Give the off-site repository its own password file, made the same way as the first (sudo install -m 600 /dev/null /root/restic/offsite-password, then fill it from /dev/urandom). Create the off-site repository with the same chunking settings as the local one, so copied snapshots deduplicate well, then copy across. Replace the SFTP URL with yours:

Bash
sudo restic-local -r sftp:backup@backup.example.com:/srv/restic/web01 --password-file /root/restic/offsite-password init --from-repo /var/backups/restic --from-password-file /root/restic/password --copy-chunker-params
sudo restic-local -r sftp:backup@backup.example.com:/srv/restic/web01 --password-file /root/restic/offsite-password copy --from-repo /var/backups/restic --from-password-file /root/restic/password

The -r and --password-file flags override the wrapper's variables for that one command. Running copy again later only sends snapshots and chunks the off-site repository does not have yet.

On a metered or prepaid connection, which is common for servers in offices across much of Africa, remember that the first upload is the full size of the data and later ones only send what changed. --limit-upload 1024 caps restic at about 1 MiB/s so a backup does not use up the line during working hours.

Check it worked

After the copy, snapshots against the off-site URL (sudo restic-local -r <your URL> --password-file /root/restic/offsite-password snapshots) lists one snapshot with the same time, host and paths as the local one. It has its own ID: copy prints snapshot <new ID> saved, copied from source snapshot <local ID> for each one, and the copy records the local ID as its original. check against it ends with no errors were found.

Next steps

The next tutorial, on scheduling and testing backups, needs only the restic binary in /usr/local/bin/restic; it creates its own repository, settings and wrapper. The demo repository in /var/backups/restic, the restic-local wrapper, /root/restic and /srv/demo-site can go. To remove them and keep restic, run sudo rm -rf /var/backups/restic /root/restic /srv/demo-site /usr/local/sbin/restic-local. If you are not continuing, add /usr/local/bin/restic to the end of that command to remove restic too. Either way this deletes the demo backups for good.

  • Run these backups on a schedule and prove the restores work every month: /tutorials/automate-and-test-restic-backups-with-a-systemd-timer.
  • Back up a database consistently before restic picks up the files: /tutorials/back-up-and-restore-postgresql-with-pg-dump.
  • Compare with plain-file snapshots you can browse without a tool: /tutorials/back-up-a-server-with-rsync-snapshots.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.