rendered paste body#!/usr/bin/perl
use Net::Ping;
#hosts.txt example:
#hostA /boot:boot /etc:etc
#hostB /boot:boot /etc:etc /usr/local:usrlocal
$backup_dir = "/backup";
&main;
sub main {
&begin;
$host_file = "<$backup_dir/hosts.txt";
open HOSTS, $host_file;
while (<HOSTS>) {
#get a list of hosts from first field in hosts.txt
@entry = split(/ /, $_);
$host = @entry[0];
chomp $host;
# Check if host is alive
unless (&host_up($host)) {
printf "\n%2s %15s %10s %-10s\n", " ", "Host:", "\'$host\'", "is up.";
printf "%2s %15s\n", " ", "Proceeding:";
@entry = reverse(@entry);
pop @entry;
mkdir "$backup_dir";
mkdir "$backup_dir/$host";
mkdir "$backup_dir/$host/live";
foreach (@entry) {
@single = split(/:/, $_);
$dir = $single[0];
$file = $single[1];
printf "%7s %20s", " ", "$dir:";
if (&pullit($host, $dir, $backup_dir, $file)) {
printf "%10s\n", "Fail";
} else {
printf "%10s\n", "Success";
}
}
}
}
&end;
}
sub pullit {
my $host = $_[0];
my $rem_dir = $_[1];
my $loc_dir = $_[2];
my $file = $_[3];
chomp($file); chomp($host); chomp($rem_dir); chomp($loc_dir);
system("rsync",
"--delete",
"-apce",
"ssh",
"$host:$rem_dir/",
"$loc_dir/$host/live/$file");
}
sub host_up {
my $host = $_[0];
chomp $host;
$p = Net::Ping->new("icmp");
if ($p->ping("$host")) { $p->close(); return 0; } else { $p->close(); return 1; }
}
sub begin {
print "\n\n";
printf "%2s %10s\n", " ", "###################";
printf "%2s %10s\n", " ", "## S Y N C I N G ##";
printf "%2s %10s\n", " ", "## R E M O T E ##";
printf "%2s %10s\n", " ", "## B A C K U P S ##";
printf "%2s %10s\n", " ", "###################";
}
sub end {
print "\n\n";
}