Skip to content

Instantly share code, notes, and snippets.

@stepansantalov
Forked from ranzhe/auto-backup-ftp.rsc
Last active June 21, 2018 11:22
Show Gist options
  • Save stepansantalov/6e968a427bd347f658d1686614f99859 to your computer and use it in GitHub Desktop.
Save stepansantalov/6e968a427bd347f658d1686614f99859 to your computer and use it in GitHub Desktop.
Automatic FTP backup script for Mikrotik RouterOS
: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 "mikrotik@domain.tld"
:local mailserver "mail.domain.tld"
:local mailto "admin@domain.tld"
#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