Restic is a modern, open-source backup program designed for efficiency, security, and simplicity. It enables users to create encrypted, incremental backups of files and directories, ensuring that data is safely stored and can be restored in case of data loss, corruption, or accidental deletion. Restic supports a wide range of backend storage options, including local directories, remote servers via SFTP, and various cloud services such as Amazon S3 and Backblaze B2. With features like deduplication, snapshot management, and pruning, Restic minimizes storage usage while maintaining data integrity. Its ease of use and strong focus on security make it an excellent choice for both individual and enterprise-level backup solutions.
Backing up your server is essential to ensure data safety and recoverability in case of hardware failure, data corruption, or accidental deletions. Restic is an efficient, fast, secure backup program supporting various backend storage options. This guide will walk you through setting up and using Restic to back up your Ubuntu 24.04 server.
Prerequisites
Before we begin, ensure you have the following:
- A server running Ubuntu 24.04.
- Root or a user with
sudo
privileges. - Access to a storage backend for storing backups (e.g., an external drive, cloud storage, or an SFTP server).
- Internet connection if you plan to use cloud storage.
Install Restic
Restic is included in the default Ubuntu repositories. I will show you how to install restic from the Ubuntu repository (method 1) and how to get the latest binary by downloading it manually (method 2).
Method 1: Install from the Official Ubuntu Repository (recommended)
Update your package index:
sudo apt update
Install Restic:
sudo apt install restic
Method 2: Install from the Official Binary
Download the latest Restic binary from the official GitHub releases page:
wget https://github.com/restic/restic/releases/download/v0.17.0/restic_0.17.0_linux_amd64.bz2
Decompress the file:
bzip2 -d restic_0.17.0_linux_amd64.bz2
Make the binary executable:
chmod x restic_0.17.0_linux_amd64
Move the binary to /usr/local/bin
for global usage:
sudo mv restic_0.17.0_linux_amd64 /usr/local/bin/restic
Verify the installation:
restic version
Initialize a Backup Repository
Restic requires a repository (a location where backups are stored). This repository can be local (a directory on the server) or remote (on another server or cloud storage).
Local Repository
Choose or create a directory where the backups will be stored. For example, use /backup/restic
:
sudo mkdir -p /backup/restic
Initialize the repository:
sudo restic init --repo /backup/restic
You’ll be prompted to enter a password to secure the repository. Make sure to remember this password, as you’ll need it for all operations with this repository.
Remote Repository (SFTP)
To back up to a remote server via SFTP:
Initialize the repository on the remote server:
restic -r sftp:user@hostname:/path/to/repo init
Replace user
, hostname
, and /path/to/repo
with appropriate values.
Enter the password when prompted.
Cloud Repository
Restic supports various cloud providers (e.g., Amazon S3, Backblaze B2, Google Cloud Storage). The configuration varies slightly depending on the provider. Refer to the Restic documentation for detailed instructions on setting up cloud storage as a repository.
Create a Backup
Now that your repository is set up, you can create your first backup.
Backup a Directory
To back up a directory (e.g., /home/user/data
):
sudo restic -r /backup/restic backup /home/user/data
If you’re using a remote repository, use:
sudo restic -r sftp:user@hostname:/path/to/repo backup /home/user/data
Excluding Files and Directories
You can exclude specific files or directories from the backup using the --exclude
option:
sudo restic -r /backup/restic backup /home/user/data --exclude /home/user/data/temp
For multiple exclusions, use an exclude file:
- Create an exclude file:
sudo nano /home/user/exclude.txt
- Add paths to be excluded:
/home/user/data/temp /home/user/data/cache
- Use the exclude file during backup:
sudo restic -r /backup/restic backup /home/user/data --exclude-file /home/user/exclude.txt
Automated Backups
You can automate backups using cron jobs. Open the crontab editor:
sudo crontab -e
Add a cron job to run daily at 2 AM:
0 2 * * * /usr/local/bin/restic -r /backup/restic backup /home/user/data >> /var/log/restic_backup.log 2>&1
Save and exit the editor.
This cron job will run the backup command daily at 2 AM and log output to /var/log/restic_backup.log
.
Verify Backups
It’s crucial to verify your backups to ensure data integrity regularly.
sudo restic -r /backup/restic check
For remote repositories:
sudo restic -r sftp:user@hostname:/path/to/repo check
Restore Data from Backup
Restic makes it straightforward when you need to restore data.
Restore to Original Location
To restore a directory to its original location:
sudo restic -r /backup/restic restore latest --target /
Restore to a Different Location
To restore to a different location, specify the target directory:
sudo restic -r /backup/restic restore latest --target /restore/location
Browsing and Selecting Snapshots
You can browse available snapshots before restoring:
sudo restic -r /backup/restic snapshots
To restore a specific snapshot:
sudo restic -r /backup/restic restore [snapshot_id] --target /restore/location
Replace [snapshot_id]
with the ID of the snapshot you want to restore.
Maintenance and Pruning
Over time, your backup repository may grow in size. Restic provides options to prune old data and maintain the repository.
Prune Old Snapshots
To remove old snapshots and optimize storage usage:
sudo restic -r /backup/restic forget --keep-last 5 --prune
This command keeps the last 5 snapshots and removes others, followed by pruning unused data.
Schedule Pruning with Cron
You can automate pruning with a cron job:
- Open the crontab editor:
sudo crontab -e
- Add a cron job to run pruning weekly at 3 AM:
0 3 * * 7 /usr/local/bin/restic -r /backup/restic forget --keep-last 5 --prune >> /var/log/restic_prune.log 2>&1
- Save and exit the editor.
Conclusion
Restic is a powerful and flexible tool for backing up your Ubuntu 24.04 server. By following this guide, you can ensure that your data is safely backed up and can be restored easily in case of an emergency. Remember to regularly verify and prune your backups to maintain data integrity and optimize storage usage. With Restic’s secure encryption and efficient data management, your backup strategy will be both robust and reliable.