#!/usr/bin/perl # our needed packages use strict "vars"; use Getopt::Std; my $VERBOSE = 0; my $DEBUG = 0; my $SIMULATE = 0; # Global Variables my $ITERATIONS = 3; my $CONFIG; # add support for config file my $opt_string = "vdhsc:"; getopts( "$opt_string", \my %opt) or usage() and exit(1); $VERBOSE = 1 if $opt{'v'}; $DEBUG = 1 if $opt{'d'}; if ( $opt{'h'} ){ usage(); exit 0; } $SIMULATE = $opt{s}; $CONFIG = $opt{c}; usage() and die("you have to supply a config file. please.\n") unless $CONFIG; # $LOCAL = $HOST unless $LOCAL; # debug("Checking if host is up and folder is present\n"); # die("The remote host could not verify the directory $DIR or is down. Exiting.\n") unless verify_host($HOST,$DIR,$USER); ########################################################## # main part # for all hosts: ( also for every line) open(CONF,"$CONFIG") or die("unable to open config $CONFIG: $!\n"); while( my $line = ){ my $HOST; my $LIST; if ( $line =~ /(\s+):(.*)/ ){ $HOST = $1; $LIST = $2; debug("read line with host: '$HOST' and dirs: '$LIST'\n"); } else { next; } my $LOCAL = $HOST; # remove the oldest if number equals $ITERATIONS if ( stat($LOCAL . "." . $ITERATIONS) ){ run_command("rm -rf " . $LOCAL . "." . $ITERATIONS); } # rotate the folders from 1 until the end, backwards for ( my $i = ( $ITERATIONS -1); $i >= 1; $i--){ if ( stat($LOCAL . "." , $i)){ run_command("mv " . $LOCAL . "." . $i . " " . $LOCAL . "." . ( $i + 1)); } } # copy with hard links base folder to .1 run_command("cp -al " . $LOCAL . " " . $LOCAL . ".1"); # execute rsync # for every folder my @FOLDERS = split /,/,$LIST; foreach my $DIR ( @FOLDERS ){ run_command("rsync -a -v --delete -e ssh $HOST:$DIR $LOCAL"); } } # debug("script is finished, exiting...\n"); # exit 0; ################################################## sub usage { print "usage:\n"; print "-h for help\n"; print "-v for verbose ( more output )\n"; print "-d for debug ( even more output)\n"; print "-s simulate only , do not execute\n"; print "----\n"; print "-c in order to supply the backup file\n"; } sub verbose { print "VERBOSE: " . $_[0] if $VERBOSE or $DEBUG ; } sub debug { print "DEBUG: " . $_[0] if $DEBUG ; } # sub verify_host { # my $host = $_[0]; # my $dir = $_[1]; # my $user = $_[2]; # $host = $user . "@" . $host if $user; # open(SSH,"ssh $host stat $dir |"); # while ( my $line = ){ # if ( $line =~ /Modify: / ){ # close(SSH); # return 1; # } # } # close(SSH); # return 0; # } sub run_command { my $command = $_[0]; debug("EXEC: " . $command . "\n"); system($command) unless $SIMULATE; }