How to Backup and Restore Files with Rsnapshot on Ubuntu

This article details the process of backing up and restoring files using Rsnapshot on Ubuntu. It describes Rsnapshot as a tool that enables automated, incremental backups utilizing hard links, allows configuration for local and remote systems, and emphasizes the importance of regular backups for data recovery. With a simple setup, data safety is enhanced.

This article explains how to backup and restore files with Rsnapshot on Ubuntu Linux.

rsnapshot is a filesystem snapshot utility for making backups on a local or remote system. It leverages the capabilities of rsync and hard links. Hard links are a feature of the file system that allows multiple files to point to the same data on disk, which is used by rsnapshot to efficiently create incremental backups.

rsnapshot provides a simple configuration file, at /etc/rsnapshot.conf, to manage backup schedules and settings. This file lets you specify the directories you want to back up, the frequency of backups, and other settings, making it easy to automate regular backups without much hassle.

Regular backups help safeguard your data. In case of accidental deletion, system failure, or corruption, you can easily restore your system to a previous state.

Install rsnapshot

rsnapshot is included in the default Ubuntu repository so it’s easy to install. To install it, run the command below.

sudo apt update
sudo apt install rsnapshot

After installing the app, you can check the binary location as well as its version installed on Ubuntu by running the command below.

which rsnapshot
rsnapshot --version

Configure SSH authentication for remote system

When you are backing up to or from a remote system, you must ensure SSH authentication is configured for the target systems.

You can use the ssh-keygen -t ed25519 command below to generate the SSH keypair and ssh-copy-id to copy the public key to the remote server.

ssh-keygen -t ed25519
ssh-copy-id username@remote_server_name

Replace the username and remote_server_name with the corresponding username and system names.

For more about setting up SSH authentication, read the post below.

How to set up SSH key based authentication login on Ubuntu

Configure rsnapshot

Once you have set up the remote system, configure rsnapshot settings so to select the files to backup, interval and others.

Before you do that, first make a backup of the original file.

sudo cp /etc/rsnapshot.conf /etc/rsnapshot.conf.orig

Then, open the file and make your changes.

sudo nano /etc/rsnapshot.conf

Choose were to store your snapshots.

# All snapshots will be stored under this root directory.
#
snapshot_root /var/cache/rsnapshot/

These lines enable remote backup so make sure they are not commented.

# rsync must be enabled for anything to work. This is the only command that
# must be enabled.
#
cmd_rsync /usr/bin/rsync

# Uncomment this to enable remote ssh backups over rsync.
#
cmd_ssh /usr/bin/ssh

Change how often backup occurs.

#########################################
# BACKUP LEVELS / INTERVALS #
# Must be unique and in ascending order #
# e.g. alpha, beta, gamma, etc. #
#########################################

retain daily 6
retain weekly 7
retain monthly 4

#retain delta 3

SSH communicates over port 22 by default. If you have a custom port, specify here.

# ssh has no args passed by default, but you can specify some here.
#
ssh_args -p 22

Use the backup section to specify what and where to backup. The highlight lines show you how to backup a remote server and path to a local server.

Replace the username and remote_server_name with the corresponding systems.

###############################
### BACKUP POINTS / SCRIPTS ###
###############################

# LOCALHOST
backup /home/ localhost/
backup /etc/ localhost/
backup /usr/local/ localhost/

backup username@remote_server_name:/etc/ local_server_name
backup username@remote_server_name:/www/data/ local_server_name

#backup /var/log/rsnapshot localhost/
#backup /etc/passwd localhost/
#backup /home/foo/My Documents/ localhost/
#backup /foo/bar/ localhost/ one_fs=1,rsync_short_args=-urltvpog
#backup_script /usr/local/bin/backup_pgsql.sh localhost/postgres/
# You must set linux_lvm_* parameters below before using lvm snapshots
#backup lvm://vg0/xen-home/ lvm-vg0/xen-home/

Save your changes and exit.

Automatic backup with Cron

Once your config changes are saved, open the rsnapshot cron file and setup the automatic backup.

sudo nano /etc/cron.d/rsnapshot

Specify the backup settings as defined in the rsnapshot file.

# This is a sample cron file for rsnapshot.
# The values used correspond to the examples in /etc/rsnapshot.conf.
# There you can also set the backup points and many other things.
#
# To activate this cron file you have to uncomment the lines below.
# Feel free to adapt it to your needs.

0 0 * * * root /usr/bin/rsnapshot daily
0 0 * * 0 root /usr/bin/rsnapshot weekly
0 0 1 * * root /usr/bin/rsnapshot monthly

Save the file and exit.

For help setting up a cron task, read the post below.

How to run cron jobs on Ubuntu

To restore your data, copy from the snapshots location defined in the rsnapshot file.

That should do it!

Conclusion:

Backing up and restoring files with rsnapshot on Ubuntu offers an efficient and effective way to safeguard your data. Here are the key takeaways:

  • Automated Backups: Rsnapshot allows for easy scheduling of automated backups, ensuring your data is regularly protected without manual intervention.
  • Incremental Backups: By utilizing hard links, rsnapshot can perform incremental backups, saving both time and storage space.
  • Remote Backups: The tool can back up files to and from remote systems, enhancing flexibility and security for critical data.
  • Simple Configuration: With its straightforward configuration file, users can easily manage backup settings, including frequency and directories to back up.
  • Ease of Restoration: Restoring data from snapshots is a simple process, making recovery quick and efficient in case of data loss.

Incorporating rsnapshot into your data management routine significantly enhances data safety and reliability.

Richard Avatar

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *


Exit mobile version