Community discussions

MikroTik App
 
fra81ita
just joined
Topic Author
Posts: 4
Joined: Thu Oct 17, 2019 12:41 pm

Fully Working Mikrotik Router OS Backup Script on local storage and FTP Upload  [SOLVED]

Wed Apr 01, 2020 6:32 pm

Hello All,
I searched for this script on the forum, but I found only few pieces tat didn't fit exactly what I was looking for.
I'm posting here only for you reference, maybe it could be helpfull to someone. We have it in production since a couple of weeks.

The following script is creating a local backup and export (we launch it daily), then upload these files to and external FTP server adding the date to the filename. It let a local version of the files that are replaced every day to avoid lots of files and running out of space (if you want you can uncomment 3 lines and delete the local backup).

Copy and paste this in your script text.
--------------
### Set local variables. Change the value between "" to reflect your environment. Do not delete quotation marks. 
:local ftpserver "FTP-SERVER-FQDN_or_IP-ADDRESS"
:local username "FTP-SERVER-USERNAME"
:local password "FTP-SERVER-PASSWORD"

### Set Local and Remote Filename variables. Do not change this unless you want to edit the format of the filename.
### Default "local file name" is always the same to avoid lots of files and running out of space, "remote file name" uploaded to FTP has the date
:local hostname [/system identity get name]
:local date ([:pick [/system clock get date] 7 11] \
. [:pick [/system clock get date] 0 3] \
. [:pick [/system clock get date] 4 6]);
:local localfilename "$hostname-Backup-Daily";
:local remotefilename "$hostname-$date";

### Enable for Debug removing staing hash in the following lines
:log info "$localfilename";
:log info "$remotefilename";
:log info "$hostname";
:log info "$date";

### Stating the Backup
:log info "STARTING BACKUP";

### Create backup file and export the config.
export compact file="$localfilename"
/system backup save name="$localfilename"
:log info "Backup Created Successfully"

### Upload config file to FTP server.
/tool fetch address=$ftpserver src-path="$localfilename.backup" \
user=$username mode=ftp password=$password \
dst-path="$remotefilename.backup" upload=yes
:log info "Config Uploaded Successfully"

### Upload backup file to FTP server.
/tool fetch address=$ftpserver src-path="$localfilename.rsc" \
user=$username mode=ftp password=$password \
dst-path="$remotefilename.rsc" upload=yes
:log info "Backup Uploaded Successfully"

### Wait 2 second before doing anything
delay 2;

### Remove starting hash in the following lines to delete created backup files once they have been uploaded. I usually let them there because it's useful having them ready.
#/file remove "$localfilename.backup"
#/file remove "$localfilename.rsc"
#:log info "Local Backup Files Deleted Successfully"

### Finishing the Backup
:log info "BACKUP FINISHED";

If is more confortable for you, copy and paste the following text in the terminal and it will create the script and the daily scheduler staring at 22:00 PM. Please change the variables at the first few lines.
/system scheduler
add interval=1d name=Backup_Daily on-event="/system script run Backup_Daily" \
    policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
    start-date=jan/01/1970 start-time=22:00:00
	
/system script
add dont-require-permissions=no name=Backup_Daily owner=admin policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="#\
    ## Set local variables. Change the value between \"\" to reflect your envi\
    ronment. Do not delete quotation marks. \r\
	\n:local ftpserver \"FTP-SERVER-FQDN_or_IP-ADDRESS\"\r\
    \n:local username \"FTP-SERVER-USERNAME\"\r\
    \n:local password \"FTP-SERVER-PASSWORD\"\r\
    \n\r\
    \n### Set Local Filename and Remote Filename variables. Do not change this\
    \_unless you want to edit the format of the filename.\r\
    \n### Default \"local file name\" is always the same to avoid lots of file\
    s and running out of space\r\
    \n:local hostname [/system identity get name]\r\
    \n:local date ([:pick [/system clock get date] 7 11] \\\r\
    \n. [:pick [/system clock get date] 0 3] \\\r\
    \n. [:pick [/system clock get date] 4 6]);\r\
    \n:local localfilename \"\$hostname-Backup-Daily\";\r\
    \n:local remotefilename \"\$hostname-\$date\";\r\
    \n\r\
    \n### Enable for Debug removing staing hash in the following lines\r\
    \n:log info \"\$localfilename\";\r\
    \n:log info \"\$remotefilename\";\r\
    \n:log info \"\$hostname\";\r\
    \n:log info \"\$date\";\r\
    \n\r\
    \n### Stating the Backup\r\
    \n:log info \"STARTING BACKUP\";\r\
    \n\r\
    \n# Create backup file and export the config.\r\
    \nexport compact file=\"\$localfilename\"\r\
    \n/system backup save name=\"\$localfilename\"\r\
    \n:log info \"Backup Created Successfully\"\r\
    \n\r\
    \n# Upload config file to FTP server.\r\
    \n/tool fetch address=\$ftpserver src-path=\"\$localfilename.backup\" \\\r\
    \nuser=\$username mode=ftp password=\$password \\\r\
    \ndst-path=\"\$remotefilename.backup\" upload=yes\r\
    \n:log info \"Config Uploaded Successfully\"\r\
    \n\r\
    \n# Upload backup file to FTP server.\r\
    \n/tool fetch address=\$ftpserver src-path=\"\$localfilename.rsc\" \\\r\
    \nuser=\$username mode=ftp password=\$password \\\r\
    \ndst-path=\"\$remotefilename.rsc\" upload=yes\r\
    \n:log info \"Backup Uploaded Successfully\"\r\
    \n\r\
    \n### Wait 2 second before doing anything\r\
    \ndelay 2;\r\
    \n\r\
    \n### Remove starting hash in the following lines to delete created backup\
    \_files once they have been uploaded\r\
    \n#/file remove \"\$localfilename.backup\"\r\
    \n#/file remove \"\$localfilename.rsc\"\r\
    \n#:log info \"Local Backup Files Deleted Successfully\"\r\
    \n\r\
    \n### Finishing the Backup\r\
    \n:log info \"BACKUP FINISHED\";\r\
    \n"


Hope it will be useful to everyone.
Francesco Capuano
 
User avatar
gogotha
newbie
Posts: 30
Joined: Mon May 21, 2018 1:16 am
Location: Tyler

Re: Fully Working Mikrotik Router OS Backup Script on local storage and FTP Upload

Sun Aug 08, 2021 9:26 pm

I know this is a little old, but WOW. Love it and thank you for sharing, works perfectly!!!!
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Fully Working Mikrotik Router OS Backup Script on local storage and FTP Upload

Mon Aug 09, 2021 2:58 am

If you want full backup anythig on routerboard, use this as model for backup all other parts:

viewtopic.php?f=1&t=175360&p=858564&hil ... te#p858564
 
User avatar
gogotha
newbie
Posts: 30
Joined: Mon May 21, 2018 1:16 am
Location: Tyler

Re: Fully Working Mikrotik Router OS Backup Script on local storage and FTP Upload

Wed Aug 11, 2021 3:38 am

Those look super! I'm curious, is there a way to encrypt backup data pre-upload via FTP or encrypt the scripts you pointed to me via email? Was speaking with one of our consultants and mentioned I was testing FTP upload and he cringed because of FTP. I checked the uploads and they are not encrypted. Most of the stuff we do can be done over VPN but we have a few sitting at the edge.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Fully Working Mikrotik Router OS Backup Script on local storage and FTP Upload

Wed Aug 11, 2021 3:43 am

Nothing is encrypted / encryptable except the ".backup" file.

You can send FTP to another routerboard under your control instead of external ftp,
or send backup by TLS over e-mail, or sftp or https, etc. etc. etc.
 
oh3mbc
just joined
Posts: 5
Joined: Sun Apr 03, 2016 11:31 am

Re: Fully Working Mikrotik Router OS Backup Script on local storage and FTP Upload

Sun Mar 27, 2022 9:27 am

The following script is creating a local backup and export (we launch it daily), then upload these files to and external FTP server adding the date to the filename. It let a local version of the files that are replaced every day to avoid lots of files and running out of space (if you want you can uncomment 3 lines and delete the local backup).
Hope it will be useful to everyone.
It IS usefull, running this on all my units at the moment!

One idea i was thinking...
How about noting the ROS version, and keeping one backup if version changes?
This could be handy in situation if new version has some unwanted features, or dare i say, even bugs.
One could just go back one step for last "known working" version
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Fully Working Mikrotik Router OS Backup Script on local storage and FTP Upload

Mon Mar 28, 2022 3:51 am

simply add the ros version on filename....
 
dustojnikhummer
just joined
Posts: 24
Joined: Tue Jan 05, 2021 1:55 pm

Re: Fully Working Mikrotik Router OS Backup Script on local storage and FTP Upload

Sun Oct 09, 2022 7:28 pm

I love this, but would it be possible to upload into a subfolder instead of / ?
 
jjdperryman
just joined
Posts: 10
Joined: Fri Nov 17, 2017 7:17 pm

Re: Fully Working Mikrotik Router OS Backup Script on local storage and FTP Upload

Thu Oct 27, 2022 7:51 pm

I think this script is broken in the Mikrotik OS 6.49.

export compact file="$localfilename" doesn't seem to be executing.

I've ran the command in terminal an dit works fine, but doesn't like it when it's running through the script.
I've even replaced it with /export with the same results.
 
jack2020
newbie
Posts: 43
Joined: Sat Aug 17, 2019 4:47 pm

Re: Fully Working Mikrotik Router OS Backup Script on local storage and FTP Upload

Sat Feb 11, 2023 7:28 pm

Good afternoon,
I was trying to use this script on my router with version 7.7 and it doesn't work. Does nothing.
Will it work in this version?
Thanks for any help.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3279
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Fully Working Mikrotik Router OS Backup Script on local storage and FTP Upload

Sat Feb 11, 2023 8:23 pm

Only one small thing missing in RouterOS. After backup is done, save the file and upload it. Also make a hash of the backup file.
Next day, after a new backup file are made, compare it to yesterdays backup file using hash, if no change, do not send file to FTP.


-----------------------------------------------------------------------------------------------
Use Splunk> to log/monitor your MikroTik Router(s).--> MikroTik->Splunk :mrgreen:
Backup config to Gmail -->Backup
Block users that tries ut use non open ports -->Block
 
swits1109
Frequent Visitor
Frequent Visitor
Posts: 81
Joined: Sat Sep 10, 2016 6:03 pm

Re: Fully Working Mikrotik Router OS Backup Script on local storage and FTP Upload

Tue Feb 14, 2023 9:48 am

Good afternoon,
I was trying to use this script on my router with version 7.7 and it doesn't work. Does nothing.
Will it work in this version?
Thanks for any help.

Agreed, the script does not seem to work anymore
 
jjdperryman
just joined
Posts: 10
Joined: Fri Nov 17, 2017 7:17 pm

Re: Fully Working Mikrotik Router OS Backup Script on local storage and FTP Upload

Wed Mar 01, 2023 10:49 pm

I got the script to work by changing the name of the router, it was named 'tower_site 12/04'. Once I changed it to 'tower_site 12-04', both my backup scripts, including this ftp backup started working.
 
User avatar
diamuxin
Member
Member
Posts: 317
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Fully Working Mikrotik Router OS Backup Script on local storage and FTP Upload

Thu Mar 02, 2023 6:51 pm

Only one small thing missing in RouterOS. After backup is done, save the file and upload it. Also make a hash of the backup file.
Next day, after a new backup file are made, compare it to yesterdays backup file using hash, if no change, do not send file to FTP.
That's a very interesting idea, how do we get to compare the hash of the previous file?

My current script creates a daily file with the date inserted in the file name, I understand that to compare it via hash it should always have the same name, right?

My script would be like this:
:local time [/system clock get time]
:local date [/system clock get date]
:local sysname [/system identity get name]
:local Version [/system resource get version]
:local mydatetime
:local textfilename
:local backupfilename

:local ftpserver "192.168.88.247"
:local username "mikrotik"
:local password "MyStrongPassword!"
:local port 2121

:log info ">>> START FTP Backup <<<"

:set mydatetime ($"date" . " " . $"time")
:set textfilename ($"mydatetime" . "-" . $"sysname" . ".rsc")
:set backupfilename ($"mydatetime" . "-" . $"sysname" . ".backup")

:if ($Version~"^7") do={
    :execute [/export file=$"textfilename" show-sensitive]
} else={
    :execute [/export file=$"textfilename"]
}
:execute [/system backup save name=$"backupfilename"]

:delay 3s
/tool fetch address=$ftpserver mode=ftp user=$username password=$password \
    src-path=$textfilename dst-path=$textfilename upload=yes port=$port;
:log info "FTP Backup: Sent $textfilename via FTP"

:delay 3s
tool fetch address=$ftpserver mode=ftp user=$username password=$password \
    src-path=$backupfilename dst-path=$backupfilename upload=yes port=$port;
:log info "FTP Backup: Sent $backupfilename via FTP"

:delay 5s
/file remove $textfilename
/file remove $backupfilename

# Telegram module
:local MessageText "\F0\9F\92\BE <b>$sysname:</b> FTP Backup OK"
:local SendTelegramMessage [:parse [/system script get MyTGBotSendMessage source]]
$SendTelegramMessage MessageText=$MessageText

:log info ">>> FTP Backup OK <<<"
Thanks in advance.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3279
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Fully Working Mikrotik Router OS Backup Script on local storage and FTP Upload

Thu Mar 02, 2023 8:08 pm

That's a very interesting idea, how do we get to compare the hash of the previous file?
That would be when MT added hash function to RouterOS :)
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Fully Working Mikrotik Router OS Backup Script on local storage and FTP Upload

Fri Mar 03, 2023 1:45 am

That's a very interesting idea, how do we get to compare the hash of the previous file?

My current script creates a daily file with the date inserted in the file name, I understand that to compare it via hash it should always have the same name, right?
Filename is not used for compute hash (or not???...)

.backup files are everytime different because do not are a simple .rsc, but also containing the terminal history, logs, etc.
is near impossible have two identical backup without copy-paste the file...

I do everyday export of .rsc of all device, and on sparse time all files go to one internal ftp server, and with "diff", ignoring first line, I have everyday one report if is changed something somewhere.

Do a simple hash calc for check inside if the file is changed can be possible on RouterOS, just something simple, nothing complex or based on CRC/SHA etc., just for see if something is different.
 
User avatar
Maggiore81
Trainer
Trainer
Posts: 558
Joined: Sun Apr 15, 2012 12:10 pm
Location: Italy
Contact:

Re: Fully Working Mikrotik Router OS Backup Script on local storage and FTP Upload

Sat Jan 13, 2024 10:42 am

This script has issue since 7.13
viewtopic.php?p=1048250#p1048240
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Fully Working Mikrotik Router OS Backup Script on local storage and FTP Upload

Sat Jan 13, 2024 11:04 am

 
User avatar
Maggiore81
Trainer
Trainer
Posts: 558
Joined: Sun Apr 15, 2012 12:10 pm
Location: Italy
Contact:

Re: Fully Working Mikrotik Router OS Backup Script on local storage and FTP Upload

Sat Jan 13, 2024 11:06 am

Thank you rextended
I have opened SUP-140393
Last edited by Maggiore81 on Sat Jan 13, 2024 12:20 pm, edited 1 time in total.
 
jaclaz
Long time Member
Long time Member
Posts: 556
Joined: Tue Oct 03, 2023 4:21 pm

Re: Fully Working Mikrotik Router OS Backup Script on local storage and FTP Upload

Sat Jan 13, 2024 12:17 pm

Only as a side-side note, I have often been (as a dinosaur coming from DOS and green/amber monitors era) mocked (in other contexts, not Mikrotik specific) for my habit of always using underscore instead of spaces in names of files, systems, etc., and I was almost convinced of dropping this habit, but (more due to laziness and stubborness than anything else) never changed.

After all these years we are back to the origin :lol: .
 
User avatar
Maggiore81
Trainer
Trainer
Posts: 558
Joined: Sun Apr 15, 2012 12:10 pm
Location: Italy
Contact:

Re: Fully Working Mikrotik Router OS Backup Script on local storage and FTP Upload

Sat Jan 13, 2024 12:37 pm

I perfectly understand your position and I generally agree.
But since it has been always allowed to have spaces, because of the modern era... it is not a nice move to CHANGE unilaterally this behaviour and have existings scripts useless.
 
jaclaz
Long time Member
Long time Member
Posts: 556
Joined: Tue Oct 03, 2023 4:21 pm

Re: Fully Working Mikrotik Router OS Backup Script on local storage and FTP Upload

Sat Jan 13, 2024 12:58 pm

Sure it isn't nice, I will go a bit further, stating that it is - to say the least - reckless, particularly because it has not (AFAIK) been documented explicitly with a big fat warning, I am not professionally in the specific field, but I believe that hundred or thousands of man hours have been (or will be) spent to correct existing scripts when updating, and only once involving working systems will show this or that sign of malfunction.

I really don't understand (it is not just Mikrotik, to be fair it has become a general approach by many) how this lack of respect for customers and their time has become the "new normal".

Who is online

Users browsing this forum: No registered users and 14 guests