dotlinux blog

How to Set Up and Use Restic for Linux Backups

In today's digital age, data is one of our most valuable assets. Whether it's important documents, precious memories in the form of photos and videos, or critical business data, losing it can be a disaster. That's where backups come in. Restic is a powerful, open-source backup tool specifically designed for Linux (and other operating systems too). It offers features like encryption, deduplication, and easy management of backups. In this blog, we'll walk you through the process of setting up and using Restic for Linux backups.

2026-03

Table of Content#

  1. Installation
  2. Initial Setup
  3. Configuring the Repository
  4. Performing Backups
  5. Restoring Backups
  6. Managing Backups
  7. Automation with Cron

Installation#

Using Package Managers (Debian/Ubuntu)#

For Debian and Ubuntu-based systems, you can use the following commands:

sudo apt update
sudo apt install restic

Using Package Managers (Fedora/RHEL)#

On Fedora or RHEL systems:

sudo dnf install restic

From Source (Advanced)#

If you prefer to build from source, first install the necessary build tools (e.g., gcc, make). Then, clone the Restic repository from GitHub:

git clone https://github.com/restic/restic.git
cd restic
make
sudo make install

Initial Setup#

Generating a Password#

Before you start using Restic, you need to generate a password. This password will be used to encrypt your backups. You can use a tool like pwgen (install it with sudo apt install pwgen on Debian/Ubuntu) to generate a strong password:

pwgen -s 32 1

Write down this password securely as you'll need it later.

Initializing the Repository#

Restic needs a repository to store your backups. This can be a local directory, a remote server (e.g., via SSH, SFTP, or S3-compatible storage). Let's assume we're using a local directory for simplicity. Create a directory for the repository:

mkdir ~/restic-repo

Now, initialize the repository with the following command. Replace <your-password> with the password you generated earlier:

restic -r ~/restic-repo init --password <your-password>

This will create the necessary structure inside the restic-repo directory.

Configuring the Repository#

To make it easier to use Restic without constantly typing the repository path and password, you can set environment variables. Open your shell's configuration file (e.g., ~/.bashrc for Bash). Add the following lines:

export RESTIC_REPOSITORY=~/restic-repo
export RESTIC_PASSWORD=<your-password>

Then, source the file to apply the changes:

source ~/.bashrc

Performing Backups#

Basic Backup Command#

To perform a backup of a directory (let's say /home/user/documents), use the following command:

restic backup /home/user/documents

Restic will scan the directory, calculate hashes for deduplication, and upload the data to the repository. You can see the progress in the terminal.

Excluding Files/Directories#

If you have files or directories you don't want to back up (e.g., temporary files, large media files you've already backed up elsewhere), you can use an exclude file. Create a file named exclude.txt in your home directory with lines like:

/tmp/*
/home/user/documents/large-video-files/

Then, use the --exclude-file option:

restic backup --exclude-file ~/exclude.txt /home/user/documents

Restoring Backups#

Listing Snapshots#

First, list the available snapshots (backups) in the repository:

restic snapshots

This will show you details like the snapshot ID, time of backup, and the directories included.

Restoring a Specific Snapshot#

To restore a snapshot (let's say the one with ID <snapshot-id>) to a specific directory (e.g., ~/restored-documents), use the following command:

restic restore <snapshot-id> --target ~/restored-documents

Restic will download the necessary data from the repository and restore it to the specified target directory.

Managing Backups#

Pruning Old Backups#

Over time, you'll accumulate multiple snapshots. To free up space by removing old backups (while keeping a certain number of recent ones), you can use the prune command. For example, to keep the last 7 daily backups:

restic prune --keep-daily 7

You can also use options like --keep-weekly and --keep-monthly depending on your retention policy.

Checking the Repository#

To check the integrity of the repository (e.g., after a system crash or suspected corruption), use:

restic check

This will scan the repository and report any issues.

Automation with Cron#

Creating a Cron Job#

To automate backups (e.g., daily at 2 AM), create a cron job. Edit the cron table:

crontab -e

Add a line like this (assuming your backup command is restic backup /home/user/documents):

0 2 * * * restic backup /home/user/documents

This will run the backup command every day at 2 AM.

Reference#

That's a comprehensive guide to setting up and using Restic for Linux backups. With Restic, you can have peace of mind knowing your data is safe and easily recoverable.