Page 1 of 1

Backup to External FTP Useful script

Posted: Tue Jun 07, 2011 11:28 am
by Alqatife
Hi,

Today .... 8) 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:
/system scheduler 
add comment="" disabled=no interval=1d name="System-UM DB- FTP backup" \
on-event=FTP-Backup start-date=jan/01/1970 start-time=00:00:00
Regardes,
Muntathar

Re: Backup to External FTP Useful script

Posted: Sun Jul 24, 2011 8:30 pm
by croskiper
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
/tool fetch address=www.1.com src-path=$fname user=user mode=ftp password=pass' dst-path=/ upload=yes
# Upload the System Backup to External FTP - change address to your ftp server + user + pass
/tool fetch address=www.1.com src-path=$fname1 user=user mode=ftp password=pass dst-path=/ upload=yes
# 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.

Thanks for help!!
Best regards..

Re: Backup to External FTP Useful script

Posted: Sat Sep 17, 2011 9:09 am
by Alqatife
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
/tool fetch address=www.1.com src-path=$fname user=user mode=ftp password=pass' dst-path=/ upload=yes
# Upload the System Backup to External FTP - change address to your ftp server + user + pass
/tool fetch address=www.1.com src-path=$fname1 user=user mode=ftp password=pass dst-path=/ upload=yes
# 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.

Thanks for help!!
Best regards..
Hi,

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"

Best Regard,

Re: Backup to External FTP Useful script

Posted: Wed Jul 31, 2013 9:29 pm
by kassiolm
works on v6.1?

Re: Backup to External FTP Useful script

Posted: Fri Aug 02, 2013 10:12 pm
by kassiolm
the script is creating the files but don't upload, need help!!!

Re: Backup to External FTP Useful script

Posted: Wed Aug 14, 2013 11:42 am
by JoePolgar
use dst-path=filename instead of dst-path=/

Re: Backup to External FTP Useful script

Posted: Fri Dec 27, 2013 8:06 pm
by zap71
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.

The whole thing can be found at Automated RouterOS Backup to FTP in Harry's TechBlog.

Re: Backup to External FTP Useful script

Posted: Fri Jul 29, 2016 6:24 pm
by mac86
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.

The whole thing can be found at Automated RouterOS Backup to FTP in Harry's TechBlog.

Thank you ZAP71...! Very Useful...!

Re: Backup to External FTP Useful script

Posted: Mon Jan 09, 2017 5:10 pm
by majestic
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.

Currently the file structure is like this..
disk1
└── backups
    └── auto
        ├── export
        │   └── Mikrotik-RB750Gr3_2-20170109-145244.rsc
        └── Mikrotik-RB750Gr3_2-20170109-145244.backup

3 directories, 2 files
rOS script code below:
# basedir
:local fbase "/disk1/backups/auto"

# filename prefix
:local fnp "Mikrotik"

# 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 system backup 
:local backupfn ($fnp."-".[/system identity get name]."-".$ds."-".$ts.".backup")
# file name for config export
:local exportfn ($fnp."-".[/system identity get name]."-".$ds."-".$ts.".rsc")

# backup the data
/system backup save name="$fbase/$backupfn"
:log info message="system backup saved $fbase/$backupfn (1/2)"
/export compact file="$fbase/export/$exportfn"
:log info message="configuration export saved $fbase/export/$exportfn (2/2)."
Don't forget to add a scheduled task and run this as often as you like.

Re: Backup to External FTP Useful script

Posted: Wed Apr 17, 2019 7:57 pm
by fstirparo
Hi folks! I'm trying to use the script posted a couple of years ago, but it seems that the command SET changed its syntaxis.

I'm trying to update de whole script to make it run on newer RouterOS version, but if anyone has done it yet, please post it.

Thanks

Re: Backup to External FTP Useful script

Posted: Tue Oct 15, 2019 9:40 pm
by jcsm1998
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";

Re: Backup to External FTP Useful script

Posted: Fri Jul 17, 2020 10:54 pm
by GhostyG89
If someone is looking from the original request, plus the additions that people have added over the years. Please see the below.

I have the script running on an RB4011 v6.45.1
#Gets and Sets 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])

# Generate File Name for System and User Manager Backup
:local fname1 ("<ROUTER IDENT-Config FILE NAME HERE>--".$ds."-".$ts.".backup")
:local fname2 ("<ROUTER IDENT-USER MANAGER FILE NAME HERE>--".$ds."-".$ts.".umb")

#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;


#### END OF SCRIPT