Copy current synchronized time/date to flash

Hi all!
Pls help me with the an issue! ROs 5.18.
I need a script which would copy current (synchronized) time/date to default time/date on flash, that after reboot or power loss I have last right time. It saves my old graphs(all graphs before default time/date) from overwrite befor next ntp-synchronization.
I know that device could be off for an hour or even more and there would be some blanks after synchronization… Anyway it’s better than lose all graphs.

Thank you for advice!

That script would set the correct time only after RouterOS is fully booted, and in that case, result would be the same as using NTP client on the router.

And what if after reboot there’s no Internet connection for 5 min more. In this case all graphs would be deleted.
Look close:
Let us pretend it’s “18:00, 06 august 2012” on my mikrotik. Than suddenly my ISP felt down. If I wouldn’t reboot mikrotik it will be ok, it still draws graphs even there’s no Internet connection because time is synchronized. But if I accidentally reboot my router and it couldn’t synchronize with ntp-server because of no Internet connection, in this case it kills all graphs. Time would be Jan/01/2002 and it deletes all graphs since this date. BUT if date would be “18:00, 06 august 2012” it leaves graphs untouched up to this date.

OK I understand. Maybe somebody more skilled in scripting could help.

But still, it is a very rare situation when both the ISP goes down, and also the router is accidentaly rebooted at the same time.

No No No! Here in Ukraine its very common situation :laughing:
Our Electricity Company sucks!
They switch off electricity for half a day and no UPS could keep charge so long.
Or when ISP down some noobie user might reboot mikrotik just in case it helps.

I think making default time to be the same as current(synchronized) time is very clever solution!

Or maybe MT could stop graphs from being updated until NTP is synchronised.

Create this as a script and run it with a scheluder. This may be hard on the NAND, so schedule accordingly, but frequence will be the ‘precision’ of the saved clock.

/ip dns static {
    :local o [find name=clock.localhost]
    :local datetime
    /system clock {
        :set datetime ([get date] . " " . [get time])
    }
    :if ([:len $o] > 0) do={
        set [:pick $o 0] comment=$datetime
    } else={
        add address=127.0.0.1 disabled=no name=clock.localhost ttl=1d comment=$datetime
    }
}

Create this as an other script, and make a scheduler to run it at startup:

/ip dns static {
    :local o [find name=clock.localhost]
    :if ([:len $o] > 0) do={
        :local datetime [get [:pick $o 0] comment]
        /system clock {
            :if ([:len $datetime] > 0 and [:pick [get date] 7 11] = "1970") do={
                set date=[:pick $datetime 0 11]
                set clock=[:pick $datetime 12 20]
            }
        }
    }
}

+1
Quite a few things should get postponed until NTP is synchronised, e.g. I have often noticed that an IPSec VPN gets established before a response from NTP kicks in, so the tunnel has to get recreated after just a few seconds.

How very odd, the NTP package changes the default bootup date, so the second script should be changed to the following:

/ip dns static {
    :local o [find name=clock.localhost]
    :if ([:len $o] > 0) do={
        :local datetime [get [:pick $o 0] comment]
        /system clock {
            :if ([:len $datetime] > 0 and [:pick [get date] 7 11] < "2012") do={
                set date=[:pick $datetime 0 11]
                set clock=[:pick $datetime 12 20]
            }
        }
    }
}

psamsig thx a lot for your help!
Though, I haven’t try your scripts yet =) have no time.
I’d try it in 2 days and report results.