Table of Content#
- Installation
- Initial Setup
- Configuring the Repository
- Performing Backups
- Restoring Backups
- Managing Backups
- 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 resticUsing Package Managers (Fedora/RHEL)#
On Fedora or RHEL systems:
sudo dnf install resticFrom 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 installInitial 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 1Write 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-repoNow, 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#
Environment Variables (Optional but Recommended)#
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 ~/.bashrcPerforming 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/documentsRestic 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/documentsRestoring Backups#
Listing Snapshots#
First, list the available snapshots (backups) in the repository:
restic snapshotsThis 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-documentsRestic 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 7You 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 checkThis 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 -eAdd 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.