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
- What restic adds over rsync
- 1. Install restic and create the repository
- 2. Make the first backup
- 3. List snapshots and restore
- 4. Send the backup to a second server
- 5. Write the backup script
- 6. Run the script every night
- Where you stand on the 3-2-1 rule
- If it fails
- Frequently asked questions
- What's next
- 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.
| Criterion | rsync | restic |
|---|---|---|
| What the copy contains | Files, readable as they are | Encrypted chunks, unreadable without the password |
| History | One version only: the latest | One snapshot per backup, and you can go back to any of them |
| Duplicates | Each copy takes whole files again | An identical chunk is stored only once |
| On the remote machine | rsync must be installed there | SSH 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.
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.
ssh alice@203.0.113.10Install restic, then check its version.
sudo apt install resticrestic versionrestic 0.18.0 compiled with go1.24.4 on linux/amd64Only 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/urandomreads 24 random bytes from/dev/urandom, the system's random number source.base64writes 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 filemdp.
mkdir -p ~/.config/restic
head -c 24 /dev/urandom | base64 > ~/.config/restic/mdp
chmod 600 ~/.config/restic/mdpNo 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.
export RESTIC_REPOSITORY=~/depot-restic
export RESTIC_PASSWORD_FILE=~/.config/restic/mdp
restic initcreated 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.
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 documentsno 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 savedThree 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.
echo "du pain" >> documents/notes.txt
echo "photo de chien" > documents/photos/chien.jpg
restic backup documentsusing 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 savedThis 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.
grep -r "liste de courses" ~/depot-restic; echo $?13List 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.
restic snapshotsID 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 snapshotsEach 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.
restic restore latest --target ~/restauration
diff -r documents restauration/documents; echo $?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
0latest 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.
restic dump bc130436 /documents/notes.txtliste de coursesThe 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.
ssh-keygen -t ed25519 -N "" -f ~/.ssh/id_ed25519ssh-copy-id alice@198.51.100.20Number 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.
ssh alice@198.51.100.20 hostnameserveur-secoursSSH 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.
restic -r sftp:alice@198.51.100.20:depot-restic initcreated 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.
restic -r sftp:alice@198.51.100.20:depot-restic backup documents
restic -r sftp:alice@198.51.100.20:depot-restic snapshotsno 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 snapshotsThe 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.
backupstores a new snapshot, as in step 2.forget --pruneremoves the snapshots you no longer need, then frees the space they took.checkverifies 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.
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --compact --dry-runApplying 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.
restic checkusing 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 foundTo 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.
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.shIf 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.
scp sauvegarde-restic.sh alice@203.0.113.10:sudo install -m 755 sauvegarde-restic.sh /usr/local/bin/set -eustops the script at the first error (-e) or at a variable that was never set (-u), so that a failure shows.--quietprints only errors, so the journal stays readable.cd /home/alicemoves into your home folder, so thatdocumentsreally 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=oneshotsays the service does its job, then stops.User=aliceruns it as alice, who owns the SSH key and the password file.Wants=andAfter=network-online.targetask systemd to bring the network up and wait until it is ready before running the script.
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
EOFThe timer sets the time. Its three settings:
OnCalendar=*-*-* 03:00:00triggers 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=15mindelays the start by a random amount, up to 15 minutes.Persistent=trueruns the backup at the next boot if the time was missed.
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
EOFTell 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.
sudo systemctl daemon-reload
sudo systemctl --quiet enable --now restic-sauvegarde.timer
systemctl list-timers restic-sauvegarde.timerNEXT 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.
sudo systemctl start restic-sauvegarde.service
systemctl show -p Result -p ExecMainStatus restic-sauvegarde.serviceResult=success
ExecMainStatus=0systemctl 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. Theexportvariables 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 thatRESTIC_PASSWORD_FILEpoints 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, whichsystemctl is-failed restic-sauvegarde.serviceshows with the wordfailed.
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.