Thursday, October 18, 2007

perl script for ftp backup



root@srvsun01 # cat /usr/scripts/backup.pl
#!/usr/bin/perl
use Net::FTP;
#my $ftp = Net::FTP->new("172.16.39.7");
#$ftp->login("backupadmin@foo.bar.edu","foobar");
my $host="172.16.39.7";
my $directory="pub";
($a,$d,$d,$day,$month,$yearoffset,$r,$u,$o) = localtime();
$year = 1900 + $yearoffset;
$backupdate= "$day-$month-$year";
print $backupdate;
$ftp=Net::FTP->new($host,Timeout=>240) or $newerr=1;
push @ERRORS, "Can't ftp to $host: $!\n" if $newerr;
myerr() if $newerr;
print "Connected\n";

$ftp->login("backupadmin@foo.bar.edu","foobar") or $newerr=1;
print "Getting file list";
push @ERRORS, "Can't login to $host: $!\n" if $newerr;
$ftp->quit if $newerr;
myerr() if $newerr;
print "Logged in\n";

$ftp->binary;
$ftp->put("backup-$backupdate.tar.gz");
$ftp->quit;

No comments: