One day your blog, code, or pretty much anything may crash, and sadly, your most valuable information could be irredeemably lost. Consider the consequences if this ever happens (touch wood!). Pictured them? Scary, right? Now, just imagine how relaxed you would have been instead if only you’d bothered to make a backup.
What Is It?
rdiff-backup backs up one directory to another, possibly over a network. The target directory ends up being a copy of the source directory. But extra reverse diffs are stored in a special subdirectory of that target directory, so you can still recover files lost some time ago. The idea is to combine the best features of a mirror and an incremental backup.
rdiff-backup is available in the most important Linux distribution. In my case, I’m using an ArchLinux distribution (Manjaro) and the yay package (Yet another Yogurt, an AUR Helper written in Go) to install the tool.
If you use another distribution, this software can also be installed:
apt-get install rdiff-backupyum install rdiff-backup
Making backups is very easy when you using
rdiff-backup. You may picture this tool as similar to the
cp command. In other words,
rdiff-backup has two arguments:
- source directory
- target directory
Both directories can be local or on a remote disk. For example, if you want to use
rdiff-backup in a local directory, you would use the following command:
In the same way, if any of the directories are in a remote server, you need only indicate the path using the classic way:
user@server::PATH. The following commands show how either the remote or local servers can be used in both the source and target directories:
When using these commands, the remote machine will probably request the user’s password (for the previous commands,
luisgarcia respectively). You can omit this step by configuring an SSH key-based authentication on a Linux server.
The real power of this tool is truly appreciated when wanting to restore the information. If you list the contents of the directory in which you made your copy, you will see the contents that you’d previously copied, and furthermore, you will find a directory named
rdiff-backup-data. This directory is very important since it stores the incremental backups of our data.
In this directory, the contents shown consist of the last version of our backup plus the incremental copies, which are stored in the
Now imagine that I’ve created a file called
file1.txt that contains a single sentence. A copy is done using
rdiff-backup and, a few minutes after, another copy is done. Now we are shown the list of files in our system, which is the following:
You may note that the file
file1.txthas an incremental copy in the
We can restore a copy with the
rdiff-backup command or by directly using the
cp command, since the copy is neither compressed nor has any of its metadata altered. Therefore, the files are in the same state as when they were copied. Although you may use the
cp command, the
rdiff-backup tool is better to use, due to the data restoration being more flexible.
The use of the command for restoring backups is similar to the one to make the backup, with the added the option of (restore-as-of,
-r), as well as the timestamp to restore. The timestamp is very flexible since the acceptible time strings are intervals, like 3D64s;
w3-datetime strings like 2002-04-26T04:22:01-07:00 (strings like 2002-04-26T04:22:01 are also acceptable —
rdiff-backup will use the current time zone); or ordinary dates, like 2/4/1997 or 2001-04-23 (various combinations are acceptable, bearing in mind that the month must always precede the day).
For example, the following command restores the copy made on 23 January 2010.
As you already know, the
rdiff-backup command makes an incremental backup, which entails a large amount of space disk being consumed. Therefore, it is highly recommended to remove old backups (as long as you have other, more recent backups, of course).
rdiff-backup tool has the
remove-older-than option, which removes any backups older than that the date used in the argument. A good example is removing any backups older than one year:
Most of the time, we are required to include files in or exclude files from our backup. The most common options which can be used in the
As well as these, there are plenty more filter options to make our backups, such as:
In this example, we exclude
/mnt/backup to avoid an infinite loop, even though
rdiff-backup can automatically detect simple loops like the one above. This is just an example. In reality, it would be important to exclude
/proc as well.
There may be a time when we need information about the backup (metadata).
rdiff-backup allows us to obtain this information. The most common options for this are the following:
Since they are quite descriptive, it isn’t hard to imagine what the goal of each of the different options is. Despite this, I will show several examples applying each of them:
Using It in cron
A good practice is automating the backups in our system. To do this, we may use the cron service.
Prior to using cron, we must remember to make sure that the script used in cron doesn’t output anything, otherwise:
- cron will assume there is an error.
- If there is any error, you will not be able to see it.
The command that we used in our script is the following:
The content of the
files_backup.txt file is the following:
+ /root/ghost - **
It’s important to know that both success and error logs are saved in the same logfile, named
rdiff-backup.log. Another interesting point is that I've used the filter option
include-globbing-filelist, which allows the use of a file as argument. This file contains the directories that will be backed up by using the string
- to express. That said, the directory must be either included or excluded. Note that the backups older than one year are deleted to preserve disk space.
Finally, edit the cron file using the
crontab -e command.
0 1 * * * sh /root/rdiff-backup-configuration/rdiff-backup.sh
In this piece, I’ve explained the
rdiff-backup tool, which allows us to make incremental backups. I've also shown you the script I use to back up my projects, which is executed by cron once a day.