-
-
Save stepansantalov/6e968a427bd347f658d1686614f99859 to your computer and use it in GitHub Desktop.
Automatic FTP backup script for Mikrotik RouterOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:local saveUserDB true | |
:local saveSysBackup true | |
:local encryptSysBackup false | |
:local saveRawExport true | |
:local FTPServer "ftp.domain.tld" | |
:local FTPPort 21 | |
:local FTPUser "mikrotik" | |
:local FTPPass "secretpass" | |
:local mailaddr "[email protected]" | |
:local mailserver "mail.domain.tld" | |
:local mailto "[email protected]" | |
#date transformation as MikroTiK has ugly date | |
:local ds [/system clock get date ] | |
:local months ("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"); | |
:local date1month [ :pick $ds 0 3 ]; | |
:local mm ([ :find $months $date1month -1 ] + 1); | |
:if ($mm < 10) do={ | |
:set date1month ("0" . $mm); | |
} else={ | |
:set date1month $mm; | |
} | |
:local ts [/system clock get time] | |
:set ts ([:pick $ts 0 2].[:pick $ts 3 5].[:pick $ts 6 8]) | |
:set ds ([:pick $ds 7 11].$mm.[:pick $ds 4 6]) | |
:local filename ("/backup-".[/system identity get name]."-"."$ds.$ts") | |
# System backup | |
:if ($saveSysBackup) do={ | |
:if ($encryptSysBackup = true) do={ /system backup save name=($filename.".backup") } | |
:if ($encryptSysBackup = false) do={ /system backup save dont-encrypt=yes name=($filename.".backup") } | |
:log info message="System Backup Finished" | |
} | |
# Export | |
if ($saveRawExport) do={ | |
/export file=($filename.".export") | |
:log info message="Raw configuration export finished" | |
} | |
#zero variable | |
:local backupFileName "" | |
#Upload | |
:foreach backupFile in=[/file find] do={ | |
:set backupFileName ("/".[/file get $backupFile name]) | |
:if ([:typeof [:find $backupFileName $filename]] != "nil") do={ | |
/tool fetch address=$FTPServer port=$FTPPort src-path=$backupFileName user=$FTPUser mode=ftp password=$FTPPass dst-path=$backupFileName upload=yes | |
} | |
} | |
:delay 5s | |
#Local cleanup | |
: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" | |
# Email notify | |
:local subject ([/system identity get name]." daily backup") | |
/tool e-mail send from=($mailaddr) server=($mailserver) subject=($subject) body=("Done ".$ds."-".$ts) to=($mailto) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment