3 Comments

The Beauty of rsync and Backup Script

rsync is a command-line tool used to copy/clone files (“fast incremental file transfer“). It is a great, simple backup tool. The basic rsync command is this:

rsync -a src dest_dir

Where src is the original directory or file and dest_dir is the destination directory. Because rsync does incremental backups it only adds the file to the dest if it has been updated from the original backup.

rsync -axS src dest_dir

This is the command I use. This command can be used to backup just about anything! The options:

  • -a means archive mode which basically means to preserve the file “as is” (same permissions…)
  • -x means not to cross file systems boundaries
  • -S means to handle sparse files efficiently
  • -v option (verbose) can be used to print what rsync is doing

An important note about rsync: when src is a directory a trailing slash (/) tells rsync to copy the “contents” of the directory:

rsync -axS src_dir/ dest_dir
ls -1 dest_dir/
 file1
 file2

Without a trailing slash:

rsync -axS src_dir dest_dir
ls -1 dest_dir
 src_dir

rsync can also use file-lists containing paths of directories and files, to both include and exclude them for backup:

sudo rsync -axS --files-from="incl_file.txt" --exclude-from="excl_file.txt" src_dir dst_dir

src_dir will have to be specified and will have to be relative to paths in the file list:

cat incl_file.txt
Desktop/
rsync -axS --files-from="incl_file.txt" --exclude-from="excl_file.txt" /home/user/ dst_dir

rsync can also remove files from the dest_dir with the --delete option, so files that get added to the exclude file or taken out of the include file will removed from dest_dir.

rsync -axS --delete-excluded --files-from="incl_file.txt" --exclude-from="excl_file.txt" /home/user/ dst_dir

Backup Script

I use rsync to backup my system configurations and /home/ to make reinstalling easy. I created the script to remember the command to use, but to also easily add to the include and exclude files:

bcksysc i /etc/hostname 
 Added "/etc/hostname" to bcksysc-inc.txt include file.

Syntax:

bcksysc 
 bcksysc  - backup configurations
 i - add to the include list a file or folder
 e - add to the exclude list a file or folder
 c - create backup

Here’s the script all that needs to be done is to change the Parent Destination Directory (for backing up /home/ I copied the script to bckhome, changed the type to home and added /home/ to the include file):

So my destination directory looks like this:

ls -1 /run/media/todd/Backup/rsync/
 ...
 aspire_2012-08-31_sysc
 aspire_2012-08-31_home
About these ads

About Todd Partridge (Gently)

Good times, good people, good fun.

3 comments on “The Beauty of rsync and Backup Script

  1. Duplicity is just as easy to use, but provides incremental backups and encryption as well. It will also plug into stuff like Amazon S3 if you want it to: https://grepular.com/Secure_Free_Incremental_and_Instant_Backups_for_Linux

  2. you can also see my own rsync shellscript with smart rotation (non-linear distribution) of backups, integrity protection with md5sum&chattr, Quotas, Filter-Rules and many more features.
    http://blog.pointsoftware.ch/index.php/howto-local-and-remote-snapshot-backup-using-rsync-with-hard-links/

Leave a Reply

Fill in your details below or click an icon to log in:

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

Follow

Get every new post delivered to your Inbox.

Join 52 other followers