terça-feira, 21 de junho de 2016

Mikrotik - backup files usando ftp server

Automated RouterOS Backup to FTP 

After I crashed my MikroTik router with a firmware upgrade (ROS 6.4 to 6.6) I lost quite a part of the configuration because I did not have an up to date backup. This experience led to an automated RouterOS backup so I won’t end up in a situation like this a second time.

After I got everything fixed, I went straight to the task of automating the complete router configuration (including ROS backup, the user manager database and an export of the configuration).


UPDATE: Script modified so the FTP server needs only to be configured once and added some more logging (2014-01-08).
Scheduler for the automated RouterOS backup.
Scheduler for the automated RouterOS backup.
The idea was to have a scheduled script push all three backup files to my NAS, which can be reached via FTP for this task. I searched the web and found a good forum post about Backup to External FTP. I slightly modified the script, added the conversion of the date string to the YYYY-MM-DD format and added the exported configuration part.
Now once per night this little helper saves all necessary stuff to my NAS, so next time I have to recover such an incident, I will have my latest configuration at hand.
Note: The backup contains passwords and other sensitive information so you have to make sure that you don’t compromise your network by sending these backups across unsafe network links or store them on a server which can be accessed by others!

RouterOS Script

# automated backup 2 External ftp

# ftp configuration
:local ftphost "<>"
:local ftpuser "<>"
:local ftppassword "<>"
:local ftppath "<>"

# months array
:local months ("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");

# get time
:local ts [/system clock get time]
:set ts ([:pick $ts 0 2].[:pick $ts 3 5].[:pick $ts 6 8])

# get Date
:local ds [/system clock get date]
# convert name of month to number
:local month [ :pick $ds 0 3 ];
:local mm ([ :find $months $month -1 ] + 1);
:if ($mm < 10) do={ :set mm ("0" . $mm); }
# set $ds to format YYYY-MM-DD
:set ds ([:pick $ds 7 11] . $mm . [:pick $ds 4 6])

# file name for user manager backup - file name will be UMDB-servername-date-time.umb
:local fname ("/UMDB-".[/system identity get name]."-".$ds."-".$ts.".umb")
# file name for system backup - file name will be UMDB-servername-date-time.backup
:local fname1 ("/UMDB-".[/system identity get name]."-".$ds."-".$ts.".backup")
# file name for config export - file name will be UMDB-servername-date-time.rsc
:local fname2 ("/UMDB-".[/system identity get name]."-".$ds."-".$ts.".rsc")

# backup the data
/tool user-manager database save name=$fname
:log info message="User manager backup finished (1/3).";
/system backup save name=$fname1
:log info message="System backup finished (2/3).";
/export compact file=$fname2
:log info message="Config export finished (3/3)."

# upload the user manager backup
:log info message="Uploading user manager backup (1/3)."
/tool fetch address="$ftphost" src-path=$fname user="$ftpuser" mode=ftp password="$ftppassword" dst-path="$ftppath/$fname" upload=yes
# upload the system backup
:log info message="Uploading system backup (2/3)."
/tool fetch address="$ftphost" src-path=$fname1 user="$ftpuser" mode=ftp password="$ftppassword" dst-path="$ftppath/$fname1" upload=yes
# upload the config export
:log info message="Uploading config export (3/3)."
/tool fetch address="$ftphost" src-path=$fname2 user="$ftpuser" mode=ftp password="$ftppassword" dst-path="$ftppath/$fname2" upload=yes

# delay time to finish the upload - increase it if your backup file is big
:delay 60s;
# find file name start with UMDB- then remove
:foreach i in=[/file find] do={ :if ([:typeof [:find [/file get $i name] "UMDB-"]]!="nil") do={/file remove $i}; }
:log info message="Configuration backup finished.";
fonte: http://harry.subnetworx.de/category/operating-system/routeros/ 

0 comentários: