backup.pl:
#!/usr/bin/perl -W
use Net::Telnet;
use Net::FTP;
use Mail::Sendmail;
use Benchmark;
$workdir = "/somedir/backup/";
$time1=new Benchmark;
$count=1;
($Second, $Minute, $Hour, $Day, $Month, $Year, $Weekday, $DayOfYear, $IsDST) = localtime(time);
$RealMonth = $Month + 1;
if ($RealMonth<10)
{
$RealMonth = "0" . $RealMonth;
}
if ($Day < 10)
{
$Day = "0" .$Day;
}
if ($Hour < 10)
{
$Hour = "0".$Hour;
}
if ($Minute < 10)
{
$Minute = "0" .$Minute;
}
if ($Second < 10)
{
$Second= "0".$Second;
}
$Fixed_Year = $Year +1900;
open(CFG, "$workdir/Backups.cfg") or (print "Can't open config file\n");
while($line = <CFG>)
{
if($line =~ /#.*/)
{
}
else
{
if($line=~ /USR::/i)
{
($junk,$username)=split("::",$line);
chomp $username;
}
if($line=~ /PWD::/i)
{
($junk,$password) = split("::",$line);
chomp $password;
}
if($line =~ /Email::/i)
{
($junk,$Email) = split("::",$line);
chomp $Email;
}
if($line =~ /ListFile::/i)
{
($junk,$LF) = split("::",$line);
chomp $LF;
}
if($line =~ /Dir::/i)
{
($junk,$DIR) = split("::",$line);
chomp $DIR;
#print "$DIR\n";
}
if($line =~ /Log::/i)
{
($junk,$Log) = split("::",$line);
chomp $Log;
#print "$Log\n";
}
}
}
close(CFG);
open(logfile, ">>$workdir/$Log");
open(FH, "$workdir/$LF") or (print logfile "Can't open the list file\n");
#open(logfile, ">>$workdir/$Log");
print logfile "\n";
print logfile "$Fixed_Year/$RealMonth/$Day/$Hour:$Minute:$Second\n";
system("rm -rf $workdir/failed_backup.txt");
$i = 0;
loop:
while($line = <FH>)
{
Errmode=>'return';
Input_log=>"input.log";
if(($line =~ /^#.*/)||($line =~ /^\s$/))
{
}
else
{
$i = $i + 1;
($IP, $Name) = split(":", $line);
chomp($Name);
print logfile "#".$i."\n";
$t = new Net::Telnet ( Timeout=>30, Errmode=>'return');
print "$count\n";
$count++;
if($t->open($IP))
{
print "open $IP - Telnet\n";
$t->login($username, $password);
print "@output\n";
print "logged in\n";
$cmd = "file remove \[find name\~\"\\\\.backup\\\$\"\]";
$t->print($cmd);
$cmd = "file remove \[find name\~\"\\\\.rsc\\\$\"\]";
$t->print($cmd);
$cmd = "system backup save name=".$Name."_"."$Fixed_Year$RealMonth$Day\n\n";;
#print "$cmd\n";
$t->print($cmd);
sleep(2);
#@output = $t->waitfor('/>/i');
$cmd = "export compact file=".$Name."_"."$Fixed_Year$RealMonth$Day\n";
$t->print($cmd);
sleep(2);
$fullfile = $Name."_"."$Fixed_Year$RealMonth$Day.backup";
$Lfullfile = $DIR.$fullfile;
$fullfile1 = $Name."_"."$Fixed_Year$RealMonth$Day.rsc";
$Lfullfile1 = $DIR.$fullfile1;
$F = Net::FTP->new($IP, Debug=>1, Timeout=>120, Errmode=>'return');
if($F)
{
print "open $IP - FTP\n";
$F->login($username, $password)or goto loop;
$F->binary or goto loop;
$F->get($fullfile,$Lfullfile) or ((print logfile "Can't get $fullfile\n")and(goto loop));
$F->get($fullfile1,$Lfullfile1) or (( print logfile "Can't get $fullfile1\n")and(goto loop));
$F->quit;
print logfile $i . " - ";
print logfile $IP . " - ";
print logfile "OK\n";
sleep(3);
print "OK\n";
$cmd = "file remove \[find name\~\"\\\\.backup\\\$\"\]";
$t->print($cmd);
sleep(2);
$cmd = "file remove \[find name\~\"\\\\.rsc\\\$\"\]";
$t->print($cmd);
sleep(2);
$t->print('quit');
}
else
{
print logfile " FTP Error - $IP\n";
print "Error\n";
open(EL, ">>$workdir/failed_backup.txt") or (print logfile "Can't open the failed_backup.txt\n");
print EL "\n" . " FTP Error - $IP\n";
$a='2';
}
}
else
{
print logfile "\n" . $i . " - " . $IP . " - Failed\n";
print "Error\n";
open(EL, ">>$workdir/failed_backup.txt") or (print logfile "Can't open the failed_backup.txt\n");
print EL "\n" . $i . " - " . $IP . " - Failed connect via Telnet\n";
$a='1';
# email("Telnet", $IP, $Email);
}
}
}
print $a;
if (($a=='1') or ($a=='2')) {
system(`mail -s \"Backup report\" mail\@domain.org\ < "$workdir/failed_backup.txt"`);
};
close(EL);
close(FH);
close(logfile);
sub email {
use strict 'refs';
my($refa, $refb, $Mailadd) = @_;
$Body = "$refa failed for $refb";
print "$Mailadd\n";
%mail = (
To => $Mailadd, From => 'mail@domain.org', Subject => 'Mikrotik Backup Failed' , Message => $Body
);
$mail{Smtp} = 'smtp.domain.org';
if(sendmail %mail) {print logfile "mail sent\n";}
else {print logfile "Error sending mail. $Mail::Sendmail::error \n";}
print "\n\$Mail::Sendmail::Log says: \n", $Mail::Sendmail::log;
}
$time2=new Benchmark;
$timebenchmark=timediff($time1, $time2);
print "Time", timestr($timebenchmark);
exit;
Backups.cfg:
USR::admin
PWD::somepass
Email::mail@domain.org
ListFile::router_list
Dir::/somedir/backup/
router_list:
1.1.1.1:GW01_1
2.2.2.2:GW02_1
3.3.3.3:GW03_1
Suggestions for improvement are welcome…