mirror of
https://github.com/beeyev/Mikrotik-RouterOS-automatic-backup-and-update.git
synced 2025-07-23 04:05:18 +02:00
The script was completely rewritten.
New features were added.
This commit is contained in:
parent
ad422b27b2
commit
4dc1d7e475
2 changed files with 192 additions and 118 deletions
|
@ -3,9 +3,9 @@
|
||||||
#----------SCRIPT INFORMATION---------------------------------------------------
|
#----------SCRIPT INFORMATION---------------------------------------------------
|
||||||
#
|
#
|
||||||
# Script: Mikrotik RouterOS automatic backup & update
|
# Script: Mikrotik RouterOS automatic backup & update
|
||||||
# Version: 20.04.15
|
# Version: 20.04.17
|
||||||
# Created: 07/08/2018
|
# Created: 07/08/2018
|
||||||
# Updated: 15/04/2020
|
# Updated: 17/04/2020
|
||||||
# Author: Alexander Tebiev
|
# Author: Alexander Tebiev
|
||||||
# Website: https://github.com/beeyev
|
# Website: https://github.com/beeyev
|
||||||
# You can contact me by e-mail at tebiev@mail.com
|
# You can contact me by e-mail at tebiev@mail.com
|
||||||
|
@ -19,10 +19,20 @@
|
||||||
:local emailAddress "yourmail@example.com";
|
:local emailAddress "yourmail@example.com";
|
||||||
|
|
||||||
## Script mode, possible values: backup, osupdate, osnotify.
|
## Script mode, possible values: backup, osupdate, osnotify.
|
||||||
# backup - only backup will be perfomed. (default value, if none provided)
|
# backup - Only backup will be performed. (default value, if none provided)
|
||||||
# osupdate - script will make a backup and install new RouterOS if it is available.
|
#
|
||||||
# osnotify - script will make a backup and notify about new RouterOS version.
|
# osupdate - The Script will install a new RouterOS if it is available.
|
||||||
:local scriptMode "osnotify";
|
# It will also create backups before and after update process.
|
||||||
|
# Email will be sent only if a new RouterOS is available.
|
||||||
|
# Change parameter `forceBackup` if you need the script to create backups every time when it runs.
|
||||||
|
#
|
||||||
|
# osnotify - The script will send email notification only (without backups) if a new RouterOS is available.
|
||||||
|
# Change parameter `forceBackup` if you need the script to create backups every time when it runs.
|
||||||
|
:local scriptMode "backup";
|
||||||
|
|
||||||
|
## Additional parameter if you set `scriptMode` to `osupdate` or `osnotify`
|
||||||
|
# Set `true` if you want the script to perform backup every time it's fired, whatever script mode is set.
|
||||||
|
:local forceBackup false;
|
||||||
|
|
||||||
## Backup encryption password, no encryption if no password.
|
## Backup encryption password, no encryption if no password.
|
||||||
:local backupPassword ""
|
:local backupPassword ""
|
||||||
|
@ -39,30 +49,47 @@
|
||||||
## Example: v6.43.6 => major.minor.PATCH
|
## Example: v6.43.6 => major.minor.PATCH
|
||||||
## Script will send information if new version is greater than just patch.
|
## Script will send information if new version is greater than just patch.
|
||||||
:local installOnlyPatchUpdates false;
|
:local installOnlyPatchUpdates false;
|
||||||
|
|
||||||
##------------------------------------------------------------------------------------------##
|
##------------------------------------------------------------------------------------------##
|
||||||
# !!!! DO NOT CHANGE ANYTHING BELOW THIS LINE, IF YOU ARE NOT SURE WHAT YOU ARE DOING !!!! #
|
# !!!! DO NOT CHANGE ANYTHING BELOW THIS LINE, IF YOU ARE NOT SURE WHAT YOU ARE DOING !!!! #
|
||||||
##------------------------------------------------------------------------------------------##
|
##------------------------------------------------------------------------------------------##
|
||||||
|
|
||||||
:log info "\r\nBkp&Upd: script \"Mikrotik RouterOS automatic backup & update\" started.";
|
#Script messages prefix
|
||||||
|
:local SMP "Bkp&Upd:"
|
||||||
|
|
||||||
|
:log info "\r\n$SMP script \"Mikrotik RouterOS automatic backup & update\" started.";
|
||||||
|
:log info "$SMP Script Mode: $scriptMode, forceBackup: $forceBackup";
|
||||||
|
|
||||||
#Check proper email config
|
#Check proper email config
|
||||||
:if ([:len $emailAddress] = 0 or [:len [/tool e-mail get address]] = 0 or [:len [/tool e-mail get from]] = 0) do={
|
:if ([:len $emailAddress] = 0 or [:len [/tool e-mail get address]] = 0 or [:len [/tool e-mail get from]] = 0) do={
|
||||||
:log error ("Bkp&Upd: Email configuration is not correct, please check Tools -> Email. Script stopped.");
|
:log error ("$SMP Email configuration is not correct, please check Tools -> Email. Script stopped.");
|
||||||
:error "Bkp&Upd: bye!";
|
:error "$SMP bye!";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#Check if proper identity name is set
|
||||||
|
if ([:len [/system identity get name]] = 0 or [/system identity get name] = "MikroTik") do={
|
||||||
|
:log warning ("$SMP Please set identity name of your device (System -> Identity), keep it short and informative.");
|
||||||
|
};
|
||||||
|
|
||||||
############### vvvvvvvvv GLOBALS vvvvvvvvv ###############
|
############### vvvvvvvvv GLOBALS vvvvvvvvv ###############
|
||||||
# Function converts standard mikrotik build versions to the number.
|
# Function converts standard mikrotik build versions to the number.
|
||||||
# Possible arguments: osVer
|
# Possible arguments: paramOsVer
|
||||||
# Example:
|
# Example:
|
||||||
# :put [$brGlobalFuncGetOsVerNum osVer=[/system routerboard get current-RouterOS]];
|
# :put [$buGlobalFuncGetOsVerNum paramOsVer=[/system routerboard get current-RouterOS]];
|
||||||
# result will be: 64301, because current RouterOS version is: 6.43.1
|
# result will be: 64301, because current RouterOS version is: 6.43.1
|
||||||
:global brGlobalFuncGetOsVerNum do={
|
:global buGlobalFuncGetOsVerNum do={
|
||||||
|
:local osVer $paramOsVer;
|
||||||
:local osVerNum;
|
:local osVerNum;
|
||||||
:local osVerMicroPart;
|
:local osVerMicroPart;
|
||||||
:local zro 0;
|
:local zro 0;
|
||||||
:local tmp;
|
:local tmp;
|
||||||
|
|
||||||
|
# Replace word `beta` with dot
|
||||||
|
:local isBetaPos [:tonum [:find $osVer "beta" 0]];
|
||||||
|
:if ($isBetaPos > 1) do={
|
||||||
|
:set osVer ([:pick $osVer 0 $isBetaPos] . "." . [:pick $osVer ($isBetaPos + 4) [:len $osVer]]);
|
||||||
|
}
|
||||||
|
|
||||||
:local dotPos1 [:find $osVer "." 0];
|
:local dotPos1 [:find $osVer "." 0];
|
||||||
|
|
||||||
:if ($dotPos1 > 0) do={
|
:if ($dotPos1 > 0) do={
|
||||||
|
@ -99,37 +126,80 @@
|
||||||
:return $osVerNum;
|
:return $osVerNum;
|
||||||
}
|
}
|
||||||
|
|
||||||
:global brGlobalVarUpdateStep;
|
# Function creates backups (system and config) and returns array with names
|
||||||
|
# Possible arguments:
|
||||||
|
# `backupName` | string | backup file name, without extension!
|
||||||
|
# `backupPassword` | string |
|
||||||
|
# `sensetiveDataInConfig` | boolean |
|
||||||
|
# Example:
|
||||||
|
# :put [$buGlobalFuncCreateBackups name="daily-backup"];
|
||||||
|
:global buGlobalFuncCreateBackups do={
|
||||||
|
:log info ("$SMP Global function \"buGlobalFuncCreateBackups\" was fired.");
|
||||||
|
|
||||||
|
:local backupFileSys "$backupName.backup";
|
||||||
|
:local backupFileConfig "$backupName.rsc";
|
||||||
|
:local backupNames {$backupFileSys;$backupFileConfig};
|
||||||
|
|
||||||
|
## Make system backup
|
||||||
|
:if ([:len $backupPassword] = 0) do={
|
||||||
|
/system backup save dont-encrypt=yes name=$backupName;
|
||||||
|
} else={
|
||||||
|
/system backup save password=$backupPassword name=$backupName;
|
||||||
|
}
|
||||||
|
:log info ("$SMP System backup created. $backupFileSys");
|
||||||
|
|
||||||
|
## Export config file
|
||||||
|
:if ($sensetiveDataInConfig = true) do={
|
||||||
|
/export compact file=$backupName;
|
||||||
|
} else={
|
||||||
|
/export compact hide-sensitive file=$backupName;
|
||||||
|
}
|
||||||
|
:log info ("$SMP Config file was exported. $backupFileConfig");
|
||||||
|
|
||||||
|
#Delay after creating backups
|
||||||
|
:delay 5s;
|
||||||
|
:return $backupNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
:global buGlobalVarUpdateStep;
|
||||||
############### ^^^^^^^^^ GLOBALS ^^^^^^^^^ ###############
|
############### ^^^^^^^^^ GLOBALS ^^^^^^^^^ ###############
|
||||||
|
|
||||||
#Current date time in format: 2020jan15-221324
|
#Current date time in format: 2020jan15-221324
|
||||||
:local dateTime ([: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]);
|
:local dateTime ([: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]);
|
||||||
|
|
||||||
:local deviceOsVerInst [/system package update get installed-version];
|
:local deviceOsVerInst [/system package update get installed-version];
|
||||||
:local deviceOsVerInstNum [$brGlobalFuncGetOsVerNum osVer=$deviceOsVerInst];
|
:local deviceOsVerInstNum [$buGlobalFuncGetOsVerNum paramOsVer=$deviceOsVerInst];
|
||||||
:local deviceOsVerAvail "";
|
:local deviceOsVerAvail "";
|
||||||
:local deviceOsVerAvailNum 0;
|
:local deviceOsVerAvailNum 0;
|
||||||
:local deviceRbModel [/system routerboard get model];
|
:local deviceRbModel [/system routerboard get model];
|
||||||
:local deviceRbSerialNumber [/system routerboard get serial-number];
|
:local deviceRbSerialNumber [/system routerboard get serial-number];
|
||||||
:local deviceRbCurrentFw [/system routerboard get current-firmware];
|
:local deviceRbCurrentFw [/system routerboard get current-firmware];
|
||||||
:local deviceRbUpgradeFw [/system routerboard get upgrade-firmware];
|
:local deviceRbUpgradeFw [/system routerboard get upgrade-firmware];
|
||||||
:local deviceIdentityName [/system identity get name];
|
:local deviceIdentityName [/system identity get name];
|
||||||
:local deviceUpdateChannel [/system package update get channel];
|
:local deviceIdentityNameShort [:pick $deviceIdentityName 0 18]
|
||||||
|
:local deviceUpdateChannel [/system package update get channel];
|
||||||
|
|
||||||
:local isOsUpdateAvailable false;
|
:local isOsUpdateAvailable false;
|
||||||
:local isOsNeedsToBeUpdated false;
|
:local isOsNeedsToBeUpdated false;
|
||||||
|
|
||||||
:local mailSubject "";
|
:local isSendEmailRequired true;
|
||||||
|
|
||||||
|
:local mailSubject "$SMP Device - $deviceIdentityNameShort.";
|
||||||
:local mailBody "";
|
:local mailBody "";
|
||||||
|
|
||||||
:local mailBodyDeviceInfo "\r\n\r\nDevice information: \r\nIdentity: $deviceIdentityName \r\nModel: $deviceRbModel \r\nSerial number: $deviceRbSerialNumber \r\nCurrent RouterOS: $deviceOsVerInst ($[/system package update get channel]) $[/system resource get build-time] \r\nCurrent routerboard FW: $deviceRbCurrentFw \r\nDevice uptime: $[/system resource get uptime]";
|
:local mailBodyDeviceInfo "\r\n\r\nDevice information: \r\nIdentity: $deviceIdentityName \r\nModel: $deviceRbModel \r\nSerial number: $deviceRbSerialNumber \r\nCurrent RouterOS: $deviceOsVerInst ($[/system package update get channel]) $[/system resource get build-time] \r\nCurrent routerboard FW: $deviceRbCurrentFw \r\nDevice uptime: $[/system resource get uptime]";
|
||||||
:local mailBodyCopyright "\r\n\r\nMikrotik RouterOS automatic backup & update \r\nhttps://github.com/beeyev/Mikrotik-RouterOS-automatic-backup-and-update";
|
:local mailBodyCopyright "\r\n\r\nMikrotik RouterOS automatic backup & update \r\nhttps://github.com/beeyev/Mikrotik-RouterOS-automatic-backup-and-update";
|
||||||
:local changelogUrl ("Check RouterOS changelog: https://mikrotik.com/download/changelogs/" . $updateChannel . "-release-tree");
|
:local changelogUrl ("Check RouterOS changelog: https://mikrotik.com/download/changelogs/" . $updateChannel . "-release-tree");
|
||||||
|
|
||||||
|
:local backupName "$deviceIdentityName.$deviceRbModel.$deviceRbSerialNumber.v$deviceOsVerInst.$deviceUpdateChannel.$dateTime";
|
||||||
|
:local backupNameBeforeUpd "backup_before_update_$backupName";
|
||||||
|
:local backupNameAfterUpd "backup_after_update_$backupName";
|
||||||
|
|
||||||
|
:local backupNameFinal $backupName;
|
||||||
:local mailAttachments [:toarray ""];
|
:local mailAttachments [:toarray ""];
|
||||||
|
|
||||||
:local updateStep $brGlobalVarUpdateStep;
|
:local updateStep $buGlobalVarUpdateStep;
|
||||||
:do {/system script environment remove brGlobalVarUpdateStep;} on-error={}
|
:do {/system script environment remove buGlobalVarUpdateStep;} on-error={}
|
||||||
:if ([:len $updateStep] = 0) do={
|
:if ([:len $updateStep] = 0) do={
|
||||||
:set updateStep 1;
|
:set updateStep 1;
|
||||||
}
|
}
|
||||||
|
@ -138,40 +208,11 @@
|
||||||
## STEP ONE: Creating backups, checking for new RouterOs version and sending email with backups,
|
## STEP ONE: Creating backups, checking for new RouterOs version and sending email with backups,
|
||||||
## steps 2 and 3 are fired only if script is set to automatically update device and if new RouterOs is available.
|
## steps 2 and 3 are fired only if script is set to automatically update device and if new RouterOs is available.
|
||||||
:if ($updateStep = 1) do={
|
:if ($updateStep = 1) do={
|
||||||
:log info ("Bkp&Upd: Performing the first step.");
|
:log info ("$SMP Performing the first step.");
|
||||||
|
|
||||||
# Set email subject and body for the first step
|
|
||||||
:set mailSubject "Bkp&Upd: Backup completed - $[:pick $deviceIdentityName 0 18].";
|
|
||||||
:set mailBody "\"$deviceIdentityName\" system backups were created and attached to this email.";
|
|
||||||
|
|
||||||
## PREPARE BACKUP DATA
|
|
||||||
:log info ("Bkp&Upd: Creating system backups.");
|
|
||||||
|
|
||||||
:local bname "$deviceIdentityName.$deviceRbModel.$deviceRbSerialNumber.v$deviceOsVerInst.$deviceUpdateChannel.$dateTime";
|
|
||||||
:local backupFileSys "$bname.backup";
|
|
||||||
:local backupFileConfig "$bname.rsc";
|
|
||||||
|
|
||||||
:set mailAttachments {$backupFileSys;$backupFileConfig};
|
|
||||||
|
|
||||||
## Make system backup
|
|
||||||
:if ([:len $backupPassword] = 0) do={
|
|
||||||
/system backup save dont-encrypt=yes name=$bname;
|
|
||||||
} else={
|
|
||||||
/system backup save password=$backupPassword name=$bname;
|
|
||||||
}
|
|
||||||
:log info ("Bkp&Upd: System backup created. $backupFileSys");
|
|
||||||
|
|
||||||
## Export config file
|
|
||||||
:if ($sensetiveDataInConfig = true) do={
|
|
||||||
/export compact file=$bname;
|
|
||||||
} else={
|
|
||||||
/export compact hide-sensitive file=$bname;
|
|
||||||
}
|
|
||||||
:log info ("Bkp&Upd: Config file was exported. $backupFileConfig");
|
|
||||||
|
|
||||||
# Checking for new RouterOS version
|
# Checking for new RouterOS version
|
||||||
if ($scriptMode = "osupdate" or $scriptMode = "osnotify") do={
|
if ($scriptMode = "osupdate" or $scriptMode = "osnotify") do={
|
||||||
log info ("Bkp&Upd: Checking for new RouterOS version. Current version is: $deviceOsVerInst");
|
log info ("$SMP Checking for new RouterOS version. Current version is: $deviceOsVerInst");
|
||||||
/system package update set channel=$updateChannel;
|
/system package update set channel=$updateChannel;
|
||||||
/system package update check-for-updates;
|
/system package update check-for-updates;
|
||||||
:delay 5s;
|
:delay 5s;
|
||||||
|
@ -179,35 +220,39 @@
|
||||||
|
|
||||||
# If there is a problem getting information about available RouterOS from server
|
# If there is a problem getting information about available RouterOS from server
|
||||||
:if ([:len $deviceOsVerAvail] = 0) do={
|
:if ([:len $deviceOsVerAvail] = 0) do={
|
||||||
:log warning ("Bkp&Upd: There is a problem getting information about new RouterOS from server.");
|
:log warning ("$SMP There is a problem getting information about new RouterOS from server.");
|
||||||
:set mailSubject ($mailSubject . " Error: No data about new RouterOS!")
|
:set mailSubject ($mailSubject . " Error: No data about new RouterOS!")
|
||||||
:set mailBody ($mailBody . "\r\n\r\nError occured! \r\nMikrotik couldn't get any information about new RouterOS from server! \r\nWatch additional information in device logs.")
|
:set mailBody ($mailBody . "Error occured! \r\nMikrotik couldn't get any information about new RouterOS from server! \r\nWatch additional information in device logs.")
|
||||||
} else={
|
} else={
|
||||||
#Get numeric version of OS
|
#Get numeric version of OS
|
||||||
:set deviceOsVerAvailNum [$brGlobalFuncGetOsVerNum osVer=$deviceOsVerAvail];
|
:set deviceOsVerAvailNum [$buGlobalFuncGetOsVerNum paramOsVer=$deviceOsVerAvail];
|
||||||
|
|
||||||
# Checking if OS on server is greater than installed one.
|
# Checking if OS on server is greater than installed one.
|
||||||
:if ($deviceOsVerAvailNum > $deviceOsVerInstNum) do={
|
:if ($deviceOsVerAvailNum > $deviceOsVerInstNum) do={
|
||||||
:set isOsUpdateAvailable true;
|
:set isOsUpdateAvailable true;
|
||||||
:log info ("Bkp&Upd: New RouterOS is available! $deviceOsVerAvail");
|
:log info ("$SMP New RouterOS is available! $deviceOsVerAvail");
|
||||||
} else={
|
} else={
|
||||||
:set mailSubject ($mailSubject . " No new OS updates.")
|
:set isSendEmailRequired false;
|
||||||
:set mailBody ($mailBody . "\r\nYour system is up to date.")
|
:log info ("$SMP System is already up to date.");
|
||||||
:log info ("Bkp&Upd: System is already up to date.")
|
:set mailSubject ($mailSubject . " No new OS updates.");
|
||||||
|
:set mailBody ($mailBody . "Your system is up to date.");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} else={
|
} else={
|
||||||
#Delay after creating backups
|
:set scriptMode "backup";
|
||||||
:delay 5s;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if ($forceBackup = true) do={
|
||||||
|
# In this case the script will always send email, because it has to create backups
|
||||||
|
:set isSendEmailRequired true;
|
||||||
|
}
|
||||||
|
|
||||||
# if new OS version is available to install
|
# if new OS version is available to install
|
||||||
if ($isOsUpdateAvailable = true) do={
|
if ($isOsUpdateAvailable = true and $isSendEmailRequired = true) do={
|
||||||
# If we only need to notify about new available version
|
# If we only need to notify about new available version
|
||||||
if ($scriptMode = "osnotify") do={
|
if ($scriptMode = "osnotify") do={
|
||||||
:set mailSubject ($mailSubject . " New RouterOS is available! v.$deviceOsVerAvail")
|
:set mailSubject ($mailSubject . " New RouterOS is available! v.$deviceOsVerAvail.")
|
||||||
:set mailBody ($mailBody . "\r\n\r\nNew RouterOS version is available to install: v.$deviceOsVerAvail ($updateChannel) \r\n$changelogUrl")
|
:set mailBody ($mailBody . "New RouterOS version is available to install: v.$deviceOsVerAvail ($updateChannel) \r\n$changelogUrl")
|
||||||
}
|
}
|
||||||
|
|
||||||
# if we need to initiate RouterOs update process
|
# if we need to initiate RouterOs update process
|
||||||
|
@ -217,105 +262,134 @@
|
||||||
:if ($installOnlyPatchUpdates = true) do={
|
:if ($installOnlyPatchUpdates = true) do={
|
||||||
#Check if Major and Minor builds are the same.
|
#Check if Major and Minor builds are the same.
|
||||||
:if ([:pick $deviceOsVerInstNum 0 ([:len $deviceOsVerInstNum]-2)] = [:pick $deviceOsVerAvailNum 0 ([:len $deviceOsVerAvailNum]-2)]) do={
|
:if ([:pick $deviceOsVerInstNum 0 ([:len $deviceOsVerInstNum]-2)] = [:pick $deviceOsVerAvailNum 0 ([:len $deviceOsVerAvailNum]-2)]) do={
|
||||||
:log info ("Bkp&Upd: New patch version of RouterOS firmware is available.");
|
:log info ("$SMP New patch version of RouterOS firmware is available.");
|
||||||
} else={
|
} else={
|
||||||
:log info ("Bkp&Upd: New minor version of RouterOS firmware is available. You need to update it manually.");
|
:log info ("$SMP New major or minor version of RouterOS firmware is available. You need to update it manually.");
|
||||||
:set mailSubject ($mailSubject . " New major RouterOS is available: v.$deviceOsVerAvail!");
|
:set mailSubject ($mailSubject . " New RouterOS: v.$deviceOsVerAvail needs to be installed manually.");
|
||||||
:set mailBody ($mailBody . "\r\n\r\nNew major RouterOS version is available to install: v.$deviceOsVerAvail ($updateChannel). \r\nYou chose to automatically install only patch updates, so this major update you need to install manually. \r\n$changelogUrl");
|
:set mailBody ($mailBody . "New major or minor RouterOS version is available to install: v.$deviceOsVerAvail ($updateChannel). \r\nYou chose to automatically install only patch updates, so this major update you need to install manually. \r\n$changelogUrl");
|
||||||
:set isOsNeedsToBeUpdated false;
|
:set isOsNeedsToBeUpdated false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#Check again, because this variable could be changed during checking for installing only patch updats
|
#Check again, because this variable could be changed during checking for installing only patch updats
|
||||||
if ($isOsNeedsToBeUpdated = true) do={
|
if ($isOsNeedsToBeUpdated = true) do={
|
||||||
:set mailSubject ($mailSubject . " New RouterOS is going to be installed! v.$deviceOsVerInst -> v.$deviceOsVerAvail");
|
:log info ("$SMP New RouterOS is going to be installed! v.$deviceOsVerInst -> v.$deviceOsVerAvail");
|
||||||
:set mailBody ($mailBody . "\r\n\r\nYour Mikrotik will be updated to the new RouterOS version from v.$deviceOsVerInst to v.$deviceOsVerAvail (Update channel: $updateChannel) \r\nFinal report with detailed information will be sent when update process is completed. \r\nIf you have not received second email in the next 5 minutes, then probably something went wrong.");
|
:set mailSubject ($mailSubject . " New RouterOS is going to be installed! v.$deviceOsVerInst -> v.$deviceOsVerAvail.");
|
||||||
|
:set mailBody ($mailBody . "Your Mikrotik will be updated to the new RouterOS version from v.$deviceOsVerInst to v.$deviceOsVerAvail (Update channel: $updateChannel) \r\nFinal report with the detailed information will be sent when update process is completed. \r\nIf you have not received second email in the next 5 minutes, then probably something went wrong. (Check your device logs)");
|
||||||
#!! There is more code connected to this part and first step at the end of the script.
|
#!! There is more code connected to this part and first step at the end of the script.
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
## Checking If the script needs to create a backup
|
||||||
|
:log info ("$SMP Checking If the script needs to create a backup.");
|
||||||
|
if ($forceBackup = true or $scriptMode = "backup" or $isOsNeedsToBeUpdated = true) do={
|
||||||
|
:log info ("$SMP Creating system backups.");
|
||||||
|
if ($isOsNeedsToBeUpdated = true) do={
|
||||||
|
:set backupNameFinal $backupNameBeforeUpd;
|
||||||
|
};
|
||||||
|
if ($scriptMode != "backup") do={
|
||||||
|
:set mailBody ($mailBody . "\r\n\r\n");
|
||||||
|
};
|
||||||
|
|
||||||
|
:set mailSubject ($mailSubject . " Backup was created.");
|
||||||
|
:set mailBody ($mailBody . "System backups were created and attached to this email.");
|
||||||
|
|
||||||
|
:set mailAttachments [$buGlobalFuncCreateBackups backupName=$backupNameFinal backupPassword=$backupPassword sensetiveDataInConfig=$sensetiveDataInConfig];
|
||||||
|
} else={
|
||||||
|
:log info ("$SMP There is no need to create a backup.");
|
||||||
|
}
|
||||||
|
|
||||||
# Combine fisrst step email
|
# Combine fisrst step email
|
||||||
:set mailBody ($mailBody . $mailBodyDeviceInfo . $mailBodyCopyright);
|
:set mailBody ($mailBody . $mailBodyDeviceInfo . $mailBodyCopyright);
|
||||||
:log info ("Bkp&Upd: Sending email with backups in attachment.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Remove function brGlobalFuncGetOsVerNum from global environment to keep it fresh and clean.
|
|
||||||
:do {/system script environment remove brGlobalFuncGetOsVerNum;} on-error={}
|
|
||||||
|
|
||||||
## STEP TWO: (after first reboot) routerboard firmware upgrade
|
## STEP TWO: (after first reboot) routerboard firmware upgrade
|
||||||
## steps 2 and 3 are fired only if script is set to automatically update device and if new RouterOs is available.
|
## steps 2 and 3 are fired only if script is set to automatically update device and if new RouterOs is available.
|
||||||
:if ($updateStep = 2) do={
|
:if ($updateStep = 2) do={
|
||||||
:log info ("Bkp&Upd: Performing the second step.");
|
:log info ("$SMP Performing the second step.");
|
||||||
## RouterOS is the latest, let's check for upgraded routerboard firmware
|
## RouterOS is the latest, let's check for upgraded routerboard firmware
|
||||||
if ($deviceRbCurrentFw != $deviceRbUpgradeFw) do={
|
if ($deviceRbCurrentFw != $deviceRbUpgradeFw) do={
|
||||||
|
:set isSendEmailRequired false;
|
||||||
:delay 10s;
|
:delay 10s;
|
||||||
:log info "Bkp&Upd: Upgrading routerboard firmware from v.$deviceRbCurrentFw to v.$deviceRbUpgradeFw";
|
:log info "$SMP Upgrading routerboard firmware from v.$deviceRbCurrentFw to v.$deviceRbUpgradeFw";
|
||||||
## Start the upgrading process
|
## Start the upgrading process
|
||||||
/system routerboard upgrade;
|
/system routerboard upgrade;
|
||||||
## Wait until the upgrade is completed
|
## Wait until the upgrade is completed
|
||||||
:delay 5s;
|
:delay 5s;
|
||||||
:log info "Bkp&Upd: routerboard upgrading process was completed, going to reboot in a moment!";
|
:log info "$SMP routerboard upgrade process was completed, going to reboot in a moment!";
|
||||||
## 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)
|
## 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=BKPUPD-FINAL-REPORT-ON-NEXT-BOOT on-event=":delay 5s; /system scheduler remove BKPUPD-FINAL-REPORT-ON-NEXT-BOOT; :global brGlobalVarUpdateStep 3; :delay 10s; /system script run BackupAndUpdate;" start-time=startup interval=0;
|
/system schedule add name=BKPUPD-FINAL-REPORT-ON-NEXT-BOOT on-event=":delay 5s; /system scheduler remove BKPUPD-FINAL-REPORT-ON-NEXT-BOOT; :global buGlobalVarUpdateStep 3; :delay 10s; /system script run BackupAndUpdate;" start-time=startup interval=0;
|
||||||
## Reboot system to boot with new firmware
|
## Reboot system to boot with new firmware
|
||||||
/system reboot;
|
/system reboot;
|
||||||
}
|
} else={
|
||||||
|
:log info "$SMP It appers that your routerboard is already up to date, skipping this step.";
|
||||||
|
:set updateStep 3;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
## STEP THREE: Last step (after second reboot) sending final report
|
## STEP THREE: Last step (after second reboot) sending final report
|
||||||
## steps 2 and 3 are fired only if script is set to automatically update device and if new RouterOs is available.
|
## steps 2 and 3 are fired only if script is set to automatically update device and if new RouterOs is available.
|
||||||
:if ($updateStep = 3) do={
|
:if ($updateStep = 3) do={
|
||||||
:log info "Bkp&Upd: RouterOS and routerboard upgrading process of was completed. New RouterOS version: v.$deviceOsVerInst, routerboard firmware: v.$deviceRbCurrentFw.";
|
:log info ("$SMP Performing the third step.");
|
||||||
|
:log info "Bkp&Upd: RouterOS and routerboard upgrade process was completed. New RouterOS version: v.$deviceOsVerInst, routerboard firmware: v.$deviceRbCurrentFw.";
|
||||||
## Small delay in case mikrotik needs some time to initialize connections
|
## Small delay in case mikrotik needs some time to initialize connections
|
||||||
|
:log info "$SMP The final email with report and backups of upgraded system will be sent in a minute.";
|
||||||
:delay 1m;
|
:delay 1m;
|
||||||
:set mailSubject "Bkp&Upd: Router - $[:pick $deviceIdentityName 0 18] has been upgraded to the new RouterOS v.$deviceOsVerInst!";
|
:set mailSubject ($mailSubject . " RouterOS Upgrade is completed, new version: v.$deviceOsVerInst!");
|
||||||
:set mailBody "RouterOS and routerboard upgrading process was completed. \r\nNew RouterOS version: v.$deviceOsVerInst, routerboard firmware: v.$deviceRbCurrentFw. \r\n$changelogUrl $mailBodyDeviceInfo $mailBodyCopyright";
|
:set mailBody "RouterOS and routerboard upgrade process was completed. \r\nNew RouterOS version: v.$deviceOsVerInst, routerboard firmware: v.$deviceRbCurrentFw. \r\n$changelogUrl \r\n\r\nBackups of the upgraded system are in the attachment of this email. $mailBodyDeviceInfo $mailBodyCopyright";
|
||||||
|
:set mailAttachments [$buGlobalFuncCreateBackups backupName=$backupNameAfterUpd backupPassword=$backupPassword sensetiveDataInConfig=$sensetiveDataInConfig];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Remove functions from global environment to keep it fresh and clean.
|
||||||
|
:do {/system script environment remove buGlobalFuncGetOsVerNum;} on-error={}
|
||||||
|
:do {/system script environment remove buGlobalFuncCreateBackups;} on-error={}
|
||||||
|
|
||||||
##
|
##
|
||||||
## SENDING EMAIL
|
## SENDING EMAIL
|
||||||
##
|
##
|
||||||
# Trying to send email with backups in attachment.
|
# Trying to send email with backups in attachment.
|
||||||
:if ($updateStep = 1 or $updateStep = 3) do={
|
|
||||||
:log info "Bkp&Upd: Sending email message...";
|
|
||||||
:do {/tool e-mail send to=$emailAddress subject=$mailSubject body=$mailBody file=$mailAttachments;} on-error={
|
|
||||||
:log error "Bkp&Upd: could not send email message ($[/tool e-mail get last-status]). Going to try it again in a while."
|
|
||||||
:delay 5m;
|
|
||||||
:do {/tool e-mail send to=$emailAddress subject=$mailSubject body=$mailBody file=$mailAttachments;} on-error={
|
|
||||||
:log error "Bkp&Upd: could not send email message ($[/tool e-mail get last-status]) for the second time."
|
|
||||||
|
|
||||||
if ($isOsNeedsToBeUpdated = true) do={
|
:if ($isSendEmailRequired = true) do={
|
||||||
:log warning "Bkp&Upd: script didn't initialise update process due to inability to send backups to email."
|
:log info "$SMP Sending email message, it will take around half a minute...";
|
||||||
}
|
:do {/tool e-mail send to=$emailAddress subject=$mailSubject body=$mailBody file=$mailAttachments;} on-error={
|
||||||
|
:delay 5s;
|
||||||
|
:log error "$SMP could not send email message ($[/tool e-mail get last-status]). Going to try it again in a while."
|
||||||
|
|
||||||
|
:delay 5m;
|
||||||
|
|
||||||
|
:do {/tool e-mail send to=$emailAddress subject=$mailSubject body=$mailBody file=$mailAttachments;} on-error={
|
||||||
|
:delay 5s;
|
||||||
|
:log error "$SMP could not send email message ($[/tool e-mail get last-status]) for the second time."
|
||||||
|
|
||||||
|
if ($isOsNeedsToBeUpdated = true) do={
|
||||||
|
:set isOsNeedsToBeUpdated false;
|
||||||
|
:log warning "$SMP script is not goint to initialise update process due to inability to send backups to email."
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
:delay 30s;
|
||||||
|
|
||||||
|
:if ([:len $mailAttachments] > 0 and [/tool e-mail get last-status] = "succeeded") do={
|
||||||
|
:log info "$SMP File system cleanup."
|
||||||
|
/file remove $mailAttachments;
|
||||||
|
:delay 2s;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Waiting for email to be sent
|
|
||||||
:delay 30s;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Fire RouterOs update process
|
||||||
|
if ($isOsNeedsToBeUpdated = true) do={
|
||||||
|
|
||||||
## Remove backups which we have already sent during first step
|
## 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)
|
||||||
:if ($updateStep = 1 and [/tool e-mail get last-status] = "succeeded") do={
|
/system schedule add name=BKPUPD-UPGRADE-ON-NEXT-BOOT on-event=":delay 5s; /system scheduler remove BKPUPD-UPGRADE-ON-NEXT-BOOT; :global buGlobalVarUpdateStep 2; :delay 10s; /system script run BackupAndUpdate;" start-time=startup interval=0;
|
||||||
:log info "Bkp&Upd: File system cleanup."
|
|
||||||
/file remove $mailAttachments;
|
:log info "$SMP everything is ready to install new RouterOS, going to reboot in a moment!"
|
||||||
:delay 2s;
|
## command is reincarnation of the "upgrade" command - doing exactly the same but under a different name
|
||||||
|
/system package update install;
|
||||||
# Fire update process only if backups were successfully sent
|
|
||||||
if ($isOsNeedsToBeUpdated = true) do={
|
|
||||||
|
|
||||||
## 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=BKPUPD-UPGRADE-ON-NEXT-BOOT on-event=":delay 5s; /system scheduler remove BKPUPD-UPGRADE-ON-NEXT-BOOT; :global brGlobalVarUpdateStep 2; :delay 10s; /system script run BackupAndUpdate;" start-time=startup interval=0;
|
|
||||||
|
|
||||||
:log info "Bkp&Upd: everything is ready to install new RouterOS, going to reboot in a moment!"
|
|
||||||
## command is reincarnation of the "upgrade" command - doing exactly the same but under a different name
|
|
||||||
/system package update install;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
:log info "$SMP script \"Mikrotik RouterOS automatic backup & update\" completed it's job.\r\n";
|
||||||
:log info "Bkp&Upd: script \"Mikrotik RouterOS automatic backup & update\" completed it's job.\r\n";
|
|
|
@ -15,7 +15,7 @@ This script provides an ability to create Mikrotik's daily backups to email. You
|
||||||
## Script operating modes:
|
## Script operating modes:
|
||||||
**Backups only** - script creates system and config backups and sends them to specified email as an attachment. Using email account as storage for your backups.
|
**Backups only** - script creates system and config backups and sends them to specified email as an attachment. Using email account as storage for your backups.
|
||||||
**Backups and notifications about new RouterOS release** - Except backups, script also checks for new RouterOS firmware release and provides this information in the email.
|
**Backups and notifications about new RouterOS release** - Except backups, script also checks for new RouterOS firmware release and provides this information in the email.
|
||||||
**Backups and automatic RouterOS upgrade** - Script makes a backup, then checks for new RouterOS version, and if new firmware released, script will initiate upgrade process. By the end, you receive two emails. The first one contains system backups of the previous RouterOS version, the second message will be sent when the upgrade process is done.
|
**Backups and automatic RouterOS upgrade** - Script makes a backup, then checks for new RouterOS version, and if new firmware released, script will initiate upgrade process. By the end, you receive two emails. The first one contains system backups of the previous RouterOS version, the second message will be sent when the upgrade process is done (including backups of the updated system).
|
||||||
|
|
||||||
## How to use
|
## How to use
|
||||||
##### 1. Configure parameters
|
##### 1. Configure parameters
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue