How to Use Restic to Backup and Restore Data in Linux

Channel: Linux
Abstract: we want to restore data from snapshot d7423006. The command to do so looks like the below. restic -r ~/mybackup restore d7423006 --target ~/Tutor The

Restic is an open-source, secure, and cross-platform backup program. Using Restic we can store multiple versions of files and directories in an encrypted repository. Restic can be used to back up data to an external device or to cloud storage.

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 does incremental backups which makes it easier and faster compared to some other backup programs. What this means is that it stores a base backup image and then for each subsequent backup, it stores the difference between that base image and the source machine. This leads to increased backup speed as only the modified data is backed up. It also consumes less backup space.

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

  • Local Storage
  • AWS S3
  • Azure
  • BackBlaze B2
  • Google Cloud Storage
  • OpenStack Swift
  • HTTP REST Server
  • sftp server (via SSH)

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.

Before we can use Restic to backup and restore data, we need to install Restic on our system.

Table of Contents
  1. Install Restic
  2. Create Local Repository
  3. Backup Data Into Local Repository Using Restic
  4. List Snapshots
  5. Check the difference between two Snapshots
  6. Exclude Files From Backup
  7. Restore Data via Restic
  8. Mount Repository to view Backup Data
  9. Conclusion
Install Restic

Restic application is available in repositories of main Linux distributions. So we can install it with one-line code execution using the package manager. Read below and 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. Follow the steps below to do so.

Create Local Repository

First, we have to create a local repository to store our backups. For this tutorial, we are going to create a repository named mybackup in the root directory.

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

restic init --repo ~/mybackup

After executing the above command, enter the password that you want to set for this repository.

Note: Please remember this password if you wish to access the data in this repository or it will be permanently lost.

When the above command is executed it will return the following output.

As the image above shows, a local repository with the ID 96d4af8d27 is created at /root/mybackup location.

Now we can back up our data into this repository.

Backup Data Into Local Repository Using Restic

We are going to backup the entire ~/Tutor directory into the repository ~/mybackup. Execute the following command to back up the entire directory.

restic -r ~/mybackup backup ~/Tutor

First let us break down the above command and see what each parameter means.

  • restic: the restic application command
  • -r: indicates the repository where we want to create backup
  • ~/mybackup: repository name
  • backup: restic sub-command to backup files
  • ~/Tutor: the directory to backup.

When the above command is executed, you will be asked to provide the password for the repository where the backup is being created. Execution of the above command will return the following output.

As the above output shows, a backup of the directory Tutor has been created and a snapshot of the current backup with a unique name d7423006 has also been created.

Now add more files or folders to the backup directory. For example, we added test.txt 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

When the above command is executed again, it will return the following output.

As we can see in the output, 1 new file was backed up. And a new snapshot 6564c790 has been created. Restic will create snapshots with unique ID’s everytime we run it. And since it does incremental backups, the subsequent backup will be faster than the previous one.

List Snapshots

To list all the snapshots available in the local repository, execute the following command.

restic -r ~/mybackup snapshots

The above command will return the following output.

The above output returns the number of snapshots present in our local directory. As you can see, we have three snapshots with unique IDs available.

Check the difference between two Snapshots

In order to see the difference between two snapshots, we can execute the following command with diff option.

restic -r ~/mybackup diff d7423006 d58b79a8

The above command will list the differences between the two given snapshots. When the command is executed it will return the following output.

The comparison of the two snapshots shows that we have added a new test.txt file to the backup.

Exclude Files From Backup

We can exclude files or directories from being backed up. Execute the following command to exclude all .pdf files:

restic -r ~/mybackup backup --exclude=*.pdf ~/Tutor

There is another more efficient method that allows us to exclude multiple file types and folders at the same time. To do this put the actual locations of all files and folders that we want to exclude from the backup and then specify the path of this file in the backup command.

For example, we create a file named DontBackup:

vi DontBackup.txt

Now add files and folders that we want to exclude from the backup:

Output
*.txt
ostechnix.zip
mydata/movies

Now run the backup by executing the following command:

restic -r ~/mybackup backup --exclude-file=exclude.txt ~/Tutor

If you wish to see more information related to restic backup command, run the following command.

restic help backup

Now that we know how to backup our files and folders using the restic command, let’s take a look at how to restore data from the backup.

Restore Data via Restic

Restoring data using the Restic command is fairly easy. First, we have to list all the available snapshots in the repository. This is done by executing the following command.

restic -r ~/mybackup snapshots

Above command will return all available snapshots. Now in order to restore data from a specific snapshot run the following command. For example, we want to restore data from snapshot d7423006. The command to do so looks like the below.

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

The execution of the above command will return the following output.

The above command restored the data from the snapshot d7423006 to the /Tutor directory.

We can also restore a single file from a snapshot. To do this execute the following command. This command will restore the .txt file from the snapshot ID of d7423006.

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

To access more information related to the restore command, execute the following command.

restic help restore
Mount Repository to view Backup Data

We can mount the repository to browse the backup as a regular file system. This is especially useful in scenarios in which we want to view the contents of a repository.

To do this, first we create a mount point by executing the following command.

mkdir ostech/

Then we mount our repository to the ostech mount point by executing the below mentioned command.

restic -r ~/mybackup mount ostech/

The command will mount the Restic repository in our local file system. And the output will look as follows.

Output
enter password for repository: 
repository c1c5bcfd opened successfully, password is correct
Now serving the repository at ostech/
When finished, quit with Ctrl-c or umount the mountpoint.

Now we can open our file manager and see that the repository is mounted there. We can go to the snapshots folder inside the ostech mount point and verify the data.

When done, press Ctrl - c to quit or unmount the repository from the file manager.

To access more information related to the mount function, run the following command.

restic help mount
Conclusion

After going through this tutorial you know how to backup your important data using the Restic backup program.

We learned how to install Restic on our specific Linux distribution, how to create backup, how to compare snapshots, and how to restore data from snapshots.

Ref From: bytexd

Related articles