TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Linux / Operations / Storage

Linux: Create System Backups With rsnapshot

Rsnapshot works as a wrapper for the widely used rsync tool and is fairly easy to install and configure. Start here to learn more.
Oct 12th, 2024 7:00am by
Featued image for: Linux: Create System Backups With rsnapshot
Image via Unsplash.

One step to data reliability is backing up your data on a regular basis. You never know when something could go wrong with a server or desktop, leading to a loss of critical files or configurations. To avoid such a nightmare, you might want to consider using a tool that handles incremental backups of local and remote file systems.

One such tool is rsnapshot, which benefits from using hard links, so disk space is used only when necessary. Rsnapshot works as a wrapper for the widely used rsync tool and is fairly easy to install and configure.

I’m going to walk you through the process of installing and configuring rsnapshot on Ubuntu Server 22.04, but you can make use of this application on most Debian-based distributions as well as those based on Fedora.

What You’ll Need

The only things you’ll need are a running instance of Ubuntu Server and a user with sudo privileges. Because rsnapshot can also back up to an external drive, you might also consider connecting such a drive for even better backup reliability. After all, should your OS go down and render the machine unbootable, if your backups are stored on the drive housing the OS, you could lose those backups as well.

For example, you might connect and external drive and mount it to a new directory named /backup, which is what I will demonstrate here. To make that happen, you might also want to configure that drive to automount at boot, which would require a line similar to this in the /etc/fstab file:


Make sure you use your particular drive UUID and any options you prefer for the automounting of drives.

With that said, let’s get to the installation.

Installing rsnapshot

The rsnapshot package can be installed from the standard repositories with the command:


If you’re using a Fedora-based distribution, the installation command is:


If your distribution of choice is Arch Linux, the command is:


This should install all dependencies. If you find rsync doesn’t install, do so with:

  • Ubuntu: sudo apt-get install rsync -y
  • Fedora: sudo dnf install rsync -y
  • Arch: sudo pacman -S rsync

Configuring rsnapshot

Now that rsnapshot is installed, it’s time to configure it. One thing to keep in mind (and this is very important) is that you can’t use spaces in the configuration file; if you do, it will result in syntax errors. Instead, if necessary, use tabs.

Open the configuration file with the command:


The first line you want to look for is this one:


On a Fedora-based distribution, that line might read:


The above line configures the directory that will house the backups. For example, if you’re going with my suggestion of an external drive mounted to /backup, the line would be:


You will also want to disable the creation of the root directory; otherwise, you’ll wind up with a child directory with /backup. To disable this feature, look for the following line:


Uncomment the line by removing the # character so the result looks like this:


You’ll need to know the path to the rsync executable, which can be found with the command:


The results should be /usr/bin/rsync. If it’s anything else, take note, because you have to configure that path in the following line:


Next, we need to set a retention policy. This is handed in the BACKUP LEVELS / INTERVALS section of the configuration file, where you’ll see the following default options:


The names above are arbitrary and the number is how many backups of that type will be retained. You can change the names if you’d like to, but remember that they should be in ascending order, and that the names you choose will be used to run the specific backups. You could change those names from alpha, beta and gamma to daily, weekly and monthly, which would make a lot more sense.

The next section to configure is what you want to back up. This section is listed under #LOCALHOST (near the bottom of the configuration file), where you’ll find the following:


You can change the directories to be backed up to whatever you need, but leave localhost/ as is; that instructs rsnapshot that we’re backing up to the local machine.

It’s also possible to exclude and include files from the backups. This is handled above the LOCALHOST section, where you’ll see the following:


For example, you might have specific files you don’t want to include in the backup. For that, make sure to create an exclude line with the direct path to the file in question.

Once you’ve taken care of the above, save and close the file with the Ctrl+X keyboard shortcut.

Testing the Configuration

Before you launch the backup, you should test the syntax of your configuration file with the command:


If the command returns Syntax OK, you’re good to go.

Let’s run a test on the daily backup (which I used in place of alpha in the config file). To run the test, issue the command:


You’ll see output that looks something like this:


To run the first backup, issue the command:


When the backup completes, you’ll find a subdirectory named daily.0 in /backup that houses the snapshot.

Scheduling Backups

Rsnapshot doesn’t include a built-in scheduler, so you’ll have to make use of cron. What we’ll do is create three entries — one each for daily, weekly and monthly. Issue the command:


At the bottom of the file, add the following lines:


The above lines do the following:

  • Takes a daily snapshot at 1 a.m.
  • Takes a weekly snapshot every Saturday.
  • Takes a monthly snapshot on the first of each month at 2 a.m.

And that’s it. You now have a backup system that will automatically take snapshots of the configured directories and save them to your chosen destination.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.