Schedule auto backup and rsc files from a Mikrotik using FTP

Last Updated or created 2024-03-14

Schedule below script after changing the first few lines

### Set local variables. Change the value between "" to reflect your environment. Do not delete quotation marks. 
:local ftpserver "ftpserver"
:local username "ftpuser"
:local password "ftppass"
:local ftppath "mikrotik/hapac2"

:local hostname [/system identity get name]
:local localfilename "$hostname-Backup-Daily";
:global thisdate [/system clock get date]
:global thistime [/system clock get time]
:global date ([:pick $thisdate 0 10])
:local remotebackup ([/system identity get name]."-".$date.".backup")
:local remotersc ([/system identity get name]."-".$date.".rsc")

### 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=($ftppath."/".$remotebackup) 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=($ftppath."/".$remotersc) upload=yes
:log info "Backup Uploaded Successfully"

Files are written on the FTP server like this:

MikroTikHAP1-2024-03-14.backup
MikroTikHAP1-2024-03-14.rsc

Leave a Reply

Your email address will not be published. Required fields are marked *