---
title: "Peace of Mind with Restic Backup on Linux"
description: "Learn how to backup your data in your Linux machine using the Restic backup tool in this step-by-step tutorial!"
canonical: "https://adamtheautomator.com/restic-backup/"
---

# Peace of Mind with Restic Backup on Linux

> Learn how to backup your data in your Linux machine using the Restic backup tool in this step-by-step tutorial!

Source: https://adamtheautomator.com/restic-backup/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Peace of Mind with Restic Backup on Linux](https://adamtheautomator.com/wp-content/uploads/2022/03/Peace-of-Mind-with-Restic-Backup-on-Linux.jpg)

# Peace of Mind with Restic Backup on Linux

[![](https://secure.gravatar.com/avatar/572a248f516b6d0cd566cb44fdbd9336f30ef95aa0f6d78f118c1f47b1b6d6b7?s=192&d=mm&r=g)Arvid Larson](https://adamtheautomator.com/author/arvid-larson/)25 March 202211 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Backups](/tag/backups/)[Linux](/tag/linux/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Installing Restic on Linux](#installing-restic-on-linux)
*   [Setting up a Backup Server](#setting-up-a-backup-server)
*   [Initializing a New Repository](#initializing-a-new-repository)
*   [Backing up Data to a Repository](#backing-up-data-to-a-repository)
*   [Excluding Files in Backup Processes](#excluding-files-in-backup-processes)
*   [Removing Snapshots from Repository](#removing-snapshots-from-repository)
*   [Restoring Data on Repository](#restoring-data-on-repository)
*   [Restoring Specific Files](#restoring-specific-files)
*   [Backing up Data Automatically with Restic and Cron](#backing-up-data-automatically-with-restic-and-cron)
*   [Conclusion](#conclusion)

Backup is one of the most critical things in every industry, and you may be looking for which backup solution is most reliable. Why not try [Restic](https://restic.net/) backup on Linux?

Restic is a secure, fast, and reliable backup solution. And in this tutorial, you’ll learn to set up Restic backup system on a Linux server.

Read on and never lose any of your data anymore!

## Prerequisites

This tutorial will be a hands-on demonstration. To follow along, be sure you have the following:

*   Two Linux Servers – This tutorial uses two [Debian 11](https://www.debian.org/releases/bullseye/debian-installer/index) servers named _server1_ and _backup-server_.
    
*   A non-root user with sudo privileges.
    

## Installing Restic on Linux

Restic stores data in the encrypted repository, and whenever you add data to the repository, Restic automatically creates a new snapshot. But before taking advantage of the Restic backup utility, you’ll first have to install Restic on your machine.

1\. Open your terminal and log in to the `server1` machine.

2\. Next, run the `wget` command below to download the Restic binary file. As of this writing, the latest version of Restic is v0.12.1.

```bash
wget https://github.com/restic/restic/releases/download/v0.12.1/restic_0.12.1_linux_amd64.bz2
```

Related:[How to Download Files with Python Wget](https://adamtheautomator.com/python-wget/)

3\. Run the following commands to extract the `restic_0.12.1_linux_amd64.bz2` file, and verify the `restic_0.12.1_linux_amd64` binary file exists.

```bash
# Extract restic_0.12.1_linux_amd64.bz2
bzip2 -dv restic_0.12.1_linux_amd64.bz2
# List the files to verify restic_0.12.1_linux_amd64 exists
ls
```

![Extracting Restrict’s Binary File](https://adamtheautomator.com/wp-content/uploads/2022/03/image-403.png)

Extracting Restrict’s Binary File

4\. Now, run the `chmod` command to make the `restic_0.12.1_linux_amd64` file executable, then rename and move the file to the `/usr/bin/restic` directory.

At this point, you can now run the restic command on your server.

```bash
# Make Restic file executable
chmod +x restic_0.12.1_linux_amd64

# Installing Restic
mv restic_0.12.1_linux_amd64 /usr/bin/restic
```

Related:[Manage Directory and File Permissions with chmod Recursive](https://adamtheautomator.com/chmod-recursive/)

5\. Run the `restic` command below to generate the bash completion for Restic. Doing so makes it easy for users to run the `restic` command.

```bash
restic generate --bash-completion /etc/bash_completion.d/restic
```

6\. Lastly, run the below command to reload the bash completion on your current terminal session, then run the `restic` command and press **TAB** to show available options for the `restic` command.

Lastly, run the following commands to reload the bash completion on your current terminal session. Type restic, and press the TAB key to show available options for the restic command.

```bash
# reload bash completion
source /etc/profile.d/bash_completion.sh

# verify restic command
restic
```

You should see all available options of the restic command, as shown below.

![Setting up Restic’s Auto-Complete](https://adamtheautomator.com/wp-content/uploads/2022/03/image-404.png)

Setting up Restic’s Auto-Complete

## Setting up a Backup Server

Now that you’ve installed Restic on the `server1` machine, the next step is to set up the `backup-server`, so you can start backing your data through the SFTP protocol.

> _By default, Restic allows you to backup data to the local directory, remote server, and SFTP protocol. Restic also lets you backup data to third-party cloud backup services such as Amazon S3, Azure Blog Storage, Backblaze B2, and Google Cloud Storage._

1\. Open a new terminal and log in to the `backup-server` machine.

2\. Next, run the below command to create a new user named `johndoe` and set up a password.

```bash
# create new user
useradd -m -s /bin/bash johndoe

# setup the password
passwd johndoe
```

3\. Run each command below to create a new backup directory (`/srv/backup`) for Restic and change the ownership (`chown`) to the `johndoe` user and group. This directory will store all backup data from the `server1` machine.

```bash
# create new backup directory
mkdir -p /srv/backup

# change ownership of the backup directory
sudo chown -R johndoe:johndoe /srv/backup
```

4\. Now, switch to the `server1` machine, and run the below command to generate an SSH key.

Instead of using password-based authentication, you’ll go for key-based authentication. An SSH key is essential, especially when setting up an automatic backup with a shell script or using [Cron](https://adamtheautomator.com/list-cron-jobs/) jobs.

```bash
ssh-keygen
```

Related:[How To Execute and List Cron Jobs for a Linux System via PHP](https://adamtheautomator.com/list-cron-jobs/)

Press ENTER to the following prompts, as shown below. Doing so sets the save location of your SSH key to the default directory (~/.ssh) and disables the password for your SSH private key.

At this point, in the ~/.ssh directory, you’ll have the id\_rsa (SSH private key) and the id\_rsa.pub (your public key) files.

![Generating an SSH Key](https://adamtheautomator.com/wp-content/uploads/2022/03/image-405.png)

Generating an SSH Key

5\. Run the `ls` command below to verify that the _id\_rsa_ and _id\_rsa.pub_ files exist in the `~/.ssh` directory.

```bash
ls ~/.ssh/
```

![Verify ID RSA](https://adamtheautomator.com/wp-content/uploads/2022/03/image-406.png)

Verify ID RSA

6\. Next, run the [`ssh-copy-id`](https://linuxhint.com/use-ssh-copy-id-command/) command below to upload the SSH public key to the `backup-server`. In this demo, the `backup-server` uses the username `johndoe` and the IP address `172.16.1.150`.

```bash
ssh-copy-id johndoe@172.16.1.150
```

When the upload completes, you’ll receive the message saying Number of key(s) added: 1 on your terminal.

![Copying SSH key to the backup-server](https://adamtheautomator.com/wp-content/uploads/2022/03/image-407.png)

Copying SSH key to the backup-server

7\. Lastly, run the `ssh` command below to verify and connect to the `backup-server`.

```bash
ssh johndoe@172.16.1.150
```

Notice that you automatically logged in to the backup-server without password authentication.

![Logging with SSH key](https://adamtheautomator.com/wp-content/uploads/2022/03/image-408.png)

Logging with SSH key

## Initializing a New Repository

You’ve now configured the Restic backup server, so the next step is to initialize a new repository. Restic will store backup data in a repository, and each backup action creates a new snapshot inside that repository.

1\. Run the `restic` command below to initialize (`init`) a repository (`-r <sftp:johndoe@172.16.1.150>:/srv/backup`) using the SFTP protocol.

```bash
restic -r sftp:johndoe@172.16.1.150:/srv/backup init
```

2\. Input a strong password for the repository and confirm the password. Remember the password, as you’ll use it to decrypt your backup data.

As shown below, the new repository (a9073b62d0) is created on the SFTP server (sftp:johndoe@172.16.1.150:/srv/backup).

![Initializing a New Repository](https://adamtheautomator.com/wp-content/uploads/2022/03/image-409.png)

Initializing a New Repository

3\. Lastly, run the `restic` command below to verify the repository. You’ll be prompted to input the password you previously noted for the repository (step two).

```bash
restic -r sftp:johndoe@172.16.1.150:/srv/backup check
```

If there’s no error, you’ll get the no errors were found message, shown below.

![Verifying the New Repository](https://adamtheautomator.com/wp-content/uploads/2022/03/image-410.png)

Verifying the New Repository

## Backing up Data to a Repository

You’ve now initialized the repository using the SFTP server on the `backup-server`. But how do you backup data with Restic? You’ll back up data from `server1` to the repository in the `backup-server`.

1\. Run the `restic` command below to back up the data from the `/var/www/wordpress` directory to the repository.

```bash
restic -r sftp:johndoe@172.16.1.150:/srv/backup backup /var/www/wordpress
```

Input the password for the repository, and when the backup process is complete, Restic creates the snapshot (736949a9), as shown below.

![Backing up Data with Restic](https://adamtheautomator.com/wp-content/uploads/2022/03/image-411.png)

Backing up Data with Restic

2\. Next, run the below command to list all `snapshots`.

```bash
restic -r sftp:johndoe@172.16.1.150:/srv/backup snapshots
```

Ensure the snapshot (736949a9) is available on the list, as shown below

![Checking Snapshots](https://adamtheautomator.com/wp-content/uploads/2022/03/image-412.png)

Checking Snapshots

3\. Run the below command to list all files (`ls`) on the snapshot (`736949a9`).

```bash
restic -r sftp:johndoe@172.16.1.150:/srv/backup ls 736949a9
```

You’ll see the list of files on the snapshot (736949a9), which is the backup data from the /var/www/wordpress/ directory.

![Checking Files on the Snapshot](https://adamtheautomator.com/wp-content/uploads/2022/03/image-413.png)

Checking Files on the Snapshot

Additionally, you can run the below command to backup multiple directories (/var/www/wordpress and /var/www/html) to the same repository.

```bash
restic -r sftp:johndoe@172.16.1.150:/srv/backup backup /var/www/wordpress /var/www/html
```

Notice below that Restic created a new snapshot (7b6e321e).

![Backing up Multiple Directories](https://adamtheautomator.com/wp-content/uploads/2022/03/image-414.png)

Backing up Multiple Directories

4\. Now, run each command below to list all snapshots, stats of backup files, and the difference between two snapshots (`736949a9` and `7b6e321e`).

```bash
# checking list snapshots
restic -r sftp:johndoe@172.16.1.150:/srv/backup snapshots

# show stats of repository
restic -r sftp:johndoe@172.16.1.150:/srv/backup stats

# checking different between two snapshots
restic -r sftp:johndoe@172.16.1.150:/srv/backup diff 736949a9 7b6e321e
```

Below, the snapshots 736949a9 and 7b6e321e have the same backup data (/var/www/wordpress). Notice that their file sizes on the repository are also the same since Restic only backs up file changes, making Restic fast and efficient.

![Checking and Comparing Snapshots](https://adamtheautomator.com/wp-content/uploads/2022/03/image-415.png)

Checking and Comparing Snapshots

## Excluding Files in Backup Processes

Backing up data comes in handy, but how do you exclude unnecessary data? Restic lets you exclude files/directories with the `--exclude-file` option during the backup process.

The `--exclude-file` option can exclude junk files, such as log files, making the backup process faster and cleaner.

1\. Run the below command to create a new directory called `/opt/backup` to store the backed-up files.

```bash
mkdir -p /opt/backup
```

2\. Next, create a new file (`/opt/backup/excludes.txt`) using your preferred text editor and populate the following configuration.

The configuration below tells Restic to exclude files with different file types.

```powershell
# excludes files .zip
*.tar.gz

# excludes directory logs
logs

# exclude .txt files on the directory data
data/*.txt
```

3\. Run the below command to backup data from the _/var/www/html_ directory to the repository (`-r <sftp:johndoe@172.16.1.150>:/srv/backup`). The `--exclude-files` option tells Restic to exclude files and directories listed in the _exludes.txt_ file.

```bash
restic -r sftp:johndoe@172.16.1.150:/srv/backup backup /var/www/html --exclude-file=/opt/backup/excludes.txt
```

Below, the new backup snapshot ca99be01 is created by Restic, excluding files, you specified in the _excludes.txt_ file.

![Excluding Files from the Backup](https://adamtheautomator.com/wp-content/uploads/2022/03/image-416.png)

Excluding Files from the Backup

4\. Now, run the below command to verify the list of files (`ls`) on the snapshot `ca99be01`.

```bash
restic -r sftp:johndoe@172.16.1.150:/srv/backup ls ca99be01
```

You’ll see all backed-up files except those you defined in the configuration file (_excludes.txt_).

![Listing Files on Snapshot](https://adamtheautomator.com/wp-content/uploads/2022/03/image-417.png)

Listing Files on Snapshot

5\. Finally, run the `tree` command below to verify excluding data works with Restic.

```bash
tree /var/www/html/
```

As you see below, the /var/www/html directory contains text files inside, but Restic didn’t exclude them from the backup.

![Listing Files on /var/www/html Directory](https://adamtheautomator.com/wp-content/uploads/2022/03/image-418.png)

Listing Files on /var/www/html Directory

## Removing Snapshots from Repository

In most cases, you may only need two or three versions of snapshots at the latest. So why not remove the older snapshots? Removing snapshots require that you know the snapshot’s ID, so check the list of snapshots first.

1\. Run the below command to list all snapshots in the repository.

```bash
restic -r sftp:johndoe@172.16.1.150:/srv/backup snapshots
```

Pick one ID of the snapshot to remove (736949a9).

![Listing Snapshots in the Repository](https://adamtheautomator.com/wp-content/uploads/2022/03/image-419.png)

Listing Snapshots in the Repository

2\. Next, run the following commands to remove (`forget`) specific snapshot (`736949a9`), and its remaining data from the repository.

```bash
# Removes snapshot only
restic -r sftp:johndoe@172.16.1.150:/srv/backup forget 736949a9
# Removes remaining data from repository
restic -r sftp:johndoe@172.16.1.150:/srv/backup prune
```

![Removing Snapshot and Remaining Data](https://adamtheautomator.com/wp-content/uploads/2022/03/image-420.png)

Removing Snapshot and Remaining Data

3\. Run the below command to perform a `--dry-run` and see which snapshots will be removed (`forget`) and which two snapshot versions will be kept (`--keep-last 2`).

> Before deleting snapshots, a _dry run is always recommended to avoid losing important snapshots._

```bash
restic -r sftp:johndoe@172.16.1.150:/srv/backup forget --keep-last 2 --prune --dry-run
```

![Checking which Snapshots to Remove and to Keep](https://adamtheautomator.com/wp-content/uploads/2022/03/image-421.png)

Checking which Snapshots to Remove and to Keep

4\. Run the below command to remove (`forget`) old snapshot versions and keep the last two (`--keep-last 2`) from the repository.

```bash
restic -r sftp:johndoe@172.16.1.150:/srv/backup forget --keep-last 2 --prune
```

![Remove Forget](https://adamtheautomator.com/wp-content/uploads/2022/03/image-422.png)

Remove Forget

5\. Finally, run the below command to list all existing snapshots.

```bash
restic -r sftp:johndoe@172.16.1.150:/srv/backup snapshots
```

You can see below that only the last two snapshot versions remain.

![Checking Available Snapshots](https://adamtheautomator.com/wp-content/uploads/2022/03/image-423.png)

Checking Available Snapshots

## Restoring Data on Repository

Perhaps you accidentally removed crucial data from the repository. If so, how do you get them back? Luckily, Restic lets you decrypt and restore backup data so long as you know the snapshot ID of the backup.

1\. Run the `restic` command below to check available snapshots on the repository.

```bash
restic -r sftp:johndoe@172.16.1.150:/srv/backup snapshots
```

Below is an example of snapshots available on the repository. Note the snapshot ID of the backup data to restore.

![Checking Snapshots](https://adamtheautomator.com/wp-content/uploads/2022/03/image-424.png)

Checking Snapshots

2\. Next, run the following commands to create a restore directory (`/opt/restore`) and restore your target snapshot (`6b44ac32`) to that restore directory.

```bash
# Create a restore directory
mkdir -p /opt/restore
# Restore snapshot data to the /opt/restore directory
restic -r sftp:johndoe@172.16.1.150:/srv/backup restore 6b44ac32 --target /opt/restore
```

![Restoring Backup Data](https://adamtheautomator.com/wp-content/uploads/2022/03/image-425.png)

Restoring Backup Data

3\. Finally, run the `ls` command below to verify the restored data.

```bash
ls /opt/restore/var/www/wordpress
```

As shown below, you’ll see the list of files restored from the snapshot (6b44ac32).

![Viewing Restored Data](https://adamtheautomator.com/wp-content/uploads/2022/03/image-426.png)

Viewing Restored Data

## Restoring Specific Files

You’ve just restored lost data, but what if you only need to restore a specific file? With Restic, you can target a specific file by appending the `--include` option in the `restic` command.

1\. Run the below command to create a directory to hold the specific file to restore from a snapshot.

```bash
mkdir -p /opt/restore/specific
```

2\. Next, run the `restic` commands below to restore a specific directory (`/var/www/wordpress/wp-content`) and a specific file (`wp-config.php`) from the snapshot (`6b44ac32`). Each command sets the target restore directory to the `/opt/restore/specific` directory.

```bash
# restore specific directory wp-config
restic -r sftp:johndoe@172.16.1.150:/srv/backup restore 6b44ac32 --target /opt/restore/specific --include /var/www/wordpress/wp-content

# restore specific file wp-config.php
restic -r sftp:backup@172.16.1.150:/srv/backup restore 6b44ac32 --target /opt/restore/specific --include /var/www/wordpress/wp-config.php
```

![target restore directory](https://adamtheautomator.com/wp-content/uploads/2022/03/image-427.png)

target restore directory

3\. Lastly, run the `ls` command below to verify the restored file and directory in the directory (`/opt/restore/specific`).

```bash
ls /opt/restore/specific/var/www/wordpress
```

If the restoration worked correctly, you’d see the directory (wp-content) and file (wp-config.php) restored from the snapshot (6b44ac32).

![Verifying Restored File and Directory](https://adamtheautomator.com/wp-content/uploads/2022/03/image-428.png)

Verifying Restored File and Directory

## Backing up Data Automatically with Restic and Cron

At this point, you’ve already learned all the basic usage of the Restic backup tool. But since manually backing up data can be a pain, why automatically backup data on your Linux system? Restic, together with Cron, can do wonders, such as automating the process of data backup.

1\. Create a new file (`/opt/backup/.secret`) using your preferred editor and input the password of your repository. This demo uses the password `ResticRepositoryPassword` for the Restic repository.

2\. Next, run the below command to change the permission of the `/opt/backup/.secret` file to `600`. The permission code `600` gives read and write permission only to the`/opt/backup/.secret` file owner.

```bash
chmod 600 /opt/backup/.secret
```

3\. Run the [`crontab`](https://www.computerhope.com/unix/ucrontab.htm) command below to create a new Cron job. The `-u root` option indicates the Cron job will be running as the root user.

```bash
crontab -u root -e
```

4\. Select your preferred editor, and the Cron configuration file opens in that editor. But this demo uses the **nano** editor.

![Creating a New Cron Job](https://adamtheautomator.com/wp-content/uploads/2022/03/image-429.png)

Creating a New Cron Job

5\. Finally, populate the Cron configuration with the following configuration to set up automatic backup and save the changes.

The below script performs the following:

*   Run the `restic` command every `1.AM` to backup directories (`/var/www/wordpress` and `/var/www/html`) to the repository SFTP server (`sftp:johndoe@172.16.1.150:/srv/backup`).
    
*   Automatically delete old snapshot at `2.AM` and keep the last two versions of snapshots.
    
*   Specifies (`--password-file`) the file (`/opt/backup/.secret`) that contains your password for the Restic repository, so you don’t have to hard code your password in the Cron configuration.
    

```powershell
# Runs daily backup at 1.AM
0 1 * * * restic --password-file /opt/backup/.secret -r sftp:johndoe@172.16.1.150:/srv/backup backup --verbose /var/www/{wordpress,html}

# Runs every 2.AM, and always keeps the last 2 backup snapshots
0 2 * * * restic --password-file /opt/backup/.secret -r sftp:johndoe@172.16.1.150:/srv/backup forget --keep-last 2 --prune
```

## Conclusion

In this tutorial, you’ve learned how to install Restic on Linux and different ways to backup your data, from directories to specific files with snapshots. Apart from data backup, you’ve discovered that you can restore unexpectedly lost data. Automation plays a big part in backing up your data, so keep the Cron jobs running to secure your data.

At this point, you’re ready to backup more data using Restic. And why not leverage your backup by using [tags](https://restic.readthedocs.io/en/latest/040_backup.html#tags-for-backup), filtering [policy](https://restic.readthedocs.io/en/latest/060_forget.html#removing-snapshots-according-to-a-policy), and [backup cloud services](https://restic.readthedocs.io/en/latest/030_preparing_a_new_repo.html)?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Frestic-backup%2F&text=Peace%20of%20Mind%20with%20Restic%20Backup%20on%20Linux)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Frestic-backup%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Frestic-backup%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/09/Practical-Linux-Unix-Tee-Commands-for-the-Linux-Admin.jpg)

### [Master Unix tee Commands for Real-World Linux Admin Tasks](/unix-tee/)

Simplify Linux output management and streamline your workflow with the “Unix tee” command. This tutorial guides you through its practical applications.

![](https://adamtheautomator.com/wp-content/uploads/2024/03/openldap-4.jpg)

### [How to Install and Configure an OpenLDAP Ubuntu Server](/openldap/)

Unlock the power of OpenLDAP on Ubuntu for centralized user authentication, seamless access control management, and enhanced directory services!

![](https://adamtheautomator.com/wp-content/uploads/2024/03/pipe-command-in-linux-1.jpg)

### [Unleashing the Power of the Pipe Command in Linux](/pipe-command-in-linux/)

Unlock the prowess of the pipe command in Linux to streamline tasks, boost productivity, and simplify complex operations effortlessly!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
