Posted inApps / Guides / Linux / Open Source

Restic: How to Create Encrypted Backups and Restore Your Files

A series of glass backup snapshots inside an archive, with one snapshot illuminated in orange.

Let’s say you’ve been working on a document for a few days. You accidentally save over it, or delete it altogether. You have a copy of the folder on another drive, but when was the last time you copied it? And did you copy it before or after you made the mistake?

This is where Restic comes in. It’s a free and open source command line backup tool for Linux, macOS, Windows and several other operating systems. It encrypts your backups, lets you store them locally or remotely and, more importantly, lets you go back in time and recover your files.

How does Restic work?

Instead of copying a folder full of files to another drive, Restic stores your data in what’s called a repository. Every time you run a backup, it creates a snapshot of the files you chose to back up (you can also tell it not to create a snapshot if nothing has changed).

Say you’ve got a folder containing 10 GB of documents. You back it up today and tomorrow you edit one document. You don’t need another 10 GB of storage for tomorrow’s backup. Restic splits file contents into chunks and stores each unique chunk only once. Both snapshots can reference the same chunks, while the newer one also references the new data. You can restore either snapshot without having to reconstruct a chain of incremental backups yourself.

Two snapshots share chunks A and B in an encrypted repository, while chunks C and D preserve the different file versions.

Restic also compresses data using Zstandard in its current repository format. How much space you save depends on what you’re backing up. Don’t expect much from a folder full of JPEGs or videos that are already compressed.

The repository is encrypted on your computer using AES-256 in counter mode, with Poly1305-AES authentication to detect tampering. Your password is used to unlock the repository’s encryption keys. You can add multiple passwords to the same repository, but if you lose all the usable passwords and keys, there’s no password reset service that can get your data back.

If you’re familiar with Cryptomator, the idea of encrypting files before they leave your computer should sound familiar. The difference is that Restic gives you a history of your data rather than just an encrypted place to store your current files.

Install Restic

On macOS you can install Restic using Homebrew:

brew install restic

On Debian, Ubuntu and most of their derivatives:

sudo apt install restic

On Arch Linux:

sudo pacman -S restic

On Fedora:

sudo dnf install restic

On Windows you can use Scoop:

scoop install restic

You can also get the standalone binary from GitHub. There are builds for both Intel and Apple Silicon Macs, 32 and 64 bit Windows, and Linux on x86, ARM and other architectures. You don’t need to install Go to run the prebuilt binary.

Check which version you’ve installed:

restic version

The examples below were verified with Restic 0.20.0 on Linux. If your package manager gives you an older release, check the installation instructions for the latest version and download verification. Version 0.20.0 fixes two security issues related to restoring files, so don’t use an older version for the sake of following this guide.

Create your first Restic backup

Before pointing Restic at your Documents folder or an entire drive, why not try it with a couple of text files? The following commands use a macOS/Linux shell. Use a fresh terminal without existing Restic configuration or credentials in its environment, and stay in the same directory throughout the example.

demo=$(mktemp -d "${TMPDIR:-/tmp}/restic-demo.XXXXXXXX") &&
cd "$demo" &&
mkdir documents &&
printf 'first version\n' > documents/notes.txt &&
printf 'keep this old file\n' > documents/old.txt &&
printf 'ignore me\n' > documents/ignore.tmp &&
cp documents/notes.txt original-notes.txt &&
cp documents/old.txt original-old.txt &&
pwd

The last command should show a newly created path containing restic-demo.. If any command failed or you’re not in that folder, stop. We’ve now got three files in documents and copies of two of them so we can check the restored data later.

First, create a repository:

restic -r ./repository init

The -r option tells Restic where the repository is. You’ll be asked for a password and then asked to confirm it. Remember it. For your actual backups, use a strong unique password and keep a recoverable copy of it somewhere other than the repository itself.

Now back up the documents folder:

restic -r ./repository backup documents --exclude '*.tmp'

Restic will skip files matching *.tmp. You can also keep a list of patterns in a separate text file and pass it to Restic using --exclude-file. At the end of the backup you’ll get a snapshot ID. You’ll need it in a minute.

Let’s simulate the two mistakes from the beginning of the article. We’ll change one of our dummy files and delete the other, then run another backup:

printf 'second version\n' > documents/notes.txt
rm documents/old.txt
restic -r ./repository backup documents --exclude '*.tmp'

List the snapshots in the repository:

restic -r ./repository snapshots

There should be two entries. Replace FIRST_ID and SECOND_ID in the following commands with the IDs of your first and second snapshots (the ones in the ID column).

To see which files were backed up in the first snapshot:

restic -r ./repository ls FIRST_ID

And to see what changed between the two backups:

restic -r ./repository diff FIRST_ID SECOND_ID

You’ll see that notes.txt was modified and old.txt was removed. But the first snapshot still has both of the original files.

Restore your files

Say all you need is the previous version of notes.txt. You don’t have to download or restore everything else in the snapshot.

restic -r ./repository restore FIRST_ID --include /documents/notes.txt --target recovered-file --verify

The path after --include is the path inside the snapshot, as displayed by restic ls, not the original path on your computer. The file will be restored to recovered-file/documents/notes.txt. The --verify option makes Restic read and verify the restored file contents.

Remember that copy of the original file we made before the first backup? Let’s compare it with the recovered one:

cmp original-notes.txt recovered-file/documents/notes.txt

If cmp returns without printing anything, the files match (assuming the command completed successfully). You’ve just recovered the earlier version without touching the current document.

Restic test output shows a modified file, a deleted file, successful verification and comparison of the restored file with its original.

What about the file we deleted? Let’s restore the entire first snapshot to another folder:

restic -r ./repository restore FIRST_ID --target restored-first --verify
cmp original-old.txt restored-first/documents/old.txt
cmp original-notes.txt restored-first/documents/notes.txt

To restore the most recent snapshot of our little repository:

restic -r ./repository restore latest --target restored-latest --verify
cmp documents/notes.txt restored-latest/documents/notes.txt

Open the folders. The first full restore contains the old versions of both files. The latest one contains the new version of notes.txt and neither old.txt nor ignore.tmp. That’s the difference between backing up a folder and being able to recover its contents as they were at a particular point in time.

NOTE: If you use one repository for several computers or different backup paths, latest by itself might not select the snapshot you want. Check the ID, host and paths first. And when recovering real files, restore them to a new, empty directory. Restic can overwrite existing files in the destination.

Check the repository

Restic has a command to check the structure and consistency of a repository:

restic -r ./repository check

It doesn’t read the contents of all stored data by default. To do that:

restic -r ./repository check --read-data

This can take a while with a large repository, especially if it’s stored remotely. It can also cost money if your storage provider charges for downloads or requests. The repository documentation explains how to check subsets of the data over several runs.

A successful check doesn’t mean you’ve backed up everything you intended to. Nor does it mean a database that was being written to while you backed it up is usable. Close applications that are actively modifying important files, or use proper database exports or filesystem snapshots where appropriate. On Windows, Restic also supports Volume Shadow Copy with --use-fs-snapshot (subject to the necessary permissions).

Restic preserves file metadata and archives symbolic links as links rather than following them to back up their targets. Keep that in mind when choosing backup paths. Not all metadata and filesystem features can be reproduced across operating systems. For example, restoring symbolic links and full security descriptors on Windows requires the appropriate privileges. If you’re moving a backup between platforms, test the things that matter to you, including permissions and links.

Where should you keep your backups?

The repository in the example above is on the same drive as the files we’re backing up. That was fine for an experiment, but it’s not much use if the drive fails.

You could create a repository on an external drive, a mounted network share or a remote machine via SFTP. Restic also has a REST backend (the separate rest-server project implements the server), and can connect directly to Amazon S3 and compatible object storage, Backblaze B2, Azure Blob Storage, Google Cloud Storage and OpenStack Swift.

If your storage provider isn’t supported directly, take a look at the rclone backend. You’ll need rclone installed and configured for your storage service. Restic is still responsible for encrypting the data and maintaining the snapshots; rclone handles access to the storage.

The 3-2-1 backup rule is a good starting point: three copies of your data, on two different types of media, with one copy offsite. Your working files on your computer’s SSD, an external hard drive with a Restic repository and a second repository at a remote location are one way to implement it. Disconnect the external drive between backups when practical. Three snapshots in the same repository are not three independent copies of your data.

Working files on a computer SSD are backed up to separate local and offsite repositories, using three copies and two media types.

Automate your backups and manage old snapshots

Restic doesn’t have a built-in backup scheduler. Use something like cron or systemd timers on Linux, launchd on macOS, or Task Scheduler on Windows. The scripting documentation explains how to supply the repository password without manually typing it on each run. Restrict access to password files, or use a password command backed by a credential store, rather than putting passwords into scripts or shell history.

Make sure you check the exit status and logs. A backup can create a snapshot even if some files could not be read (exit code 3). If a backup is interrupted, you can rerun it and Restic can reuse data already stored in the repository. Don’t assume an interrupted backup created a usable new snapshot. Check what actually got saved.

Eventually you’ll probably want to get rid of some old snapshots. But which ones? Let’s go back to our example repository and see what would happen if we kept just the most recent snapshot:

restic -r ./repository forget --keep-last 1 --dry-run

Nothing gets deleted with --dry-run. Restic shows which snapshots would be kept and which would be removed. By default, retention policies operate separately on groups of snapshots with the same host and paths. You could keep daily, weekly and monthly snapshots rather than just a fixed number of backups. See the retention documentation for the options.

Only if you want to delete the first snapshot from this disposable repository, run:

restic -r ./repository forget --keep-last 1

forget removes the snapshot. It doesn’t immediately remove all the underlying data that is no longer needed. That’s what prune is for. Preview the changes first:

restic -r ./repository prune --dry-run

To reclaim the unused storage in the demo repository:

restic -r ./repository prune

Once the old snapshot has been forgotten and its unreferenced data pruned, don’t count on recovering the old document from this repository. For real backups, make sure your other copy is usable and check your retention policy before removing anything.

Pruning may rewrite data and needs an exclusive repository lock. Plan for the extra storage, time and transfer costs, and don’t run maintenance while other jobs are accessing the repository. Restic uses locks to coordinate concurrent operations. Don’t use unlock to get around an active job. It’s intended for stale locks; the repair commands are for specific repository problems, not routine maintenance. Diagnose the problem and preserve a separate copy before attempting repair.

Finally, check the repository again and restore the remaining snapshot to a new folder:

restic -r ./repository check --read-data
restic -r ./repository restore latest --target restored-after-prune --verify
cmp documents/notes.txt restored-after-prune/documents/notes.txt

Can you still get your files back after maintenance?

Can Restic protect your backups from ransomware?

Encryption is great if someone gains access to the storage where your backup is kept. It’s not much help if that same person can simply delete the repository. If ransomware runs with your permissions, it may also be able to delete backups on connected drives or use your cloud storage credentials. If someone obtains the decrypted repository master keys, changing the repository password won’t revoke those keys. You need a new repository with new encryption keys.

Restic’s REST server supports an append only mode that allows a backup client to add data but restricts its ability to overwrite or delete stored backup data. You can keep the credentials that allow deletion on another, trusted machine and perform maintenance from there. It isn’t a complete solution by itself. A compromised client can still create misleading snapshots or fill up the storage, and a poorly chosen retention policy can remove older, good backups. Restic recommends --keep-within for this type of repository, but even that doesn’t eliminate the risk. Read the security considerations for append only repositories before automating retention. Storage provider immutability or object locking is another option, but check whether it works with your repository and its maintenance operations.

What if you don’t want to use the command line?

If you’re on a Mac and just want an easy way to restore your files or migrate to another Mac, Time Machine might be all you need. If you’re looking for an alternative with a graphical interface and support for remote storage, take a look at Kopia or Duplicati. BorgBackup is another tool that offers encrypted, deduplicated backups, particularly interesting if you run your own backup server over SSH. And if you like the way Restic works but don’t want to manage it from the terminal, there’s Backrest, a separate web interface and backup scheduler built around Restic.

Tools like rsync and rclone are great for copying or syncing data. But simply keeping two folders in sync won’t necessarily help you recover a file you deleted a week ago. You need to keep a history of your files, not just another copy of their current state.

Whatever you end up using, try restoring your data before you need to. Preferably on another computer, with nothing but the backup, the credentials and the software required to read it. That’s when you’ll find out if your backup strategy actually works.

Support FSMdotCOM on Liberapay