Simple Ubuntu Server: Part 4a – Backing Up Your Machine with Rsync

Before we get too far into setting everything up, its a good idea to have at least one backup of your system (and preferably an up-to-date copy!) in case anything happens to your machine. From a hard drive failure or typing a wrong command that blasts your system to your house or office burning down, its always a good idea to have a backup or three or seven.

There are almost as many ways to backup your system as there are partitioning schemes, two of which I’ll cover here. The first part (4a) covered here, will use rsync to copy all your files to a different device (local hard drive, ftp site, etc.), the second part (4b) will use Clonezilla to create an image of your hard drive or an individual partition.

Lets get started! Rsync at its most basic is a file copying tool. The first thing you will need is a location to copy the files to. I’m assuming you know how to mount drives, but in case you don’t I’ll give you a small primer. Note that it is very important that the backup device you use needs to be in a format that saves permissions (i.e. ext3/4, not NTFS)

First create the mount point for your backup drive:

sudo mkdir /backup/directory

then use the following command to list the UUIDs for the device you want to mount:

sudo blkid

then open /etc/fstab for editing:

sudo vi /etc/fstab

and add the appropriate parameters for your device (below is assuming the device is ext4 formatted). This will automatically mount your device at login. If you don’t want to automatically mount it, then simply use the mount and umount command insteads which are shown right below the /etc/fstab entry

/etc/fstab entry

/etc/fstab entry

sudo mount /dev/sda0 /backup/directory
sudo umount /dev/sda0
-or-
sudo umount /backup/directory

To mount all the entries in /etc/fstab you can issue the following command

sudo mount -a

To ensure the device is mounted, you can issue the command and read the output, checking for your device

mount

Now onto the actual rsync backing up portion. To copy a directory from one location to another, the command is pretty straight-forward

rsync /source/directory /backup/directory

There are a few options you will want to use when preforming backups, namely -a, -z, -v, and optionally, –delete-before and –dry-run.

The -a option preserves dates, times and permissions, the -z option compresses the files to save space, and the -v option makes rsync more verbose (remember to turn this option off if you make a crontab entry)

The –dry-run option will allow you to preform the exact command, but not actually write anything, only ensure everything is working as it should. The –delete-before option will delete files from the /backup/directory that don’t exist in the /source/directory. Keep in mind that it is a very good idea to have multiple (rotating weekly, daily or incremental) backups so that if you need to restore an accidentally deleted directory you are able too.

You can optionally use –exclude-from=/path/to/exclude_directories_list to exclude directories like /proc, /sys, /dev, etc. An important directory to exclude if you are backing up your root directory is the actual backup destination itself!

Finally, remember to use sudo to ensure the root directories that the logged in user may not have permission to read.

The final command will look similar to this backing up from the root directory:

sudo -azv --delete-before --exclude-from=/home/seawolf167/exclude-list / /media/backup_directory

The contents of my exclude-list (note that my backup_directory is mounted in /media which is part of the excluded directories) looks like this:

Exclude-list contents

Exclude-list contents

You can add this command directly to crontab or create an executable file that crontab points to. The following will run every morning at 1am

crontab entry

crontab entry

That’s pretty much it for rsync. Of course you can backup to a network storage location or to/from a webserver using ssh, you just have to use a slightly modified rsync command. For more rsync information, you can read its man page

man rsync

which contains lots of examples and dozens upon dozens of optional switches.

In the next part (4b), I’ll cover backing up your system using Clonezilla, a software which has a few strengths (which mainly pertain to multiple operating systems on the same computer since the software creates device and/or partition images) and weaknesses (since you have to reboot into the Clonezilla live cd in order to run the software) over rsync.

Tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.

Protected by WP Anti Spam