Skip to content

Instantly share code, notes, and snippets.

@ranzhe
Last active September 3, 2020 01:01
Show Gist options
  • Save ranzhe/a2ea15bd328e9766f0a4 to your computer and use it in GitHub Desktop.
Save ranzhe/a2ea15bd328e9766f0a4 to your computer and use it in GitHub Desktop.
Automatic E-mail backup script for Mikrotik RouterOS
:local saveUserDb true
:local saveSysBackup true
:local encryptSysBackup false
:local saveRawExport true
:local mailTo "destination@email.tld"
: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])
:local fname ("BACKUP-".[/system identity get name]."-".$ds."-".$ts)
:local sfname ("/".$fname)
:if ($saveUserDb) do={
/tool user-manager database save name=($sfname.".umb")
:log info message="User Manager DB Backup Finished"
}
:if ($saveSysBackup) do={
:if ($encryptSysBackup) do={
/system backup save name=($sfname.".backup")
}
else={
/system backup save dont-encrypt=yes name=($sfname.".backup")
}
:log info message="System Backup Finished"
}
if ($saveRawExport) do={
/export file=($sfname.".rsc")
:log info message="Raw configuration script export Finished"
}
:foreach backupFile in=[/file find] do={
:set backupFileName ("/".[/file get $backupFile name])
:if ([:typeof [:find $backupFileName $sfname]] != "nil") do={
/tool email send subject=([:pick $backupFileName 1 [:len $backupFileName]]) to=$mailTo file=$backupFileName
}
}
:delay 5s
:foreach backupFile in=[/file find] do={
:if ([:typeof [:find [/file get $backupFile name] "BACKUP-"]]!="nil") do={
/file remove $backupFile
}
}
:log info message="Successfully removed Temporary Backup Files"
:log info message="Automatic Backup Completed Successfully"
@0x9394
Copy link

0x9394 commented Oct 27, 2015

line 26 should be

} else={

otherwise script will stop at line 27.

then got bellow error, pls tell how to fix it, thanks. ros=v6.32.3

image

@0x9394
Copy link

0x9394 commented Oct 27, 2015

got it , missed $ befor "backupFileName"

@0x9394
Copy link

0x9394 commented Oct 27, 2015

then got this error,

[admin@MikroTik] /system script> run 0
Saving system configuration
Configuration backup saved
input does not match any value of file

btw, /tool email should be: /tool e-mail

@xant1986
Copy link

line 41
/tool email send subject=([:pick $backupFileName 1 [:len $backupFileName]]) to=$mailTo file=$backupFileName
should be
/tool email send subject=([:pick $backupFileName 1 [:len $backupFileName]]) to=$mailTo file=([:pick $backupFileName 1 [:len $backupFileName]])

@beeyev
Copy link

beeyev commented Apr 17, 2020

Mikrotik RouterOS automatic backup and update
https://github.com/beeyev/Mikrotik-RouterOS-automatic-backup-and-update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment