📜 ⬆️ ⬇️

Mikrotik RouterOS log / backup to email

I want to share my configuration backup script and sending logs to the mail with the Mikrotik RouterBoard RB750GL (in my case 5.14, firmware 2.38). I think it will fit most devices on this glorious OS. The advantage of the approach is that the configuration file itself comes as an attachment to the letter with the file logs. And the logs are right in the letter - not in the application.

#: log info "/// ----> I started backing up" (uncommenting these lines for debug)
: global backupfile ([/ system identity get name]. ".backup") # set the variable
: if ([/ file find name = $ backupfile]! = "") do = {/ file rem $ backupfile} # delete if the file already exists
: delay 2s

#: log info "/// ----> I am preparing a log for sending"

: global logMessages;
: set logMessages ""
: foreach i in = [/ log find] do = {
: set logMessages ($ logMessages. [/ log get $ i time]. "");
: set logMessages ($ logMessages. [/ log get $ i message]);
: set logMessages ($ logMessages. "\ n")
}
')
#: log info "/// ----> Create a backup"
/ system backup save name = $ backupfile
#: log info "/// ----> Waiting for 5 seconds to complete the procedure"
: delay 5s
#: log info "/// ----> I send the config to e-mail" (the following code is one long line!)
/ tool e-mail send to=mail@examle.com subject = (“router daily backup”) file = $ backup body = (“backup configuration file of the router”. [/ system identity get name]. ". \ nPerformed:" . [/ system clock get time]. "". \ [/ system clock get date]. "\ n ____________________ \ n \ nUptime:". [/ system resource get uptime]. "\ n Free RAM:". [/ system resource get free-memory]. "from". [/ system resource get total-memory]. "Kbyte. \ nFree space:". [/ system resource get free-hdd-space]. "from". [/ system resource get total-hdd-space]. "Kbyte. \ nBad-blocks:". [/ system resource get bad-blocks]. "%. \ nDownloading the processor:". [/ system resource get cpu-load]. " % \ n ____________________ \ n \ nLast log entries: \ n \ n ". $ logMessages." \ n ____________________ \ n ". [/ system identity get name])

#: log info "/// ----> I wait 10 seconds until the letter is sent"
: delay 10s
/ file remove $ backupfile
: log info "/// ----> Backup completed. The file was sent to mail@example.com. Hurray!"


I put on a nightly performance and as a result, along with the news, I read the news from the front in the morning - I receive a letter of this type

image

I will be glad to recommendations and tips for improving the process!
via dobryj.ru

Source: https://habr.com/ru/post/153971/


All Articles