Community discussions

MikroTik App
 
juaco
just joined
Topic Author
Posts: 15
Joined: Tue Nov 09, 2010 12:15 am

Config watchdog (toggable auto restore) for remote work

Sat Feb 26, 2011 4:43 am

update: added toggable behaviour
  1. save the script and adjust ScriptName to the script name
  2. set policies
  3. tweak BackupName, WatchdogInterval, WatchdogLoop variables
Run once to backup the config and schedule a restore/reboot, run it again before the scheduler triggers to cancel the scheduler and delete the backup.

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).
# 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"];
};

Who is online

Users browsing this forum: No registered users and 21 guests