# Backup and configuration report script for Mikrotik routers. # This script prepares two files of router settings and sends them by email # The backup file (.backup) is not human-readable and is useful only for this machine # It may be useful if machine becomes corrupted and needs full restore # The config file (suffix .rsc) is a readable text file # and may be restored to in whole or in part if a software reversion is required. # Begun 23 May 2023, Denis de Castro Biomedical Consulting, BMCP@pm.me # Email should be already set up in Tools>Email # Please put the email address to send your report to here: :local addressto "name@domain.tld" # Report version 1, 24 May 23 (and please copy your version number into the next line if changed) :local reportVersion "1" # if you change the name of the script please alter it on the next line :local scriptName "backup-and-config-report" #-----# #-----# #-----# # Collect informations :local reportBody ""; :local deviceName [/system identity get name]; :local hwModel [/system routerboard get model]; :local date [/system clock get date]; :local day [ :pick $date 4 6 ]; :local month [ :pick $date 0 3 ]; :local year [ :pick $date 7 11 ]; :local shortDate ($day.$month.$year); :local longDate ($day." ".$month." ".$year); :local timezone [/system clock get time-zone-name]; :local deviceTime [/system clock get time]; :global lastBackupReportDate; :local runcount [/system script get [/system script find name=$scriptName] run-count]; :local backupFile "$deviceName.$shortDate.backup"; :local configFile "$deviceName.$shortDate.rsc"; #-----# #-----# #-----# # Start compiling report :set $reportBody ($reportBody."This email contains two settings files from $deviceName, a Mikrotik $hwModel,\n"); :set $reportBody ($reportBody."sent on $day $month $year at $deviceTime $timezone time.\n"); # Last backup report information # if no previous lastBackupReportDate, set to today, otherwise show lastBackupReportDate :if ([:len $lastBackupReportDate] = 0) do={ :set $reportBody ($reportBody . "There is no record of this report being sent previously.\n\n") } else={ :set $reportBody ($reportBody."The last report was sent on $lastBackupReportDate. \n\n"); } :set $lastBackupReportDate $longDate; # backup file :set $reportBody ($reportBody . "------------------------------------------\n\n"); :set $reportBody ($reportBody."The 'backup' file (suffix .backup) is not human-readable and is useful only for this machine\n"); :set $reportBody ($reportBody."It can be used if the machine becomes corrupted and requires a full restore.\n\n"); # Remove previous backup file from Files and save new file /file remove [;find where type=backup] /system backup save name=$backupFile dont-encrypt=yes :delay 2; :set $reportBody ($reportBody . "A backup file has been saved to the Files folder as '".$backupFile."'.\n"); :set $reportBody ($reportBody . "It can be restored (to this device) from Files>[select file]>Restore\n"); :set $reportBody ($reportBody . " or by the command-line with '/system backup load name='.\n\n"); # config file :set $reportBody ($reportBody . "------------------------------------------\n\n"); :set $reportBody ($reportBody. "The 'resource configuration' file (suffix .rsc) is a readable text file\n"); :set $reportBody ($reportBody. "which may be restored to in whole or in part if software reversion is required.\n") :set $reportBody ($reportBody. "Some sections of the file may be useful on other Mikrotik devices.\n\n") # Remove previous config file and save a new one :foreach line in=[/file find] do={ :local rscFile [/file get $line name]; :if ([:len [:find $rscFile "rsc"]] >0) do={ /file remove $rscFile; } } /export file=$configFile :delay 2; :set $reportBody ($reportBody . "A config. file has been saved to the Files folder as '".$configFile."'.\n"); :set $reportBody ($reportBody . "To restore part of the file (eg firewall rules, scripts) upload the shortened .rsc\n"); :set $reportBody ($reportBody . "to Files and run it in the Terminal with /import \n"); :set $reportBody ($reportBody . "or create a script and run it in the Terminal (/sys scr run )\n"); :set $reportBody ($reportBody . "(to restore an entire configuration run it with /import in the Terminal)\n"); :set $reportBody ($reportBody . "To restore after reset go to /System Reset Configuration & set the configuration file to run;\n"); :set $reportBody ($reportBody . "in this case a delay eg ':delay 15s;' should be prefixed to the file for interface setup.\n\n"); # Advice, report version and number :set $reportBody ($reportBody . "------------------------------------------\n"); :set $reportBody ($reportBody . "You may wish to archive copies of these files in case of future needs.\n\n"); :set $reportBody ($reportBody."Report $runcount sent by the script '$scriptName' (version $reportVersion).\n"); # Join and send files :local emailSubject ("Backup & Config files from " . $deviceName); :local files2send {$backupFile;$configFile}; /tool e-mail send subject=$emailSubject to=$addressto body=$reportBody file=$files2send; # Make log entry: :log info "***Backup report $runcount sent with current backup and config files***"