For this script I have 3 files. One is the actual script that runs. It is the .PL file. The next file is a configuration file. It stores the configurations for the script such as where the backups will be placed, passwords, etc... The last file is a list of the mikrotiks and the name of the backup in the format of IP:Name.
Example: 192.168.1.1:TestBackup
In the .PL file there are 3 extra perl modules you have to install into perl to make the script work. I used the Perl Packet Manager(PPM) to install them. They are listed at the start of the file. There are two modifications to this file that will need to be made. Towards the bottom there is a line that says enter your mail server here and enter the from address here. You need to enter your mail server name and an email address for the message to originate from.
Start of .PL file
#!/usr/bin/perl
#########################################################################################
# #
# MikroTik Backup Script 3-15-05 #
# Written by: Phillip Hutchison #
# #
#########################################################################################
#This Script needs the following Perl Modules installed on the machine that it is run on.
use Net::Telnet;
use Net::FTP;
use Mail::Sendmail;
# user variables
# Are Located in the Backups.cfg file.
# Code starts Here
sub email;
$count = 1;
($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $DayOfYear, $IsDST) = localtime(time);
$RealMonth = $Month + 1;
if($RealMonth < 10)
{
$RealMonth = "0" . $RealMonth; # add a leading zero to one-digit months
}
if($Day < 10)
{
$Day = "0" . $Day; # add a leading zero to one-digit days
}
$Fixed_Year = $Year + 1900;
open(CFG, "Backups.cfg") or (print "Can't open the config file\n");
while($line = <CFG>)
{
if($line =~ /#.*/)
{
}
else
{
if($line =~ /UN::/i)
{
($junk,$username) = split("::",$line);
chomp $username;
#print "$username\n";
}
if($line =~ /PW::/i)
{
($junk,$password) = split("::",$line);
chomp $password;
#print "$password\n";
}
if($line =~ /Email::/i)
{
($junk,$Email) = split("::",$line);
chomp $Email;
#print "$Email\n";
}
if($line =~ /ListFile::/i)
{
($junk,$LF) = split("::",$line);
chomp $LF;
#print "$LF\n";
}
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(FH, $LF) or (print logfile "Can't open the list file\n");
open(logfile, ">>$Log");
print logfile "$RealMonth/$Day/$Fixed_Year\n\n";
$i = 0;
while($line = <FH>)
{
if(($line =~ /^#.*/)||($line =~ /^\s$/))
{
}
else
{
$i = $i + 1;
($IP, $Name) = split(":", $line);
chomp($Name);
print logfile $i . " - ";
$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 = "system backup save name ".$Name;
#print "$cmd\n";
$t->print($cmd);
sleep(1);
@output = $t->waitfor('/.*>/i');
print logfile " @output\n";
$t->print('quit');
$fullfile = $Name.".backup";
$Lfullfile = $DIR.$fullfile;
$F = Net::FTP->new($IP);
if($F)
{
print "open $IP - FTP\n";
$F->login($username, $password);
$F->binary;
$F->get($fullfile,$Lfullfile) or print logfile "Can't get $fullfile\n";
$F->quit;
print logfile $i . " - ";
print logfile $IP . " - ";
print logfile "OK\n";
sleep(3);
print "OK\n";
}
else
{
print logfile " FTP Error - $IP\n";
print "Error\n";
email("FTP", $IP, $Email);
}
}
else
{
print logfile "\n" . $i . " - " . $IP . " - Failed\n";
print "Error\n";
email("Telnet", $IP, $Email);
}
}
}
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 => 'enter from address here', Subject => 'Mikrotik Backup Failed', Message => $Body
);
$mail{Smtp} = 'enter your mail server here';
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;
}
END .PL File
The next file is the .cfg file. It is named backups.cfg and is in the same directory as the .pl file.
start of .cfg file
#This file contains the configuration information for the Mikrotik backup
#Script. Lines that are blank or start with # will be ignored by the script.
#Username and Password to log into the Mikrotik with.
UN::admin
PW::enter your password here
#email address to send notification to when there is an error.
Email::enter your email here
#Name of the list file containing the IP addresses and Names of the mikrotiks.
#List file format is xxx.xxx.xxx.xxx:Name.
ListFile::list.txt
#Directory where the Backup files will be stored
Dir::D:\mikrotik\backups\
#Log filename
Log::backuplog.txt
End of .cfg file
The last file is a text file that is named List.txt. You will notice that this can be changed in the .cfg file if you want a different nameIt is a list of the IPs and the Name of the Backup file that will be created and FTPed back to the location specified in the .cfg file. The following are just examples you will need to replace them with your values. You can list as many IP and name combinations as you want. Just enter each mikrotik on a new line.
Start of .txt file
#list of router IPs and Names
192.168.0.1:testbackup1
172.16.1.1:testbackup2
If you have any more questions just ask and I will try to help you with them.