Community discussions

MikroTik App
 
ParisDragon
newbie
Topic Author
Posts: 32
Joined: Wed May 24, 2006 9:52 pm
Location: NorthEast Texas, USA
Contact:

FTP Backups possible?

Thu May 25, 2006 11:13 pm

I have many scripts that let me email my current config to an email address, is it not possible to modify this script to FTP the config to an ftp server for backup instead?
 
Wyoming
Member Candidate
Member Candidate
Posts: 124
Joined: Wed Jun 09, 2004 11:43 pm
Location: Wyoming

Wed May 31, 2006 8:02 pm

I just used Perl to write a script that runs each night and logs into the router to create a backup file and then it ftps the file off the router to the server runing the perl script to a specified directory. So yes it is possible to write a script that will ftp backups off the router. I am not sure if you can write one on the router that will ftp the backup to a server.
 
nikhil
Member Candidate
Member Candidate
Posts: 262
Joined: Wed Dec 22, 2004 5:04 pm
Location: US

Thu Jun 01, 2006 9:25 am

Can you share the script with us ?
 
ParisDragon
newbie
Topic Author
Posts: 32
Joined: Wed May 24, 2006 9:52 pm
Location: NorthEast Texas, USA
Contact:

Thu Jun 01, 2006 5:15 pm

I just used Perl to write a script that runs each night and logs into the router to create a backup file and then it ftps the file off the router to the server runing the perl script to a specified directory. So yes it is possible to write a script that will ftp backups off the router. I am not sure if you can write one on the router that will ftp the backup to a server.
Yeah I already have it backing itself up nightly and then a remote client logging in and downloading it. Just the more radios you deploy the worst it gets for scheduling the client do log into each of them. I am hopeful they will add the ability to issue FTP commands directly from the Mikrotik as you can in linux so each new box can then make its backup with the date as part of the name. The client needs a static name.
 
nikhil
Member Candidate
Member Candidate
Posts: 262
Joined: Wed Dec 22, 2004 5:04 pm
Location: US

Thu Jun 01, 2006 5:27 pm

I have the code below emailing me but I would like an FTP backup instead !

/system script add name=ebackup source={/system backup save name=([/system identity get name] . "-" . [:pick [/system clock get date] 7 11] . [:pick [/system clock get date] 0 3] . [:pick [/system clock get date] 4 6]); /tool e-mail send to="x@x.com" subject=([/system identity get name] . " Backup " . [/system clock get date]) file=([/system identity get name] . "-" . [:pick [/system clock get date] 7 11] . [:pick [/system clock get date] 0 3] . [:pick [/system clock get date] 4 6] . ".backup"); :delay 10; /file rem [/file find name=([/system identity get name] . "-" . [:pick [/system clock get date] 7 11] . [:pick [/system clock get date] 0 3] . [:pick [/system clock get date] 4 6] . ".backup")]; :log info ("System Backup emailed at " . [/sys cl get time] . " " . [/sys cl get date])}

/system script add name=eexport source={/export file=([/system identity get name] . "-" . [:pick [/system clock get date] 7 11] . [:pick [/system clock get date] 0 3] . [:pick [/system clock get date] 4 6]); /tool e-mail send to="x@x.com" subject=([/system identity get name] . " Export " . [/system clock get date]) file=([/system identity get name] . "-" . [:pick [/system clock get date] 7 11] . [:pick [/system clock get date] 0 3] . [:pick [/system clock get date] 4 6] . ".rsc" ); :delay 10; /file rem [/file find name=([/system identity get name] . "-" . [:pick [/system clock get date] 7 11] . [:pick [/system clock get date] 0 3] . [:pick [/system clock get date] 4 6] . ".rsc")]; :log info ("System Export emailed at " . [/sys cl get time] . " " . [/sys cl get date])}
 
Wyoming
Member Candidate
Member Candidate
Posts: 124
Joined: Wed Jun 09, 2004 11:43 pm
Location: Wyoming

Fri Jun 02, 2006 12:56 am

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.
 
lrhea
just joined
Posts: 2
Joined: Sun May 21, 2006 7:43 am
Location: Paris Texas
Contact:

Tue Jul 18, 2006 7:50 pm

Much appreciated, the main reason I want it running on the Mikrotik itself is so I dont need to continually edit/update a script running on a machine. Also I already have FTP servers setup and running without dedicating a client to schedule etc. Hopefully they will look into adding it.
 
nikhil
Member Candidate
Member Candidate
Posts: 262
Joined: Wed Dec 22, 2004 5:04 pm
Location: US

Tue Jul 18, 2006 9:39 pm

actually the script is not working with 2.9.27 that i use. Using windows perl (activestate) with the components.
 
mperdue
Member Candidate
Member Candidate
Posts: 292
Joined: Wed Jun 30, 2004 8:18 pm

Tue Jul 18, 2006 10:19 pm

Whats the point of doing a backup of a mikrotik unit if the backup can not be moved to another unit?

If you save a backup, and then your router goes dead. You try to restore your items to a new unit it won't work.
 
csickles
Forum Guru
Forum Guru
Posts: 1255
Joined: Fri May 28, 2004 8:46 pm
Location: Phoenix, AZ
Contact:

Tue Jul 18, 2006 10:45 pm

Yes there are usualy some tweaks to make after the restore, but it usualy saves a large amount of time.

Craig
 
Syonyk
Member Candidate
Member Candidate
Posts: 109
Joined: Mon Feb 14, 2005 6:32 pm
Location: Coralville, IA
Contact:

Wed Jul 19, 2006 11:53 pm

Many things work properly when restoring a backup. IP addresses, routes, firewall rules, queues, etc.

The major thing that doesn't work properly is the interfaces, especially wireless. You'll need to reconfigure the interfaces & assign IPs to each interface, but this is usually a lot easier than redoing all the configs from scratch.

You can also export the settings as a setup file - the 'export' command will export all config stuff to a file that can then be applied to another router.

-=Russ=-
 
nikhil
Member Candidate
Member Candidate
Posts: 262
Joined: Wed Dec 22, 2004 5:04 pm
Location: US

Thu Jul 20, 2006 8:46 am

Its because MT ties everything to the interface name which in turn seems to be tied into a mac-address and since this is unique. If you export and update the mac's it comes up fine