Restic is a free, fast, open source, secure and cross-platform backup program, written using Go programming language. Restic encrypts the data with AES-256 in counter mode and authenticate it using Poly1305-AES cryptographic message authentication code. Backing up and restoring data with restic is really fast, and easy. In this tutorial, we will see how to install restic and how to backup and restore data using Restic on Linux.
Install Restic In Linux
On Arch Linux, Restic is available in [Community] repository. Make sure the Community repository is enabled and install Restic using Pacman as shown below.
$ sudo pacman -S restic
Restic is also available in AUR. So, you can install it using any AUR helpers, such as Yay, in Arch-based systems.
$ yay -S restic-git
On Debian, Ubuntu, Linux Mint:
$ sudo apt-get install restic
On Nix OS:
$ nix-env --install restic
On openSUSE:
$ sudo zypper install restic
Using Linuxbrew:
$ brew install restic
For other operating systems, you can compile and install it like below. First, make sure you have installed Go language on your system.
Once Go installed, git clone restic github respository:
$ git clone https://github.com/restic/restic
This command will clone all the contents of restic repository in the current working directory in your system.
Cd to the restic directory:
$ cd restic
And, install Restic like below:
$ go run build.go
Backup and restore data using Restic
We can backup our important data on our local system itself, however it is not a real backup strategy. Restic supports the following backends to store the backup:
- Local directory
- sftp server (via SSH)
- HTTP REST server
- AWS S3
- OpenStack Swift
- BackBlaze B2
- Microsoft Azure Blob Storage
- Google Cloud Storage
Due to lack of resources and time, I have only covered how to backup and restore data in a local directory. For learning other backup methods, click on the respective link given above.
Backup data in local directory using Restic
First, let us create a repository to store the backup. For example, I am going to create a repository named backup in my HOME directory.
$ restic init --repo ~/backup
Enter the password for the repository twice. You must remember the password to access this repository later. Otherwise, you will permanently lose the data stored in the repository. You have been warned!
enter password for new repository: enter password again: created restic repository cbeb0c0585 at /home/sk/backup Please note that knowledge of your password is required to access the repository. Losing your password means that your data is irrecoverably lost.
Next, backup your data to the repository:
$ restic -r ~/backup backup ~/mydata
Enter the password of your repository:
Sample output:
enter password for repository: password is correct scan [/home/sk/mydata] scanned 1 directories, 3 files in 0:00 [0:00] 100.00% 90.272 KiB / 90.272 KiB 4 / 4 items 0 errors ETA 0:00 duration: 0:00 snapshot 6eda7c7d saved
Here, I am backing up ~/mydata folder to the repository ~/backup.
As you can see, restic created the backup of the given directory i.e mydata. Additionally, I created a snapshot of the current backup with a unique name 6eda7c7d.
If you run the above command again, restic will create another snapshot with a unique name and this time it will backup pretty faster than the previous backup. We can keep adding the data in the folder and run the backup to create many snapshots.
To check the difference between two snapshots, we can use diff option like below
$ restic -r ~/backup diff 6eda7c7d b52d462b
Sample output:
enter password for repository: password is correct comparing snapshot 6eda7c7d to b52d462b: + /mydata/Country road.mp3 Files: 1 new, 0 removed, 0 changed Dirs: 0 new, 0 removed Others: 0 new, 0 removed Data Blobs: 2 new, 0 removed Tree Blobs: 1 new, 1 removed Added: 2.991 MiB Removed: 1.127 KiB
As you see, I have added a new mp3 file in the backup.
To list the available snapshots in a repository, run:
$ restic -r ~/backup snapshots enter password for repository: password is correct ID Date Host Tags Directory ---------------------------------------------------------------------- 6eda7c7d 2018-03-02 18:16:48 sk /home/sk/mydata b52d462b 2018-03-02 18:21:11 sk /home/sk/mydata ---------------------------------------------------------------------- 2 snapshots
As you see, I have 2 snapshots, namely 6eda7c7d and b52d462b.
Not just entire directories, restic also allows us to backup a single file as well.
$ restic -r ~/backup backup ~/mydata/file.txt
It is also possible to exclude some files or directories. For example the following command will exclude all .doc type files:
$ restic -r ~/backup backup --exclude=*.doc ~/mydata
Alternatively, you can put the actual location of all files and folders that you want to exclude from the backup in a file and specify its path in the backup command.
For example, create a file named exclude:
$ vi exclude
Add the files or folders you want to exclude:
*.txt ostechnix.zip mydata/movies
Now, start backup process using command:
$ restic -r ~/backup backup --exclude-file=exclude ~/mydata
For more details about restic backup, please run:
$ restic help backup
Now, we have successfully backed up our data. Next we will see how to restore the data from the local backup.
Restore data using Restic
Restoring data is easy! Just use the following command to restore the data from a snapshot:
$ restic -r ~/backup restore b52d462b --target ~/mydata
Sample output:
enter password for repository: password is correct restoring <Snapshot b52d462b of [/home/sk/mydata] at 2018-03-02 18:21:11.244087232 +0530 IST by [email protected]> to /home/sk/mydata
We just restored the data from the snapshot b52d462b to ~/mydata.
You already know how to list the available snapshots, right? Here is how we list the snapshots.
$ restic -r ~/backup snapshots
To restore a single file from the snapshot, we do:
$ restic -r ~/backup restore b52d462b --target ~/mydata file.txt
For more details, check the restore help section.
$ restic help restore
Viewing backup data
You might not want to restore the data, but only view them. It is easy. You can browse the backup as a regular file system. First, create a mount point:
$ mkdir ostechnix
Then, mount your repository on ostechnix mount point as shown below.
$ restic -r ~/backup mount ostechnix/
Now, open your file manager and you will see your repository is mounted. You can browse it as the way you do your local file system.
For more details, check the help section:
$ restic help mount
Remove snapshots
First, list all available snapshots in a repository:
$ restic -r ~/backup snapshots enter password for repository: password is correct ID Date Host Tags Directory ---------------------------------------------------------------------- 6eda7c7d 2018-03-02 18:16:48 sk /home/sk/mydata b52d462b 2018-03-02 18:21:11 sk /home/sk/mydata ---------------------------------------------------------------------- 2 snapshots
To delete a snapshot, for example 6eda7c7d, run:
$ restic -r ~/backup forget 6eda7c7d enter password for repository: password is correct removed snapshot 6eda7c7d
Check whether the snapshot is deleted or not:
$ restic -r ~/backup snapshots enter password for repository: password is correct ID Date Host Tags Directory ---------------------------------------------------------------------- b52d462b 2018-03-02 18:21:11 sk /home/sk/mydata ---------------------------------------------------------------------- 1 snapshots
The snapshot is gone! However, the data that was referenced by files in this snapshot is still stored in the repository. To cleanup unreferenced data, run:
$ restic -r ~/backup prune
Sample output:
enter password for repository: password is correct counting files in repo building new index for repo [0:00] 100.00% 4 / 4 packs repository contains 4 packs (9 blobs) with 3.081 MiB processed 9 blobs: 0 duplicate blobs, 0B duplicate load all snapshots find data that is still in use for 1 snapshots [0:00] 100.00% 1 / 1 snapshots found 7 of 9 data blobs still in use, removing 2 blobs will remove 0 invalid files will delete 1 packs and rewrite 0 packs, this frees 1.555 KiB counting files in repo [0:00] 100.00% 3 / 3 packs finding old index files saved new indexes as [75bbcda0] remove 2 old index files [0:00] 100.00% 1 / 1 packs deleted done
You know now how to install and use Restic backup program to safeguard your important data. We have only scratched the surface. There is more! I recommend you to check the Restic official documentation for more detailed usage.
Resources:
Thanks for stopping by!
Help us to help you:
- Subscribe to our Email Newsletter : Sign Up Now
- Support OSTechNix : Donate Via PayPal
- Download free E-Books and Videos : OSTechNix on TradePub
- Connect with us: Reddit | Facebook | Twitter | LinkedIn | RSS feeds
Have a Good day!!
2 comments
Thank you for this! This fine summary article is what convinced me to use restic. Well done.
thanks for the summary !! assuming that I want to remove the restic files (not snapshot) after testing