Community discussions

 
Alqatife
just joined
Topic Author
Posts: 14
Joined: Wed Jun 02, 2010 3:40 pm

Backup to External FTP Useful script

Tue Jun 07, 2011 11:28 am

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
 
croskiper
just joined
Posts: 6
Joined: Sat Jan 09, 2010 2:50 pm

Re: Backup to External FTP Useful script

Sun Jul 24, 2011 8:30 pm

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..
 
Alqatife
just joined
Topic Author
Posts: 14
Joined: Wed Jun 02, 2010 3:40 pm

Re: Backup to External FTP Useful script

Sat Sep 17, 2011 9:09 am

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,
 
kassiolm
just joined
Posts: 7
Joined: Tue Apr 01, 2008 4:04 pm

Re: Backup to External FTP Useful script

Wed Jul 31, 2013 9:29 pm

works on v6.1?
 
kassiolm
just joined
Posts: 7
Joined: Tue Apr 01, 2008 4:04 pm

Re: Backup to External FTP Useful script

Fri Aug 02, 2013 10:12 pm

the script is creating the files but don't upload, need help!!!
 
JoePolgar
just joined
Posts: 4
Joined: Tue Dec 14, 2010 12:33 am

Re: Backup to External FTP Useful script

Wed Aug 14, 2013 11:42 am

use dst-path=filename instead of dst-path=/
 
User avatar
zap71
Frequent Visitor
Frequent Visitor
Posts: 83
Joined: Fri Sep 27, 2013 11:10 am
Location: Berlin, Germany
Contact:

Re: Backup to External FTP Useful script

Fri Dec 27, 2013 8:06 pm

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.
 
User avatar
mac86
Member Candidate
Member Candidate
Posts: 118
Joined: Sat Nov 25, 2006 12:52 am
Location: bahia blanca - argentina
Contact:

Re: Backup to External FTP Useful script

Fri Jul 29, 2016 6:24 pm

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...!
http://www.netpro-ar.com
MTINE - MTCWE - MTCRE - MTCTCE - MTCNA
Patagonia Argentina IT Consultant

Mikrotik user from V2.7.7 - Aug/2003
 
majestic
Frequent Visitor
Frequent Visitor
Posts: 81
Joined: Mon Dec 05, 2016 11:19 am

Re: Backup to External FTP Useful script

Mon Jan 09, 2017 5:10 pm

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.
 
fstirparo
just joined
Posts: 10
Joined: Thu Mar 07, 2019 8:06 pm

Re: Backup to External FTP Useful script

Wed Apr 17, 2019 7:57 pm

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
 
jcsm1998
just joined
Posts: 1
Joined: Sun Mar 25, 2018 9:03 pm

Re: Backup to External FTP Useful script

Tue Oct 15, 2019 9:40 pm

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

Who is online

Users browsing this forum: No registered users and 14 guests