- save the script and adjust ScriptName to the script name
- set policies
- tweak BackupName, WatchdogInterval, WatchdogLoop variables
When the router is back from a restore/reboot, if WatchdogLoop is set to false the script will remove itself from the scheduler to cancel further executions, otherwise it'll keep restoring/rebooting (unless you delete the scheduled entry, either manually or by running the script).
Code: Select all
# policy: read,write,test,policy,sensitive
# GLOBALS ---------------------------------------------------------------------
:global WatchdogRunFromScheduler;
# LOCALS ----------------------------------------------------------------------
:local ScriptName "config-watchdog";
:local BackupName "ConfigWatchdog";
:local WatchdogInterval [:totime "1m"];
:local WatchdogLoop false;
# START ---------------------------------------------------------------------
:if ( [:len [/system scheduler find name="$ScriptName"]] = 0 ) do={
:put "DEBUG: $ScriptName: programming execution in $WatchdogInterval";
:log debug "$ScriptName: programming execution in $WatchdogInterval";
/system scheduler add \
name=$ScriptName \
on-event=" :global WatchdogRunFromScheduler; :if ( [:typeof \$WatchdogRunFromScheduler] = \"nothing\" ) do={ :set WatchdogRunFromScheduler $WatchdogLoop; } else={ :set WatchdogRunFromScheduler true; }; /system script run $ScriptName;" \
interval=$WatchdogInterval \
start-date=[/system clock get date] \
start-time=[/system clock get time] \
policy=[:tostr [/system script get [find name="$ScriptName"] policy ]] \
comment="config watchdog";
:put "DEBUG: $ScriptName: saving configuration...";
:log debug "$ScriptName: saving configuration...";
/system backup save name=$BackupName;
:set WatchdogRunFromScheduler false;
} else={
if ( $WatchdogRunFromScheduler = true ) do={
:put "DEBUG: $ScriptName: programmed execution, restoring backup...";
:log debug "$ScriptName: programmed execution, restoring backup...";
/system backup load name="$BackupName";
};
:put "DEBUG: $ScriptName: removing from scheduler";
:log debug "$ScriptName: removing from scheduler";
/system scheduler remove [find name="$ScriptName"];
:put "DEBUG: $ScriptName: deleting the backup";
:log debug "$ScriptName: deleting the backup";
/file remove [find name="$BackupName.backup"];
};