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

Thursday, March 26, 2009

Customizing Your Login Message With /etc/issue

If you are bored with same login message at Linux console,
you can change it by changing /etc/issue file.

# clear > /etc/issue
# echo "This is Mitesh's Machine." >> /etc/issue

Even you can provide other details of your system, by using
single alphabet given here.

# echo "\d \m \s \v" >> /etc/issue

Fortune Status for Pidgin IM

If you want to change status message of Pidgin Message by
fortune message. Just create a file pidgin_status_message.py
And copy the following lines.

#----------------------------

#!/usr/bin/env python

import dbus,subprocess,time

def set_status(message):
  bus = dbus.SessionBus()
  obj = bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
  purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")
  current = purple.PurpleSavedstatusGetType(purple.PurpleSavedstatusGetCurrent())
  status = purple.PurpleSavedstatusNew("", current)
  purple.PurpleSavedstatusSetMessage(status, message)
  purple.PurpleSavedstatusActivate(status)

while
True:
  fortune = subprocess.Popen('fortune', stdout = subprocess.PIPE).stdout.read()
  set_status(fortune)
  time.sleep(120)
#-------------------------------

Start Pidgin and give following command:
$ python pidgin_status_message.py &