Restic: How to Efficiently Backup & Restore Data on Linux

restic logo on a green background

Restic is an open-source, cross-platformfast, efficient and secure backup program. It supports multiple backends (e.g. local storage, SFTP, Amazon S3, Google Cloud Storage, Backblaze B2, Microsoft Azure Blob Storage, and more) and encrypts data with AES-256 by default.

The way Restic backs up data makes it very space-efficient and allows for fast restores.

The way this works is that it saves snapshots of your data and only saves the differences (deltas) when you create new snapshots.

So, for example, if you have a 100GB file and you change 1MB of it, only 1MB will be backed up the next time you create a snapshot, instead of the entire 100GB file.

Introduction

Restic is a command line-based program that uses an index and a checksum method to create backups instead of the traditional snapshot methods utilized by most backup programs.

Restic uses a chunking method by chunking data into several smaller bits and then hashing them to give a unique id to each chunk and then storing all these chunks in the database. It utilizes an indexing method to reference these files which are useful when rebuilding these files i.e. restoring files from backup.

We first define a backup repository for Restic and then every time we backup data using Restic, all the information related to the backup is stored in that repository including the backup snapshots. Unlike other backup programs, Restic allows users to upload backups from multiple hosts into the same repository.

Restic is a backup by default system which means that all backups created using Restic are encrypted client-side. Restic encrypts data with the AES-256 in counter mode and then authenticates it using the Poly1305-AES cryptographic message authentication code. This way Restic guarantees confidentiality and data integrity by utilizing cryptography.

Restic allows to backup files and directories in a local disk, remote system, or cloud storage. Restic supports popular cloud storage backends such as

The program itself was written using the Go programming language and the source code is available on GitHub. It is a cross-platform application and supports GNU/Linux, macOS, FreeBSD, OpenBSD, and Microsoft Windows.

Now that we have covered what Restic is, let’s move forward and see how we can create backups with Restic and then how to restore data from a backup using Restic.

Backup Data using Restic

To backup data using Restic, we need to create a backup repository where all our backups are going to be stored. But first, we need to install Restic.

Install Restic

Restic is available in repositories for main Linux distributions. We can install it with one-line code execution using the package manager. Execute the suitable command depending on your Linux distribution to install Restic.

For Debian, Ubuntu, Linux Mint, Pop_OS

sudo apt-get install restic

For Fedora

sudo dnf install restic

For RHEL, CentOS, AlmaLinux and Rocky Linux

sudo dnf copr enable copart/restic

For Arch Linux, EndeavourOS, Manjaro Linux

sudo pacman -S restic

Make sure that the [Community] repository is enabled in Arch Linux before proceeding to install Restic using the Pacman.

Now that we have installed Restic application, let’s take a look at how we can backup and restore data in a local directory.

Create Repository for Backups

You can either use an already created repository or create a new one. For this tutorial, we are going to create a new repository named mybackup.

Execute the following command to create a repository named mybackup in the home directory.

restic init --repo ~/mybackup

The above command will lead you to a command prompt asking for a new password for the repository. Remember this password in order to access backup data from the repository otherwise, you will not be able to access it.

word image 11257 1

Note: There is no role-set in Restic, meaning anyone who has the password to a repository will have administrative access to backups present in the repository.

Creating Backups

Now that we have created a local repository for storing backups, we can use Restic to create backups in this repository. The base syntax of a Restic backup command looks like the following.

restic -r path/to/repository backup path/to/directory

Let’s see what each parameter in the above command stands for to understand it better.

  • restic: the restic application command
  • -r: flag to indicate repository
  • path/to/repository: repository path where we want to store backups
  • backup: restic sub-command to backup files
  • path/to/directory: the directory to backup.

We are going to back up the Tutor directory using Restic. Execute the following command to backup the directory.

restic -r ~/mybackup backup ~/Tutor

We will be prompted to provide the password for the repository named mybackup. We will get the following output when the above command is executed.

word image 11257 2

As shown in the above figure, we have created a backup of the directory Tutor. The Restic command has created a backup snapshot with a unique id name d7423006.

Every time we create a backup, Restic creates a snapshot with a unique snapshot id. Now let’s say we made some changes in our directory and want to backup those changes.

To take an example, we create a txt file named test.txt and then add this file to the Tutor directory. Let’s execute the same backup command again after altering the contents of the directory.

restic -r ~/mybackup backup ~/Tutor

The picture below shows the expected output from the execution of the above command.

word image 11257 3

As it is clear from the snapshot created, we have 1 new file that was added in the backup and 1 unmodified file that was backed up in the first backup. A new snapshot 6564c790 has been created and if we look at the time it took for this backup it shows 0:00. This is because Restic does incremental backups which means each backup is faster than the previous one.

This is how we backup data using Restic. Now let’s move forward and learn how to restore data using Restic.

Restore Data using Restic

Restic stores backups in the form of snapshots while assigning each snapshot a unique id. We can use these snapshots to restore data. But first, let’s see how to check all the snapshots that are present in the backup repository.

List Available Snapshots

Execute the following command to list all the available snapshots in the local repository.

restic -r ~/mybackup snapshots

word image 11257 4

As we can see, the output shows all the available snapshots that are present in our local backup directory. The command will return information about snapshots including their IDs, time of creation, host, tags, and complete paths to the snapshots.

Restore Data

Restoring data using Restic is fairly easy. Since Restic creates a unique snapshot every time we use it to backup data, we can use these unique snapshots to restore data. The only thing to be careful about is that you should know which snapshot has your desired data.

Restore Entire Snapshot

First, list all our available snapshots using the list snapshots command.

restic -r ~/mybackup snapshots

word image 11257 5

The base syntax of a Restic command to restore data from a snapshot is as follows

restic -r path/to/repository restore latest|snapshot_id --target path/to/target

Let’s say we want to restore data from the snapshot d7423006 into the local directory Tutor. The above general command to do so will become like this:

restic -r ~/mybackup restore d7423006 --target ~/Tutor

We will get the following output.

word image 11257 6

The picture above shows that the snapshot d7423006 was successfully opened and data from this snapshot has been restored to the /Tutor directory.

Restore Single File from a Snapshot

Like restoring a complete snapshot, we also have the option to restore a specific file from a snapshot by using tags to define the file types to be restored or by specifying the file name.

For example, we want to restore the .txt file from the snapshot ID of d7423006. We can specify the .txt tag at the end of the command we used above. The command will become as written below.

restic -r ~/mybackup restore d7423006 --target ~/Tutor file.txt

The above command will restore .txt file from the snapshot with the id d7423006.

Useful Restic Functions

Now that we have seen how to create backups and restore data from backups using Restic, let’s take a look at some useful Restic functions that can come in handy in certain scenarios while backing up data and restoring it.

Creating Multiple Keys to access Backup Repository

We can create multiple keys to access the same repository. First list the available keys by executing the following command.

restic -r mybackup/ key list

The output of the above command is shown in the picture below.

word image 11257 7

We can see from the output that there is one key present that we created when we created this backup repository.

We can add more keys by using the key add command. Execute the following command to add keys.

restic -r mybackup/ key add

The prompt will first ask you for the password for the directory and then give you the option to assign a password for the new key. Refer to the image below.

word image 11257 8

Now if we list keys using the key list command:

restic -r mybackup/ key list

word image 11257 9

From the output we can see that now we have two keys.

Conclusion

In this tutorial we learned how to create a repository to store backups, how to backup data using Restic, and how to restore data using snapshots. We also learned about additional functions that come with Restic such as creating additional keys.


Resources & Acknowledgements

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
DachshundMan
DachshundMan
1 month ago

I think this needs an explanation on how to restore the home directory,for example, with the latest version of each file no matter which repository it is in.

You May Also Like