Today … Useful script for Automatic backup for System + UserManagerDB …
Daily… new file name by time and date!!!
Daily… upload to External FTP!!!
Daily… Clean!!! Save the space … No duplicate in Mikrotik!!!
First the script:
/system script
add name=FTP-Backup policy=ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive source="# automated System - UM database backup 2 External FTP\r\
\n# Get time\r\
\n:local ts [/system clock get time]\r\
\n:set ts ([:pick \$ts 0 2].[:pick \$ts 3 5].[:pick \$ts 6 8])\r\
\n# Get Date\r\
\n:local ds [/system clock get date]\r\
\n:set ds ([:pick \$ds 7 11].[:pick \$ds 0 3].[:pick \$ds 4 6])\r\
\n# This line to generate the file name for user manager backup - file name will be UMDB- ServerName\? - Date\? - Time\?\r\
\n:local fname (\"/UMDB-\".[/system identity get name].\"-\".\$ds.\"-\".\$ts.\".umb\")\r\
\n# This line to generate the file name for System backup - file name will be UMDB- ServerName\? - Date\? - Time\?\r\
\n:local fname1 (\"/UMDB-\".[/system identity get name].\"-\".\$ds.\"-\".\$ts.\".backup\")\r\
\n# all above is workaround to get unique \$filename=UserManagerDB - \$filename1=System\r\
\n/tool user-manager database save name=\$fname\r\
\n:log info message=UserManagerBackupFinished;\r\
\n/system backup save name=\$fname1\r\
\n:log info message=SystemBackupFinished;\r\
\n# Upload the UserManagerDB Backup to External FTP - change address to your ftp server + user + pass\r\
\n/tool fetch address=www.1.com src-path=\$fname user=user mode=ftp password=pass dst-path=/ upload=yes\r\
\n# Upload the System Backup to External FTP - change address to your ftp server + user + pass\r\
\n/tool fetch address=www.1.com src-path=\$fname1 user=user mode=ftp password=pass dst-path=/ upload=yes\r\
\n# Delay time to finish the upload - increase it if your backup file is big\r\
\n:delay 60s;\r\
\n# Find file name start with UMDB- then remove\r\
\n:foreach i in=[/file find] do={:if ([:typeof [:find [/file get \$i name] \"UMDB-\"]]!=\"nil\") do={/file remove \$i}}\r\
\n:log info message=UserMangerBackup-SystemBackup-TempRemoved;\r\
\n:log info message=UserManger-SystemBackup2CentOS-Finished;"
Don’t forget to change the address to your ftp server in + user + pass:
1 - /tool fetch address=www.1.com src-path=$fname user=user mode=ftp password=pass dst-path=/ upload=yes
2 - /tool fetch address=www.1.com src-path=$fname1 user=user mode=ftp password=pass dst-path=/ upload=yes
The last step is to configure the scheduler to execute the above script at a given time interval. The following command makes sure that the script is executed every day:
Hi, I have installed your script but it isn’t working.
When I try to run script it isn’t working, nothing in log. I put my addres, user and password for ftp but it isn’t working When I remove:
_---------------------------------------------------------------------------------------------
Upload the UserManagerDB Backup to External FTP - change address to your ftp server + user + pass
Delay time to finish the upload - increase it if your backup file is big
:delay 60s;
---------------------------------------------------------------------------------------------_ it is working.
Log tels me that system backub and usermanager backup has been finished.
After that backup has been removed automatically.
Yes it’s removed the backup file from mikrotik after upload it to ftp server…
1- Generate backup for UserManager temporary.
2- log info message=UserManagerBackupFinished.
3- Generate backup for system temporary.
4- log info message=SystemBackupFinished.
5- Upload the UserManagerDB Backup to FTP server.
6- Upload the System Backup to External FTP server.
7- remove Temporary back file UserManager + system.
8- log info message=UserMangerBackup-SystemBackup-TempRemoved.
9- log info message=UserManger-SystemBackup2CentOS-Finished “FTP server”
I modified the whole stuff a little bit, e.g. beautified the date-string to represent YYYY-MM-DD format and added an /export file to the backup so one gets a human readable format as well.
Thanks very much for the script ideas, I was that impressed, I thought I would make myself a version of this to save locally onto my microSD and instead reinventing the wheel I decided to use some of your existing code and adjusted it to my purpose. Credit goes to the original author and if you want to be kind, a little credit can goto me for for the changes
This version doesn’t save any files to the internal flash to save ware and tare on the finite resource. Please beware that you need to change the fbase path to wherever your base dir is going to be to save your backups and this directory needs to be created before hand as there is no check currently in here to create if it doesn’t already exist.
I hope some people find this useful for all those routers which have external storage such as the RB750Gr3.
Hello i updated the script im using the v6.45.1
Change this on the script and set the scheduler to run the script
replace usr to the username
replace passwd to password
replace > www.ftp.com > to the FTP server
Add to a new script and give permisions
#Get Time and Date
:local ts [/system clock get time]
:set ts ([:pick $ts 0 2].[:pick $ts 3 5].[:pick $ts 6 8])
:local ds [/system clock get date]
:set ds ([:pick $ds 7 11].[:pick $ds 0 3].[:pick $ds 4 6])
# This line to generate the file name for System backup
:local fname1 ("CCR-FTP--".$ds."-".$ts.".backup")
/system backup save name=$fname1;
#uncomment to send also by email (configure the tool email in your MK with valid smtp)
#/tool e-mail send to="email@domain.com" subject=("mikrotik backup") body="Respaldo Historico CloudCore" file=$fname1;
#post to root of the ftp server
/tool fetch address=www.ftp.com src-path=$fname1 user=usr mode=ftp password=passwd dst-path=$fname1 upload=yes
:log info "Backup Done";
#Process the Backup
/system backup save name=$fname1;
:log info message=ConfigBackupFinished;
/user export file=$fname2;
:log info message=UserManagerBackupFinished;
#Upload to FTP Server
/tool fetch address=DOMAIN/IP src-path=$fname1 user=username mode=ftp password=password dst-path=$fname1 upload=yes
/tool fetch address=DOMAIN/IP src-path=$fname2 user=username mode=ftp password=password dst-path=$fname2 upload=yes
:log info “Backup Uploaded”;
#Find “MHOME” and remove.
foreach i in=[/file find] do={if ([:typeof [:find [/file get $i name] “ROUTER IDENT-”]]!=“nil”) do={/file remove $i}}
:log info message=UserMangerBackup-ConfigBackup-TempRemoved;