Learn how to – Backup Linux System to Minio S3 and SFTP using Restic.
Data backup is a crucial task on any system. The user’s data is one of the organization’s most essential resources. Losing this proprietary data and information may be fatal since the organization spends most time building it. Data loss can lead to revenue losses, upset customers e.t.c. Companies that do not make backups may fall victims to data loss.
Today there are several tools that can be used to make data backups. These include Rsync, Timeshift, Bacula, CloudBerry Backup, Bareos, Amanda, Clonezilla, BackupPC, DĂ©jĂ Dup e.t.c
Restic is one of the most secure data backup programs. This tool is written in the Go language. Restic can be used to back up local; files to different backend storage such as local directories, SFTP, S3 storage e.t.c.
The features provided by Restic are:
- It is freely available for use and completely open-source
- Secure with cryptography in every part of the process
- Easy to install and use
- verifiably, enabling you to make sure that your files can be restored when needed
- Can be installed on Linux, Windows, Mac, and BSD systems
This guide will walk you through how to install Restic and use it to back up Linux System to Minio S3 and SFTP Server.
Step 1 – Install Restic on Linux
Restic exists in several Linux system repositories which makes the installation very easy.
The below commands can be used to install Restic on the desired Linux system.
##On debian/Ubuntu
sudo apt update
sudo apt install restic
##On Fedora
sudo dnf copr remove copart/restic
sudo dnf install restic
##On OpenSUSE
sudo zypper install restic
##On RHEL 8 &9 /CentOS 8 & 9/Rocky Linux 8/AlmaLinux 8
sudo dnf install epel-release -y
sudo dnf install restic -y
##On RHEL 7/CentOS 7
sudo yum install yum-plugin-copr
sudo yum copr enable copart/restic
sudo yum install restic
##On Arch Linux
pacman -S restic
Once installed, check the Restic version.
$ restic version
restic 0.13.1 compiled with go1.17.7 on linux/amd64
To update Restic, use the command:
$ sudo restic self-update
writing restic to /usr/bin/restic
find latest release of restic at GitHub
restic is up to date
Check the new version.
$ restic version
restic 0.13.1 compiled with go1.17.7 on linux/amd64
Step 2 – Prepare a new repository
In this context, a repository refers to the location where the Restic backups will be saved. The repository can be local or stored on a remote server. When making automated backups, Restic accepts the repository expressed using the RESTIC_REPOSITORY environment variable. You can also specify the repository using the flag --repository-file
A. Local
For example, to create a local repository say at /srv/restic-repo, use the command:
$ sudo restic init --repo /srv/restic-repo
enter password for new repository:
enter password again:
created restic repository 417acc230c at /srv/restic-repo
Please note that knowledge of your password is required to access
the repository. Losing your password means that your data is
irrecoverably lost.
B. SFTP
To make data backups via SFTP, you need to establish SSH access to the remote server. Normally, passwordless login is recommended and so we need to generate and copy SSH keys as required.
ssh-keygen -t rsa
Copy the generated keys to the remote host replacing the IP address on the remote system appropriately. The root user is required since they have permissions to access the /srv directory in the remote system
ssh-copy-id -i ~/.ssh/id_rsa.pub root@remote_IP
Now you should be able to login into the remote system without password authentication.
Now specify the IP address and port of the remote SFTP server. To do this, create an SSH config file:
sudo vim /etc/ssh/ssh_config
Add the details for the remote host:
Host restic-backup-host
Hostname remote_IP_address
User root
Port 22
Now use the command below to create a repository:
$ restic -r sftp:restic-backup-host:/srv/restic-repo init
enter password for new repository:
enter password again:
created restic repository 05e02f3b2c at sftp:restic-backup-host:/srv/restic-repo
Please note that knowledge of your password is required to access
the repository. Losing your password means that your data is
irrecoverably lost.
C. Minio S3
The Minio server offers an S3-compatible object Object Storage. Minio S3 -can be configured on your system using the aid in the guides below:
- Deploy MinIO Storage on Rocky Linux 8|AlmaLinux 8|CentOS 8
- Deploy and Manage MinIO Storage on Kubernetes
To create the Minio repository, begin by exporting the variables as below:
export AWS_ACCESS_KEY_ID=<YOUR-MINIO-ACCESS-KEY-ID>
export AWS_SECRET_ACCESS_KEY=<YOUR-MINIO-SECRET-ACCESS-KEY>
Now initialize your Restic repository using the command:
restic -r s3:http://IP_addres:9000/restic init
To automate the process later, it is recommended that you put all the variables in a file as below.
vim ~/.restic-env
Provide Minio the variables as below:
export AWS_ACCESS_KEY_ID=<YOUR-MINIO-ACCESS-KEY-ID>
export AWS_SECRET_ACCESS_KEY=<YOUR-MINIO-SECRET-ACCESS-KEY>
export RESTIC_REPOSITORY="s3:http://IP_Address:9000/restic"
export RESTIC_PASSWORD="a-strong-repository-password"
Source the file:
source ~/.restic-env
Verify if the repository;
$ echo $RESTIC_REPOSITORY
s3:http://192.168.205.2:9000/restic
Now initialize the repository easily with the command:
restic init
Sample Output:
You will have the restic bucket created on Minio S3 as shown.
Step 3 – Perform Backup on Minio S3 and SFTP Server using Restic
With the repositories initialized, we are set to perform data backups. The contents of a directory are called the snapshot. To take a backup, of the local directory say ~/Downloads, with the below files:
$ ls
amzn2-vmware_esx-2.0.20220606.1-x86_64.xfs.gpt.ova docker-desktop-4.9.1-amd64.deb facts.txt
The command with the syntax below can be used:
##For SFTP
restic -r sftp:restic-backup-host:/srv/restic-repo -verbose backup ~/Downloads
##For Minio S3
restic -r s3:http://IP_addres:9000/restic --verbose backup ~/Downloads
##OR(when using the .restic-env file)
restic --verbose backup ~/Downloads
Sample Output:
The backup is really fast and that makes Restic really nifty. To create another backup of the above directory, use a similar command as above. For example:
restic --verbose backup ~/Downloads
Sample Output:
This time a new repository will be created and the process is even faster since no new data is added.
To back up individual files, provide the file name for example:
restic -r /srv/restic-repo backup ~/work.txt
To list snapshots use the command:
For SFTP
restic snapshots -r sftp:restic-backup-host:/srv/restic-repo
Sample Output:
For Minio S3
restic snapshots
Sample Output:
Step 4 – Comparing Snapshots
To be able to see the difference between the two snapshots, Restic provides the diff
command that can be used as shown:
##For SFTP
restic -r sftp:restic-backup-host:/srv/restic-repo diff fa174dfb 9eeb825c
##For Minio S3
restic diff fa174dfb 9eeb825c
Sample output:
Step 5 – Restore a Snapshot
Once a backup has been taken, it only becomes useful when restored. To restore the backups taken, use the restore
command.
For example, restoring a backup 9eeb825c to /tmp/restore-work, the command will be:
##For SFTP
restic -r sftp:restic-backup-host:/srv/restic-repo restore 9eeb825c --target /tmp/restore-work
##For Minio S3
restic restore 9eeb825c --target /tmp/restore-work
Execution output:
repository 94460011 opened successfully, password is correct
restoring <Snapshot 9eeb825c of [/home/deb11/Downloads] at 2022-06-27 07:57:56.788365598 -0400 EDT by deb11@learnhowto> to /tmp/restore-work
Verify this:
The latest backup taken can be restored with the command:
restic -r sftp:restic-backup-host:/srv/restic-repo restore latest --target /tmp/restore-new
Step 6 – Automating Backups
Backups can be automated by creating Cron Jobs. The forget
is used to maintain a running archive of snapshots. The restic forget --prune
sets policies of the backups to be done in a day, week ,or hour.
So we will create a cron job with the command:
crontab -e
Add the lines below:
Minio S3
For Minio using the ~/.restic-env file with all the variables:
30 * * * * . /home/deb11/.restic-env; restic backup -q /home/deb11/Downloads; restic forget -q --keep-last 2 --prune
In the command:
- 30 * * * *: runs the backup on the 31st minute of every hour, day, month, and day of the week
- . /home/deb11/.restic-env; is the
source ~/.restic-env
command. - restic backup -q /home/deb11/Downloads; is the backup command and backs up contents of ~/Downloads
- restic forget -q –keep-last 2 –prune prunes the old snapshots no longer required using the defined policy. Here the last two snapshots are maintained
SFTP
The concept is still the same but here you need to create a secret file containing the password of your Restic repository:
echo "Password_here" > ~/.secret
Now add the Cron job.
30 * * * * restic --password-file /home/deb11/.secret -r sftp:restic-backup-host:/srv/restic-repo backup -q /home/deb11/Downloads; restic -r sftp:restic-backup-host:/srv/restic-repo forget -q --keep-last 2 --prune --password-file /home/deb11/.secret
The end!
Closing Thoughts
That marks the end of this guide on how to backup Linux systems to Minio S3 and SFTP Server using Restic. This guide is a starter that illustrates the basic backup and restores using Restic. I hope this was significant.
See more:
- Best Secure Backup Application for Linux|macOS|Windows
- Install and Use Bacula Backup on Rocky Linux 8|AlmaLinux 8
- Install and Use Bareos Backup Solution on Debian
Leave a Reply
You must be logged in to post a comment.