Sunday, January 16, 2011

Script of the Week: sync.pl

I've started experimenting with gist for storing and sharing snippets of code online. Below is a summary of a script that I use to synchronize directories across different machines using rsync.


#!/usr/bin/perl
use strict;
use warnings;
use File::Path;
use Getopt::Long;
use File::Basename;
my $push = 0;
my $hostname = 'dig';
&GetOptions(
'push' => \$push,
'host=s' => \$hostname
);
my $pwd = $ENV{PWD};
my $home = $ENV{HOME};
my $dig_wd = $pwd;
$dig_wd =~ s#$home/##g;
my $cmd;
if ( !$push ) {
execute_cmd( "rsync -azv $hostname:$dig_wd/* ." );
} else {
execute_cmd( "ssh $hostname 'mkdir -p $dig_wd'" );
execute_cmd( "rsync -azv . $hostname:$dig_wd/" );
}
sub execute_cmd {
my $cmd = shift;
print $cmd, "\n";
system($cmd);
}
view raw sync.pl hosted with ❤ by GitHub