APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Using Rsnapshot

Rsnapshot is a Perl script that uses rsync to create incremental backups. You can download it from http://www.rsnapshot.org/.

The first thing to decide is where you will keep your backups. By default, the configuration file points them at /.snapshots. Although the script is smart enough to avoid containing its own directory in backups (because it would lead to recursion), I'd still rather have this directory somewhere off the backup set if possible. In my case, I use this for backup of a remote webserver, so /.snapshots is fine. This, and everything else, is set in the configuration file (/etc/rsnapshot.conf by default). All elements have to be tab separated.

If backup is to mounted media, you may also want to set no_create_root:

snapshot_root	/mnt/.snapshots/
no_create_root	1

By not allowing rsnapshot to create the snapshoot_root directory, it will then fail if media is not mounted (you would have to create the .snapshots directory on the media manually).

The next step is to specify what will be backed up and how. You'll need to uncomment ssh if backing up from a remote site:

cmd_ssh	/usr/bin/ssh

Next, you specify intervals:

interval	daily	3
interval	weekly	2
interval	monthly	2

That's actually bad terminology: these are really labels for backup sets, and will end up as directories under your snapshot_root directory. The labels themselves control nothing; you need a cron job to run rsnapshot, or you need to run it manually. When you run it, you''ll be telling it what interval you are using:

rsnapshot daily

Just realize that the "daily" label really has no meaning unless you run "rsnapshot daily" on a daily basis. If you ran it hourly, rsnapshot would still use the "daily" label - it's just a label for your convenience. You could call it "fred" for all rsnapshot cares.

The "3" in "interval daily 3" means that we'll end up with three directories - daily.0, daily.1, daily.2. The daily.0 will always be the most current set. So, if you begin by running "rsnapshot daily", that will create /.snapshots/daily.0 and backup to there. If you run the same command ten minutes later, you'll have daily.0 and daily.1, and daily.0 will have the most current set. At that point, daily.1 will have what was in the daily.0 set. Run it once more, and you'll add daily.2, which of course will have what daily.0 originally had at the beginning.

The files in the directories are hard links if nothing has changed. So, if you were backing up /usr/fred, and no files had changed, the files in daily.0 and daily.1 would just be links to the files in daily.2 and wouldn't consume any disk space beyond their directory slots. This is a simple scheme, but it works well. the disk space used for multiple sets might only be a little more that one backup set. That's especially apt to be so if the thing being backed up has a lot of small files rather than a few very large files - so it's probably better for a web server than a big database app.

However, if your data does lend itself to this, it makes it very easy to go back in time exactly where you want to be without any guesswork. An interesting side effect is that you can tell whether or not a file has changed simply by examining the number of links in an ls -l listing.

Of course we haven't yet specified what we're backing up. That comes a bit later in the configuration file, but before that we get to specify include and exclude options. These are passed directly to rsync, and can be a little confusing, so I'll cover that in more depth later. For the moment, let's just tell rsnapshot that we have an excludes file:<./p>

exclude_file	/Users/apl/bin/excludes

Next are the things to backup (I have skipped over settings that should be obvious or are well explained in the file itself). You can specify multiple lines, each with a source, a tab, and the directory under your interval directories to store it under. For example, you might have:

backup	foo@foobar.com:/usr/local/www/vhosts/		mywebsite/
backup	ralph@othersite.com:/usr/ralph		ralph/
backup	/home/		localhomedirs/

You can also specify a backup script. This might be used for dumping databases, running lynx -dump, etc. It needs to create files which rsnapshot then backs up.

All of this is easy enough, but we often want to exclude files, and that can be a little confusing. Let's look at an example exclude file:


These are all slightly different ways of excluding, and each will have slightly different meaning as to what actually will be excluded. I tested this on a directory containing these files and directories:

$ find testrsnap -name not_this -exec file {} \;
testrsnap/home/not_this: directory
testrsnap/home/not_this/home/b/not_this: empty
testrsnap/homes/not_this: directory
testrsnap/homes/this/not_this: directory
testrsnap/not_this: ASCII text

The config has:

backup	/Users/tony/testrsnap	mydocs

The first backup was done with nothing in the excludes file. The results of a find in the snap directory are (as expected) no different from the source files:

$ cd ~/snapshots/daily.0/mydocs/Users/apl/testrsnap 
$ find . -name not_this -exec file {} \;
./home/not_this: directory
./home/not_this/home/b/not_this: empty
./homes/not_this: directory
./homes/this/not_this: directory
./not_this: ASCII text

I then tested with each exclude parameter. The results were:

  • */not_this

    • No file or directory named "not_this" will be backed up. The "find" will list no files.

  • not_this

    • No file or directory named "not_this" will be backed up. The "find" will list no files.

  • not_this/

    • No directory named "not_this" will be backed up. Ordinary files will be. The "testrsnap/home/not_this/home/b/not_this" will not be backed up because it is in a subdirectory named "testrsnap/home/not_this/". The "find" will only list
      ./not_this: ASCII text.

  • home/not_this

    • No file or directory named "home/not_this" will be backed up. The find will list
      ./homes/not_this: directory
      ./homes/this/not_this: directory
      ./not_this: ASCII text

If this doesn't make sense immediately, experiment with a small set of files until you see how it works. I've found the excludes confusing, which may be because of how rsync looks at --exclude. Rsnapshot provides easily understood incremental backups.

Got something to add? Send me email.


Increase ad revenue 50-250% with Ezoic

More Articles by

Find me on Google+

© Tony Lawrence

Fri Feb 18 21:16:27 2005: 39   anonymous


Tue Aug 12 14:19:59 2008: 4484   wilson

I was looking for a quick clear explanation about excludes and this is it!
Also, the comments about the hourly, daily labels was informative too. I was a little confused about that.

Sat Jul 11 07:28:55 2009: 6630   LargerMon

Thanks for the tip!

Tue Nov 3 18:50:06 2009: 7421   BrianStPierre

Thanks for this tutorial.

A warning on excludes: if you specify exclude rules as rules in the conf file (and not externally), do not put quotation marks around the pattern. Rsnapshot will pass the quotation marks to rsync and it will treat them as part of the pattern.

Tue Nov 3 21:55:24 2009: 7422   TonyLawrence

Thanks! I probably will forget that by the time I have need to use a question mark, but I'll only have myself to blame :-)

Mon Feb 14 01:46:55 2011: 9310   OscarGonzalez


This is a great writeup about rsnapshot. Very clear and much more relatable than the man page.

Kerio Samepage

Have you tried Searching this site?

Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Try not to become a man of success, but rather try to become a man of value. (Albert Einstein)

This post tagged: