Anime-Sanctuary

Encrypted backups with restic

Failed disk, file deleted by mistake, stolen server: an encrypted restic backup protects you from all three. Here's how to create it, send it to a second server, prove it restores and run it on its own.

Course
Tutorial 31 of 35
Time
35 min to read
Tested with
Debian 13.7 (trixie), restic 0.18.0
What you will be able to do
  • Create an encrypted restic repository and store a first backup of your documents in it.
  • Restore a whole folder, or a single file in an older version.
  • Send the backup to a second server over SFTP.
  • Keep the right snapshots, check the repository and run it all every night with a systemd timer.

Before you start: Your first backup with rsync, Users, sudo and file permissions, Secure SSH: turn off passwords, Your first website with Apache.

Contents
  1. What restic adds over rsync
  2. 1. Install restic and create the repository
  3. 2. Make the first backup
  4. 3. List snapshots and restore
  5. 4. Send the backup to a second server
  6. 5. Write the backup script
  7. 6. Run the script every night
  8. Where you stand on the 3-2-1 rule
  9. If it fails
  10. Frequently asked questions
  11. What's next
  12. Sources

How do you make an encrypted backup with restic, and get it back intact the day you need it? You'll create a repository, store your documents in it, send it to a second server, then let a timer take care of it. You already know how to copy folders with rsync, as in Your first backup with rsync. The second server is another machine you can reach over SSH (the secure remote connection), at 198.51.100.20 in our examples. You log in to it with a key, as in Secure SSH: turn off passwords. The commands with sudo rely on Users, sudo and file permissions.

What restic adds over rsync

restic stores your files in an encrypted repository, and never stores the same data twice. rsync makes a readable copy of your folders, which is a good first step. Three words are enough to see the difference:

  • A repository is the folder restic manages: everything in it is cut into encrypted chunks.
  • A snapshot is a photo of your files at one precise moment. Each backup adds one.
  • Deduplication means storing an identical chunk only once, even if it shows up in ten snapshots.

The table compares the two tools on what matters for a backup.

Criterionrsyncrestic
What the copy containsFiles, readable as they areEncrypted chunks, unreadable without the password
HistoryOne version only: the latestOne snapshot per backup, and you can go back to any of them
DuplicatesEach copy takes whole files againAn identical chunk is stored only once
On the remote machinersync must be installed thereSSH is enough

Encryption matters most when the copy lives with someone else. Without the password, nobody can read your files, not even the administrator of the second server. Here's the path your files will follow.

The path of a restic backup The documents folder on mon-serveur goes through restic, which cuts it into chunks and encrypts them. The result goes to a repository on the second server, and to a local repository used for practice. documents mon-serveur restic cuts and encrypts local repository depot-restic second server 198.51.100.20
Text version: the documents folder on mon-serveur goes through restic, which cuts it into chunks and encrypts them. In this tutorial, the exercises write to the local repository first. The nightly backup writes only to the second server.

The path is drawn. Let's start at the beginning: install restic and prepare the local repository.

1Install restic and create the repository

Log in as alice to mon-serveur, the name we give your server (203.0.113.10), as in Your first SSH connection. All the commands below are typed there, unless noted otherwise.

On your computer
ssh alice@203.0.113.10

Install restic, then check its version.

On mon-serveur
sudo apt install restic
On mon-serveur
restic version
Expected output
restic 0.18.0 compiled with go1.24.4 on linux/amd64

Only the start matters: the version number. The repository is protected by a password, which we'll call the "repository password". Let's make a long, random one and keep it in a file only you can read. The ~ stands for your home folder. The block below creates the ~/.config/restic folder (mkdir -p), builds the password with one line that chains four pieces, then runs chmod 600 to reserve the file for you alone:

  • head -c 24 /dev/urandom reads 24 random bytes from /dev/urandom, the system's random number source.
  • base64 writes them as 32 readable characters.
  • The pipe | passes the output of the left command to the one on the right.
  • > stores the result in the file mdp.
On mon-serveur
mkdir -p ~/.config/restic
head -c 24 /dev/urandom | base64 > ~/.config/restic/mdp
chmod 600 ~/.config/restic/mdp

No password manager yet? Paper works fine. Vaultwarden sets one up, but install it on a machine other than this server.

Two environment variables save you from repeating the repository and the password in every command. A variable is a label stuck on a value: here, export sticks the label where restic can read it, until you close the terminal. Then we create the repository.

On mon-serveur
export RESTIC_REPOSITORY=~/depot-restic
export RESTIC_PASSWORD_FILE=~/.config/restic/mdp
restic init
Expected output
created restic repository 5ecefe8937 at /home/alice/depot-restic

Please note that knowledge of your password is required to access
the repository. Losing your password means that your data is
irrecoverably lost.

The repository ID, here 5ecefe8937, will be different on your machine. The repository exists, but it's empty: it needs files to protect.

2Make the first backup

The restic backup command reads a folder and stores a snapshot of it in the repository. First, let's build a practice documents folder with three small files, one of them in a subfolder. The > writes text into a file, replacing it if it exists.

On mon-serveur
mkdir -p documents/photos
echo "liste de courses" > documents/notes.txt
echo "rapport de mars" > documents/rapport.txt
echo "photo de chat" > documents/photos/chat.jpg
restic backup documents
Expected output
no parent snapshot found, will read all files

Files:           3 new,     0 changed,     0 unmodified
Dirs:            2 new,     0 changed,     0 unmodified
Added to the repository: 1.811 KiB (1.291 KiB stored)

processed 3 files, 47 B in 0:01
snapshot bc130436 saved

Three new files, and a snapshot saved under the ID bc130436. Yours will be different, and so will the duration. The first line just says there is no earlier snapshot to compare with. The "Added" line counts what restic writes into the repository: your data plus the description of the folders, hence a number bigger than the 47 bytes on the "processed" line. One KiB is 1024 bytes, and "stored" gives the size after compression.

Now let's change one file and add another, to watch restic store only what changed. The >> adds a line at the end of a file, where > overwrites it.

On mon-serveur
echo "du pain" >> documents/notes.txt
echo "photo de chien" > documents/photos/chien.jpg
restic backup documents
Expected output
using parent snapshot bc130436

Files:           1 new,     1 changed,     2 unmodified
Dirs:            0 new,     2 changed,     0 unmodified
Added to the repository: 2.150 KiB (1.292 KiB stored)

processed 4 files, 70 B in 0:00
snapshot 8c501314 saved

This time, restic compares with the previous snapshot, its "parent". Only the changed file and the new file are written. You now have two snapshots: let's see how to find them.

Is the repository really unreadable?

grep searches for text in all the files of the repository. After each command, the terminal remembers a small number, its exit code: 0 for "succeeded", anything else for "failed". echo $? prints it. The 1 means "nothing found": the content really is encrypted.

On mon-serveur
grep -r "liste de courses" ~/depot-restic; echo $?
Expected output
1

3List snapshots and restore

A backup you never restored is a parachute you never packed: you only find out by jumping. So we restore right away. First, restic snapshots lists the snapshots in the repository.

On mon-serveur
restic snapshots
Expected output
ID        Time                 Host         Tags        Paths                  Size
-----------------------------------------------------------------------------------
bc130436  2026-11-06 09:37:03  mon-serveur              /home/alice/documents  47 B
8c501314  2026-11-06 09:37:04  mon-serveur              /home/alice/documents  70 B
-----------------------------------------------------------------------------------
2 snapshots

Each line gives the ID, the date, the machine and the folder that was backed up. Let's restore the most recent one into a new folder, restauration, then compare it with the original using diff -r, which says nothing when two folders are identical. In the snapshot, the folder keeps its name, so it comes back as restauration/documents.

On mon-serveur
restic restore latest --target ~/restauration
diff -r documents restauration/documents; echo $?
Expected output
restoring snapshot 8c501314 of [/home/alice/documents] at 2026-11-06 09:37:04.264566043 +0000 UTC by alice@mon-serveur to /home/alice/restauration
Summary: Restored 6 files/dirs (70 B) in 0:00
0

latest means the most recent snapshot. The final 0, the exit code from earlier, confirms there is no difference: the restore is faithful. Even better, restic can travel back in time. restic dump prints a file from a snapshot on screen, without restoring anything. Let's look at notes.txt as it was in the first snapshot, before the bread was added. Replace bc130436 with the first ID in your list.

On mon-serveur
restic dump bc130436 /documents/notes.txt
Expected output
liste de courses

The old notes.txt doesn't contain the "du pain" line: it really is the earlier version. But all of this still sits on the same disk as the original. Let's send the repository elsewhere.

4Send the backup to a second server

So far everything stayed on mon-serveur, the disk that holds the original. This step sends a copy to the second server, another machine at 198.51.100.20. All the commands are still typed on mon-serveur.

restic writes to a remote repository over SFTP, file transfer through SSH. So the second server has nothing to install: it only needs SSH.

The nightly backup will connect on its own, with nobody there to type a password. So it needs an SSH key without a passphrase, meaning no password on the key. Don't mix it up with the repository password.

Create this key (-N "" leaves the passphrase empty), then copy it to the second server with ssh-copy-id. The option -t ed25519 picks the key type: a recent, short and safe one. If the file ~/.ssh/id_ed25519 already exists, skip this first command.

On mon-serveur
ssh-keygen -t ed25519 -N "" -f ~/.ssh/id_ed25519
On mon-serveur
ssh-copy-id alice@198.51.100.20
Expected output
Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'alice@198.51.100.20'"
and check to make sure that only the key(s) you wanted were added.

The second server should be in a different place from yours, or a single outage or fire takes everything down. The alice account must exist on the second server, with a password for this first login. The command asks for it once, then prints the lines above. Next, log in once by hand. SSH shows the server's fingerprint, its unique signature, so you don't end up talking to an impostor. Answer yes.

On mon-serveur
ssh alice@198.51.100.20 hostname
Expected output
serveur-secours

SSH remembers your answer. Without it, the nightly backup would fail on that question. The command prints the name of the machine, here serveur-secours. Yours will be different.

Now let's create the remote repository, with the same name, depot-restic, as the local one. An SFTP address is written sftp:user@address:folder, and the folder starts from alice's home folder on the second server. The -r option ("repository") points to that repository instead of the one in the variables.

On mon-serveur
restic -r sftp:alice@198.51.100.20:depot-restic init
Expected output
created restic repository d307635b48 at sftp:alice@198.51.100.20:depot-restic

Please note that knowledge of your password is required to access
the repository. Losing your password means that your data is
irrecoverably lost.

The repository password is the same, so the file is the same. Let's send a backup there, then list the snapshots of the remote repository.

On mon-serveur
restic -r sftp:alice@198.51.100.20:depot-restic backup documents
restic -r sftp:alice@198.51.100.20:depot-restic snapshots
Expected output
no parent snapshot found, will read all files

Files:           4 new,     0 changed,     0 unmodified
Dirs:            2 new,     0 changed,     0 unmodified
Added to the repository: 2.062 KiB (1.484 KiB stored)

processed 4 files, 70 B in 0:00
snapshot d7ebf7fb saved
ID        Time                 Host         Tags        Paths                  Size
-----------------------------------------------------------------------------------
d7ebf7fb  2026-11-06 09:41:12  mon-serveur              /home/alice/documents  70 B
-----------------------------------------------------------------------------------
1 snapshots

The copy now lives on another machine, unreadable to anyone without your password. Every backup makes the repositories grow, so you need to decide what to keep.

5Write the backup script

For the backup to start on its own, you need a script: a small file that chains three restic commands. It targets the second server's repository. The local repository is only for practice, and the script never touches it.

  • backup stores a new snapshot, as in step 2.
  • forget --prune removes the snapshots you no longer need, then frees the space they took.
  • check verifies that the repository is consistent and nothing is broken.

The forget rule comes down to three options: --keep-daily 7, --keep-weekly 4 and --keep-monthly 6. They keep one snapshot per day for seven days, one per week for four weeks and one per month for six months. An older snapshot disappears for good, unless it is the last one of a week or month you keep. Your current files are never touched.

Try forget and check by hand

These two trials use the local repository from step 1, thanks to the export variables. To see what forget would remove without deleting anything, add --dry-run. Your two snapshots are recent, so the simulation keeps them all. With one backup per night, the oldest would go. The --compact option shortens the output.

On mon-serveur
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --compact --dry-run
Expected output
Applying Policy: keep 7 daily, 4 weekly, 6 monthly snapshots
keep 2 snapshots:
ID        Time                 Host         Tags    Size
--------------------------------------------------------
bc130436  2026-11-06 09:37:03  mon-serveur          47 B
8c501314  2026-11-06 09:37:04  mon-serveur          70 B
--------------------------------------------------------
2 snapshots

For the repository itself, restic check rereads its structure. Only the last line matters: no errors were found. The number after restic-check-cache changes every time.

On mon-serveur
restic check
Expected output
using temporary cache in /tmp/restic-check-cache-546839416
create exclusive lock for repository
load indexes
check all packs
check snapshots, trees and blobs
[0:00] 100.00%  2 / 2 snapshots
no errors were found

To reread part of the data too, add --read-data-subset=10%. It is slower, but useful against a disk that silently damages files.

We write the script from the terminal with the trick from Your first website with Apache, a heredoc: all the text typed after <<'EOF', up to the line EOF, is handed to the command as content. sudo tee file writes it into the file. The > /dev/null stops that text from being printed a second time on screen.

Copy the block, replacing 198.51.100.20 with the address of your second server. If your account or folder aren't called alice and documents, change those too.

On mon-serveur
sudo tee /usr/local/bin/sauvegarde-restic.sh > /dev/null <<'EOF'
#!/bin/sh
set -eu

export RESTIC_REPOSITORY=sftp:alice@198.51.100.20:depot-restic
export RESTIC_PASSWORD_FILE=/home/alice/.config/restic/mdp

cd /home/alice
restic backup --quiet documents
restic forget --quiet --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
restic check --quiet
EOF
sudo chmod 755 /usr/local/bin/sauvegarde-restic.sh

If you download it instead of copying the block, it lands on your computer. Send it to the server first with scp, file copy through SSH, then put it in place with install. Then open it with sudo nano /usr/local/bin/sauvegarde-restic.sh to replace the address, the account and the folder.

On your computer
scp sauvegarde-restic.sh alice@203.0.113.10:
On mon-serveur
sudo install -m 755 sauvegarde-restic.sh /usr/local/bin/
  • set -eu stops the script at the first error (-e) or at a variable that was never set (-u), so that a failure shows.
  • --quiet prints only errors, so the journal stays readable.
  • cd /home/alice moves into your home folder, so that documents really means /home/alice/documents.

The script is ready. Next, let's get it running every night.

6Run the script every night

A systemd timer starts a task at a set time. systemd is the program that starts and watches Debian's services, the background programs. A timer keeps its traces in the journal (see Read your logs with journalctl) and catches up on a missed backup if the machine was off. You need two files: a service that runs the script, and a timer that triggers the service.

The service runs the script as alice. Its settings:

  • Type=oneshot says the service does its job, then stops.
  • User=alice runs it as alice, who owns the SSH key and the password file.
  • Wants= and After=network-online.target ask systemd to bring the network up and wait until it is ready before running the script.
On mon-serveur
sudo tee /etc/systemd/system/restic-sauvegarde.service > /dev/null <<'EOF'
[Unit]
Description=Sauvegarde restic vers le second serveur
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
User=alice
ExecStart=/usr/local/bin/sauvegarde-restic.sh
EOF

The timer sets the time. Its three settings:

  • OnCalendar=*-*-* 03:00:00 triggers the service every day at 3:00 AM, server time (UTC in this tutorial's outputs). The stars read as "every": every year, every month, every day.
  • RandomizedDelaySec=15min delays the start by a random amount, up to 15 minutes.
  • Persistent=true runs the backup at the next boot if the time was missed.
On mon-serveur
sudo tee /etc/systemd/system/restic-sauvegarde.timer > /dev/null <<'EOF'
[Unit]
Description=Sauvegarde restic chaque nuit

[Timer]
OnCalendar=*-*-* 03:00:00
RandomizedDelaySec=15min
Persistent=true

[Install]
WantedBy=timers.target
EOF

Tell systemd to reread its files, then enable the timer. The enable command keeps it active after a reboot, --now starts it right away and --quiet silences the confirmation message. The next start is then shown.

On mon-serveur
sudo systemctl daemon-reload
sudo systemctl --quiet enable --now restic-sauvegarde.timer
systemctl list-timers restic-sauvegarde.timer
Expected output
NEXT                        LEFT LAST PASSED UNIT                    ACTIVATES
Fri 2026-11-06 03:13:34 UTC  19h -         - restic-sauvegarde.timer restic-sauvegarde.service

1 timers listed.
Pass --all to see loaded but inactive timers, too.

The exact time in the NEXT column will vary on your machine, because of the random delay. Rather than wait until 3 AM, trigger a first backup by hand to see that it works.

On mon-serveur
sudo systemctl start restic-sauvegarde.service
systemctl show -p Result -p ExecMainStatus restic-sauvegarde.service
Expected output
Result=success
ExecMainStatus=0

systemctl show prints properties of the service, and -p picks which ones. Result is the outcome of the last run, and ExecMainStatus is the script's exit code. success and a status of 0 mean the script ran to the end. The timer will run the backup tonight, but how many copies do you really have?

Where you stand on the 3-2-1 rule

The 3-2-1 rule asks for three copies of your data, on two different media, with one away from home (a reminder from Your first backup with rsync). Let's count, honestly:

  • The original, on the disk of mon-serveur, is the first copy.
  • The second server's repository, fed every night, is the second copy, off the machine.
  • The local repository from step 1 is for practice: the timer doesn't fill it, and it shares the original's disk.

So you have two copies, not three. For the third, you need another medium, such as an external disk, with a third repository created as in step 1. Above all, keep the copy of the password away from the server: it matters as much as the data. Now, what can go wrong.

If it fails

Four messages come back most often, and each has its own cause.

  • Fatal: Please specify repository location (-r or --repository-file): restic doesn't know which repository to use. The export variables from step 1 are forgotten in every new terminal, so type them again.
  • Fatal: wrong password or no key found: the password it read isn't the repository's. Check the file that RESTIC_PASSWORD_FILE points to.
  • Host key verification failed: this line comes before the failure message in the service's journal. SSH doesn't know the second server's fingerprint (its signature) yet, so log in once by hand, as in step 4.
  • ssh: connect to host 198.51.100.20 port 22: Connection refused: the second server is off or SSH is stopped. The service is then marked as failed, which systemctl is-failed restic-sauvegarde.service shows with the word failed.

A nightly backup that fails won't warn you. Read the journal now and then with journalctl -u restic-sauvegarde.service. Also do a trial restore every few months, as in step 3, but from the second server by adding -r sftp:alice@198.51.100.20:depot-restic.

Frequently asked questions

How do I make an encrypted backup on Debian?

Install restic, create a repository protected by a password, then run restic backup on your folders. Each backup adds an encrypted snapshot to the repository.

What happens if I lose my restic repository password?

You lose all your backups: restic has no back door. Copy the password somewhere other than the server as soon as you create the repository.

How do I check that a restic backup really restores?

Restore the latest snapshot into a new folder with restic restore, then compare it with the original using diff -r. An exit code of 0 means there is no difference.

What's next

Your documents are copied, encrypted and checked on another machine. But the disk that holds them can still fail. A backup helps you recover after a failure. The next tutorial, Software RAID and SMART checks, shows you how to see a failure coming and make it less serious.