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:
sudo apt-get update
sudo apt-get install -y curl bzip2 ca-certificatesDownload the binary for your CPU (amd64 or arm64, which dpkg reports) and the checksum list into a throwaway folder:
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 SHA256SUMSThe 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:
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 versionA 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:
sudo restic self-updateCheck 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:
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/nullPrint it once to copy it into your password manager, then clear the terminal:
sudo cat /root/restic/passwordrestic 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 ...:
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-localThe repository lives in /var/backups/restic, outside the folders you back up, so a backup never tries to include itself. Create it:
sudo restic-local initCheck it worked
init prints created restic repository followed by an ID and the path /var/backups/restic. List the repository folder:
sudo ls /var/backups/resticYou 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.
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/nullWrite 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:
sudo tee /root/restic/excludes > /dev/null <<'EOF'
# Caches are rebuilt by the application.
/srv/*/cache
# Partial uploads and editor leftovers.
*.tmp
*.swp
EOFRun the first backup:
sudo restic-local backup /etc /srv --exclude-file /root/restic/excludesrestic 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:
sudo restic-local ls latest /srv/demo-siteYou 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:
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/excludesThis 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:
sudo restic-local snapshotsThe 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:
sudo restic-local diff 1a2b3c4d 5e6f7a8bCheck it worked
The diff output has one line per changed path, something like:
M /srv/demo-site/index.html
+ /srv/demo-site/about.html
- /srv/demo-site/notes.txtM 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:
sudo restic-local restore 1a2b3c4d --target /var/tmp/restic-one --include /srv/demo-site/notes.txtrestic 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:
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.txtTo 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:
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:
sudo rm -rf /var/tmp/restic-one /var/tmp/restic-restoreStep 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:
sudo restic-local forget --keep-daily 7 --keep-weekly 4 --dry-runThe 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:
sudo restic-local forget --keep-last 1 --dry-run
sudo restic-local forget --keep-last 1 --pruneThe preview lists one snapshot to keep and, under a line like remove 1 snapshots, the older one.
Check it worked
sudo restic-local snapshotsOne 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:
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):
sftp:backup@backup.example.com:/srv/restic/web01For 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:
s3:https://s3.eu-west-1.amazonaws.com/example-backups/web01Give 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:
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/passwordThe -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.
Related tutorials
- Write safer Bash scripts with strict mode and trapsYou will build a small Bash release script that stops on the first error, says which line failed, survives file names with spaces and always removes its temporary files.
- Write least-privilege sudo rules with visudoYou will replace blanket sudo access with checked rule files in /etc/sudoers.d that let two operators restart one service and edit one file, give an automation account a password-free command list, and audit what every account can run.
- Upgrade a server from Ubuntu 24.04 LTS to 26.04 LTSYou will take a backed-up Ubuntu 24.04 server through do-release-upgrade to 26.04 LTS over SSH with a fallback login, then confirm services, repositories and language runtimes work on the new release.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials