diff --git a/Auto-Upgrade/README.md b/Auto-Upgrade/README.md deleted file mode 100644 index 05f6fa9..0000000 --- a/Auto-Upgrade/README.md +++ /dev/null @@ -1,4 +0,0 @@ -# MikrtoTik RouterOS automatic firmware update - -## firmware-updater.rsc - diff --git a/Auto-Upgrade/firmware-updater.rsc b/Auto-Upgrade/firmware-updater.rsc deleted file mode 100644 index 6af37f6..0000000 --- a/Auto-Upgrade/firmware-updater.rsc +++ /dev/null @@ -1,109 +0,0 @@ -# Script name: firmware-updater - -########## Set variables -## Notification e-mail -:local emailEnabled true -:local emailAddress "email@example.com" -:local sendBackupToEmail true -########## - - -## !!!! DO NOT CHANGE ANYTHING BELOW THIS LINE !!!! ## - -:global beeUpdateStep; - -## Check for update -/system package update -set channel=current -check-for-updates - -## Wait on slow connections -:delay 15s; - -## First step, check for new updates -:if ([:len $beeUpdateStep] = 0 && [get installed-version] != [get latest-version]) do={ - ## New version of RouterOS available, let's upgrade - :log info ("Upgrading RouterOS on router $[/system identity get name], board name: $[/system resource get board-name], serial number: $[/system routerboard get serial-number] | From $[/system package update get installed-version] to $[/system package update get latest-version] (channel:$[/system package update get channel])") - - :if ($emailEnabled = true) do={ - :local attachments; - - :if ($sendBackupToEmail = true) do={ - ## date and time in format: 2018aug06-215139 - :local dtame ([:pick [/system clock get date] 7 11] . [:pick [/system clock get date] 0 3] . [:pick [/system clock get date] 4 6] . "-" . [:pick [/system clock get time] 0 2] . [:pick [/system clock get time] 3 5] . [:pick [/system clock get time] 6 8]); - ## unified backup file name without extension - :local bname "$[/system identity get name].$[/system routerboard get serial-number].v$[/system package update get installed-version]_$dtame" - :local sysFileBackup "$bname.backup" - :local configFileBackup "$bname.rsc" - :set attachments {$sysFileBackup;$configFileBackup} - - ## Make system backup - /system backup save dont-encrypt=yes name=$bname - ## Export config file - /export compact file=$bname - - ## Wait until bakup is done - :delay 15s; - } - - /tool e-mail send to=$emailAddress subject="Upgrade router: $[/system identity get name] FW has been started" body="Upgrading RouterOS on router $[/system identity get name] from $[/system package update get installed-version] to $[/system package update get latest-version] \r\nYou will recieve final report with detailed information when upgrade process is finished. If you have not got second email in next 5 minutes, then probably something went wrong." file=$attachments - - ## Wait for mail to be send & upgrade - :delay 15s; - - ## Remove backups which we have already sent - :if ($sendBackupToEmail = true && [/tool e-mail get last-status] = "succeeded") do={ - /file remove $attachments; - } - } - - - ## Set scheduled task to upgrade routerboard firmware on the next boot, task will be deleted when upgrade is done. (That is why you should keep original script name) - /system schedule add name=BEE-UPGRADE-NEXT-BOOT on-event=":global beeUpdateStep \"routerboardUpgrade\"; :delay 1s; /system script run firmware-updater;" start-time=startup interval=0 - - ## "install" command is reincarnation of the "upgrade" command - doing exactly the same but under a different name - install -} - -## Second step (after first reboot) routerboard firmware upgrade -:if ( $beeUpdateStep = "routerboardUpgrade") do={ - - ## Remove global variable for this step - /system script environment remove beeUpdateStep; - ## Remove task because we need it just once, right after reboot next to RouterOS install - /system scheduler remove BEE-UPGRADE-NEXT-BOOT - - ## RouterOS latest, let's check for updated firmware - /system routerboard - - :if ( [get current-firmware] != [get upgrade-firmware]) do={ - ## New version of firmware available, let's upgrade - :log info ("Upgrading firmware on router $[/system identity get name], board name: $[/system resource get board-name], serial number: $[/system routerboard get serial-number] | From $[/system routerboard get current-firmware] to $[/system routerboard get upgrade-firmware]") - - ## Start the upgrading process - upgrade - - ## Wait until the upgrade is finished - :delay 60s; - - ## Set scheduled task to send final report on the next boot, task will be deleted when is is done. (That is why you should keep original script name) - /system schedule add name=BEE-FINAL-REPORT-NEXT-BOOT on-event=":global beeUpdateStep \"finalReport\"; :delay 1s; /system script run firmware-updater;" start-time=startup interval=0 - - ## Reboot system to boot with new firmware - /system reboot - } -} - -## Last step (after second reboot) sending final report -:if ( $beeUpdateStep = "finalReport") do={ - ## Remove global variable for this step - /system script environment remove beeUpdateStep; - ## Remove task because we need it just once - /system scheduler remove BEE-FINAL-REPORT-NEXT-BOOT - - :log info "Upgrading RouterOS and routerboard firmware finished. Current RouterOS version: $[/system package update get installed-version], routerboard firmware: $[/system routerboard get current-firmware]." - - :if ($emailEnabled = true) do={ - /tool e-mail send to="$emailAddress" subject="Router: $[/system identity get name] has been upgraded with new FW!" body="Upgrading RouterOS and routerboard firmware finished. \r\n\r\nRouter name: $[/system identity get name]\r\nCurrent RouterOS version: $[/system package update get installed-version]; Routerboard firmware: $[/system routerboard get current-firmware]; Update channel: $[/system package update get channel]; \r\nBoard name: $[/system resource get board-name]; Serial number: $[/system routerboard get serial-number]; \r\n\r\n Changelog: https://mikrotik.com/download/changelogs/current-release-tree" - } -} \ No newline at end of file diff --git a/README.md b/README.md index ef8506b..94ce4b0 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,28 @@ -# Mikrotik scripts -Useful scripts for MikroTik RouterOS +# Mikrotik Firmware Auto Updater +Script automatically updates router to the latest firmware. +When script finds new uptate it sends email notification that upgrade process has started, system backup and config file are in attachment. After firmware and routerboard got updated, it sends second email which tells that upgrade process has been finished. -## [Auto update, backup and email notification](https://github.com/beeyev/mikrotik/tree/master/Auto-Upgrade) +## How to use +##### 1. Create new script +System -> Scripts [Add] +**Imprtant! ** Script name has to be **firmware-updater** +Put script source and **set your email address** to the variable *emailAddress* +![](https://github.com/beeyev/Mikrotik-Firmware-Auto-Updater/raw/master/howto/script-name.png) + +##### 2. Configure mail server +Tools -> Email +Set your email server parameters. If you don't have one, i recommend to use [smtp2go.com](https://smtp2go.com "smtp2go.com") service, it allows to send thousand emails per month for free. +![](https://github.com/beeyev/Mikrotik-Firmware-Auto-Updater/raw/master/howto/email-config.png) + +##### 3. Create scheduled task +System -> Scheduler [Add] +Name: Firmware Updater +Start Time: 03:00:00 +Interval: 1d 00:00:00 +On Event: /system script run firmware-updater; +![](https://github.com/beeyev/Mikrotik-Firmware-Auto-Updater/raw/master/howto/scheduler-task.png) + +Or you can use this command to create the task: +``` +/system scheduler add name="Firmware Updater" on-event="/system script run firmware-updater;" start-time=03:00:00 interval=1d comment="" disabled=no +``` \ No newline at end of file diff --git a/firmware-updater.rsc b/firmware-updater.rsc new file mode 100644 index 0000000..8e60a01 --- /dev/null +++ b/firmware-updater.rsc @@ -0,0 +1,119 @@ +# Script name: firmware-updater + +########## Set variables +## Notification e-mail +:local emailEnabled true; +:local emailAddress "email@example.com"; +:local sendBackupToEmail true; + +## Backup encryption password, no encryption if no password. +:local backupPassword "" +## If true, passwords will be included in exported config +:local sensetiveDataInConfig false; + +## Update channel. Possible values: current, bugfix +:local updateChannel "current"; +########## + + +## !!!! DO NOT CHANGE ANYTHING BELOW THIS LINE, IF YOU ARE NOT SURE WHAT YOU ARE DOING !!!! ## +:log info ("Firmware checking and upgrade process has started"); +## Set global var to the local one, then delete it form the global environment +:global beeGlobalUpdateStep; +:local updateStep $beeGlobalUpdateStep; +/system script environment remove beeGlobalUpdateStep + +## if it is a very first step +:if ([:len $updateStep] = 0) do={ + ## Check for update + /system package update set channel=$updateChannel; + /system package update check-for-updates; + + # If we found some updates + :if ([/system package update get installed-version] != [/system package update get latest-version]) do={ + ## New version of RouterOS available, let's upgrade + :log info ("Upgrading RouterOS on router $[/system identity get name], board name: $[/system resource get board-name], serial number: $[/system routerboard get serial-number] | From $[/system package update get installed-version] to $[/system package update get latest-version] (channel:$[/system package update get channel])"); + + :if ($emailEnabled = true) do={ + :local attachments; + + :if ($sendBackupToEmail = true) do={ + ## date and time in format: 2018aug06-215139 + :local dtame ([:pick [/system clock get date] 7 11] . [:pick [/system clock get date] 0 3] . [:pick [/system clock get date] 4 6] . "-" . [:pick [/system clock get time] 0 2] . [:pick [/system clock get time] 3 5] . [:pick [/system clock get time] 6 8]); + ## unified backup file name without extension + :local bname "$[/system identity get name].$[/system routerboard get serial-number].v$[/system package update get installed-version]_$dtame"; + :local sysFileBackup "$bname.backup"; + :local configFileBackup "$bname.rsc"; + :set attachments {$sysFileBackup;$configFileBackup} + + ## Make system backup + :if ([:len $backupPassword] = 0) do={ + /system backup save dont-encrypt=yes name=$bname; + } else={ + /system backup save encryption=aes-sha256 password=$backupPassword name=$bname; + } + + ## Export config file + :if ($sensetiveDataInConfig = true) do={ + /export compact file=$bname; + } else={ + /export compact hide-sensitive file=$bname; + } + + ## Wait until bakup is done + :delay 15s; + } + + /tool e-mail send to=$emailAddress subject="Upgrade router: $[/system identity get name] FW has been started" body="Upgrading RouterOS on router $[/system identity get name] from $[/system package update get installed-version] to $[/system package update get latest-version] \r\nYou will recieve final report with detailed information when upgrade process is finished. If you have not got second email in next 5 minutes, then probably something went wrong." file=$attachments; + + ## Wait for mail to be send & upgrade + :delay 15s; + + ## Remove backups which we have already sent + :if ($sendBackupToEmail = true && [/tool e-mail get last-status] = "succeeded") do={ + /file remove $attachments; + } + } + + ## Set scheduled task to upgrade routerboard firmware on the next boot, task will be deleted when upgrade is done. (That is why you should keep original script name) + /system schedule add name=BEE-UPGRADE-NEXT-BOOT on-event="/system scheduler remove BEE-UPGRADE-NEXT-BOOT; :global beeGlobalUpdateStep \"routerboardUpgrade\"; :delay 5s; /system script run firmware-updater;" start-time=startup interval=0; + + ## command is reincarnation of the "upgrade" command - doing exactly the same but under a different name + /system package update install; + } +} + + +## Second step (after first reboot) routerboard firmware upgrade +:if ( $updateStep = "routerboardUpgrade") do={ + + ## RouterOS latest, let's check for updated firmware + + :if ( [/system routerboard get current-firmware] != [/system routerboard get upgrade-firmware]) do={ + ## New version of firmware available, let's upgrade + :log info ("Upgrading firmware on router $[/system identity get name], board name: $[/system resource get board-name], serial number: $[/system routerboard get serial-number] | From $[/system routerboard get current-firmware] to $[/system routerboard get upgrade-firmware]"); + + ## Start the upgrading process + /system routerboard upgrade; + + ## Wait until the upgrade is finished + :delay 30s; + + ## Set scheduled task to send final report on the next boot, task will be deleted when is is done. (That is why you should keep original script name) + /system schedule add name=BEE-FINAL-REPORT-NEXT-BOOT on-event="/system scheduler remove BEE-FINAL-REPORT-NEXT-BOOT; :global beeGlobalUpdateStep \"finalReport\"; :delay 10s; /system script run firmware-updater;" start-time=startup interval=0; + + ## Reboot system to boot with new firmware + /system reboot; + } +} + +## Last step (after second reboot) sending final report +:if ( $updateStep = "finalReport") do={ + :log info "Upgrading RouterOS and routerboard firmware finished. Current RouterOS version: $[/system package update get installed-version], routerboard firmware: $[/system routerboard get current-firmware]."; + + :if ($emailEnabled = true) do={ + /tool e-mail send to="$emailAddress" subject="Router: $[/system identity get name] has been upgraded with new FW!" body="Upgrading RouterOS and routerboard firmware finished. \r\n\r\nRouter name: $[/system identity get name]\r\nCurrent RouterOS version: $[/system package update get installed-version]; Routerboard firmware: $[/system routerboard get current-firmware]; Update channel: $[/system package update get channel]; \r\nBoard name: $[/system resource get board-name]; Serial number: $[/system routerboard get serial-number]; \r\n\r\n Changelog: https://mikrotik.com/download/changelogs/current-release-tree"; + } +} + +:log info ("Firmware updater has finished it's job"); \ No newline at end of file diff --git a/howto/email-config.png b/howto/email-config.png new file mode 100644 index 0000000..a29ed51 Binary files /dev/null and b/howto/email-config.png differ diff --git a/howto/scheduler-task.png b/howto/scheduler-task.png new file mode 100644 index 0000000..24eb11b Binary files /dev/null and b/howto/scheduler-task.png differ diff --git a/howto/script-name.png b/howto/script-name.png new file mode 100644 index 0000000..3c0b082 Binary files /dev/null and b/howto/script-name.png differ