Sunday, March 29, 2009

Backup Using rsync

There are many backup tools, but most are not as configurable as
rsync command. But still we may want more easiness to use rsync.
Therefore, I have written a Perl Script to use rsync for backing up
as many directories as you want. Here is a Perl script:


#!/usr/bin/perl
#===============================================================================
#
# FILE: rsync_backup.pl
#
# USAGE: ./rsync_backup.pl [output_dir]
#
# DESCRIPTION:
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Mitesh Singh Jat (mitesh), <mitesh[at]yahoo-inc[dot]com>
# VERSION: 1.0
# CREATED: Saturday 28 March 2009 07:36:54 IST IST
# REVISION: ---
#===============================================================================

use strict;
use warnings;

my $rsync_include = "rsync_include.conf";
my $rsync_exclude = "rsync_exclude.conf";
my $out_dir = "/media/FreeAgent Drive";
if (@ARGV >= 1)
{
$out_dir = $ARGV[0];
}

unless (-f $rsync_include)
{
print STDERR "$0: Include config file $rsync_include is not present.";
exit(-1);
}
my $exclude_option = "--exclude-from=$rsync_exclude";
unless (-f $rsync_exclude)
{
print STDERR "$0: exclude config file $rsync_exclude is not present.";
$exclude_option = "";
}
unless (-d $out_dir)
{
print STDERR "$0: $out_dir Destination dir does not exists.\n";
exit(-1);
}
$out_dir =~ s/ /\\ /g;

open(FH, "$rsync_include") or die("$0: Cannot open $rsync_include\n");
my $line;
my $nlines = 0;
while ($line = <FH>)
{
my $command;
my $options = "";
chomp($line);
++$nlines;
if ($line =~ /^#/) # skip comments
{
next;
}
my @fields = split(//, $line); # this junk char is Ctrl-X
if (@fields < 3)
{
print "Not enough arguments in line $nlines\n";
next;
}
if ($fields[2] =~ /^[Yy]/) # sync
{
$options .= " --delete --delete-excluded";
}
if (defined($fields[3]))
{
$options .= " $fields[3]";
}

$fields[0] =~ s/<home>/\/home\/mitesh/;
$fields[1] =~ s/<home>/\/home\/mitesh/;
$fields[0] =~ s/<out_dir>/$out_dir/g;
$fields[1] =~ s/<out_dir>/$out_dir/g;

$command = "rsync -av $fields[0] $fields[1] $options ";
system($command) == 0 or die("$0: Error in running\n$command\n$!\n");
}
close(FH);
exit(0);



There are 2 configuration files, I have used for this script. Namely
1. rsync_include.conf
2. rsync_exclude.conf

Please place these 2 files in the directory, from where you are going
to run this script, else you can change path of these configuration
files in rsync_backup.pl .

Sample rsync_include.conf contains:
# Configuration file for backing up
#
# ^X is delimiter, which can be typed as Ctrl-V Ctrl-X
#src_folder^Xdest_folder^Xdelete_at_dest[Y/n]^Xother_rsync_options
#
## delete_at_dest =>
# y = sync // Use cautiously
# n = copy blindly
#
# Some tags that can be used:
## <home> = Home directory; change to your home directory in the script.
## <out_dir> = Common Destination Directory
#
/home/mitesh/Music^X<out_dir>^Xy^X
<home>/Documents^X<out_dir>^Xn^X
<home>/Programming^X<out_dir>^Xn^X
/home/mitesh/Videos^X<out_dir>^Xn^X
#/workspace/temp/*^X<out_dir>/temp^Xn^X
/mnt/extra/Extra/Films/*^X<out_dir>/Videos^Xn^X
/mnt/extra/Extra/Download/*^X<out_dir>/Videos^Xn^X

Sample rsync_exclude.conf contains:
.trash
Trash
Cache
.thumbnails
*.torrent

Sample run (I have placed above config files in ~/bin/):
$ cd ~/bin
$ ./rsync_backup.pl
...
or, if you want to use some backup directory on harddisk,
mounted on /media/disk (say, /media/disk/backup)
$ ./rsync_backup /media/disk/backup

No comments: