More backup goodness with rsync

20 11 2008

So, a while ago I posted about how to use rsync and rsback to backup important files and folders. Wanting to be extra safe, I decided to create another backup on a separate disk stored somewhere else at home. This will act as a form of redundant backup. It wont backup everything that often, but I will always have something to fall back on in case both my main drive and backup drive fail, which is quite unlikely.

NSLU2

NSLU2

This solution uses the rather fabulous little NSLU2, a great little NAS device made by Linksys, I will post more about this little tool later.

Once again I decided to make use of rsync, since it seems like the best option and I was now more comfortable with it. I wanted my NSLU2 to pull data from my Ubuntu server onto a USB disk attached to the NSLU2. The best way to do this, is run an rsync daemon on the server and run rsync on the NSLU2 scheduled via cron.

First, you need to install rsync on the server, which I had already done for my previous backup solution, use synaptics and search for rsync if you haven’t got it installed already. Next, to get rsync to run in daemon mode automatically, you need to edit the /etc/default/rsync file and ensure that

RSYNC_ENABLE=true

Next, you need to create an rsyncd.conf file, located at /etc/, that controls how the daemon will operate. Now, you have to be carefull here with permissions, I had loads of problems with permissions before I figured this one out. You can set a number of options in the rsyncd.conf file to do with security and what folders can be accessed. Here is mine:

uid = root
gid = root
motd file = /etc/rsyncd.motd
log file = /etc/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock


[backup]
path = /mnt/media/backupsfolder
comment = Media Disk Backup
uid = nobody
gid = nobody
read only = yes
list = yes
hosts allow = 192.168.1.142

The most important part is uid and gid, which determines what user the daemon runs at, whichever you choose, that user MUST have the correct permission to write to the directory that the log and pid files are stored. If you don’t choose a user with the correct permission, you will find that the daemon will run, but wont be able to transfer anything. The motd location and file is for the Message Of The Day, the file can contain whatever little message you want the rsync daemon to show when the rsync client connects. [backup] and what follows lists the folders that you want to make available for copying. You can have as many different folders as you want, each with their own [name] and file location. You can also change which rsync clients are allowed to read which file lists, using the hosts allow field. There are other options you can add here, to do with users and passwords, but I haven’t bothered since no one else on my network is using rsync.

Now that that is set up, you then need to set up the actual rsync client that does the pulling of the files on the NSLU2. I have decided to do this by a script that is run via cron. So, go create a script somewhere, I have stored mine at /etc/rsync/backuprsync.sh. When you create the file, make sure you make it executable, I chose chmod 744 /etc/rsync/backuprsync.sh. I wanted to have some details about the rsync process logged to a file, that was emailed to me, so, I added some extra code to the script. Here it is:


#!/bin/sh
echo Subject: Backup Backup Started on `date "+%m/%d/%y %l:%M:%S %p"` > /mnt/disk/backuprsync.log
rsync -av --stats rsync://192.168.1.111/backup/ /mnt/disk/backup/ >> /mnt/disk/backuprsync.log
echo END: Backup Backup Complete on `date "+%m/%d/%y %l:%M:%S %p"` >> /mnt/disk/backuprsync.log
cat /mnt/disk/backuprsync.log | ssmtp someaddress@gmail.com

What this does is first removes rm the old logfile (so make sure when you first run this, there is a log file there, otherwise the script will fail 😉 It then prints the time the backup started echo Subject:... to some location I have chosen /mnt/disk/backuprsync.log. Next is the actual rsync command. The -av --stats options creates an archive file (a), creates detailed output (v) and creates some stats at the end (stats). rsync://192.168.1.111/backup/ is the address for the Ubuntu server based on its IP address, whilst backup is the name of the file location set up in the rsyncd.conf file. If you want to change the backup job to another file location, then just change backup to whatever you called the other job. /mnt/disk/backup/ is the location on the NSLU2 where the backup is stored. Finally, >> /mnt/disk/backuprsync.log will output the results of -v and –stats to the log file (by the way using >> instead of > means it appends the output, instead of overwrites it).

The next echo command prints when the job has ended to the end of the log file, as before (note the use of >> to append, rather than overwrite). Finally, cat /mnt/disk/backuprsync.log | ssmtp someaddress@gmail.com takes the log file and emails it to me using ssmtp. I had to use this as the OS I am using on the NSLU2 doesn’t have its own mail command. There will be more about setting up ssmtp in another post.

Finally, you’ll want to run this job via cron, so, add

01 1 3 * * root /etc/rsync/backuprsync.sh

To your /etc/crontab file. Ensure that your crontab file knows to use the sh shell, so it should have SHELL=/bin/sh at the beggining of your crontab file. This crontab entry makes cron run the script at 01.13am on the 3rd day of every month.

If all goes to plan, rsync will connect to your server’s rsync daemon, suck all the changed files from last time to the NSLU2 and then email you the results. Sweet.

Advertisements

Actions

Information

One response

20 11 2008
The NSLU2 is awesome | pruperting

[…] have already mentioned Linksys’ NSLU2 in a previous post and I thought I should go in some more detail about this great little product. As mentioned, it is […]

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s