Hello
i want script to save current date and time in text file and another script to restore date and time from this text file after reboot
You will wear out the flash doing this.
Why would you want to do that ?
Use NTP client.
Or IP / Cloud.
Much easier and more accurate.
ntp update date and time only one time, and save this value if router reboot then start with this saved value .. if not found internet as my case to update the time it will running with this wrong saved time and make many problems
so i want to save always currect time
sorry i can not understand
How do you know the date is right when you save the file?
The fact that you create the file already assigns it the current date and time,
so in recovery just read the date and time of the file,
without worrying about reading or writing the content.
The most obvious thing is to let the NTP client do its job.
No, it does check frequently to update time.
That’s the whole point of using NTP process.
That’s true but how do you know the time you write back is correct ? You don’t…
Besides, if you have no internet, I think you have other problems then only wrong timestamps.
Hi.
You can run this on startup and then call it as a function after some NTP events.
It is possible to use a log parser to analyze NTP messages.
So, you need a script that will monitor NTP events and call this one ![]()
:global $SaveNTP; $SaveNTP YourMonitorScriptName
This creates a csv file “SaveNTP.csv” “YourMonitorScriptName” as argument.
You will see an error messages “YourMonitorScriptName SaveNTP …” in the system log if something wrong.
# SaveNTP function
:global SaveNTP do={
:local scriptName "SaveNTP";
:local fileName "$scriptName.csv";
:local fileMessage "";
:local errorMessage "";
/log debug "$1 $scriptName started";
# file add
do {
:if ([/file find name=$fileName]) do={} else={/file add name=$fileName};
} on-error={
:set errorMessage "$1 $scriptName file add error";
/log warning $errorMessage;
:return false;
};
# message
:do {
:local routerName [/system identity get name];
:local uptime [/system resource get uptime];
:local date [/system clock get date];
:local time [/system clock get time];
:set fileMessage "$routerName;$uptime;$date;$time"
} on-error={
:set errorMessage "$1 $scriptName message error";
/log warning $errorMessage;
:return false;
};
# file set
:do {
:if ([/file find name=$fileName]) do={
/file set $fileName contents=$fileMessage;
} else={
:set errorMessage "$scriptName file find error";
/log warning $errorMessage;
:return false;
};
/log debug "$scriptName executed";
:return true;
} on-error={
:set errorMessage "$scriptName file set error";
/log warning $errorMessage;
:return false;
};
};
Another idea is to store the data not with a separator, but with the name of the variable for import: http://forum.mikrotik.com/t/variables-from-a-file/34451/1
:local fileName "$scriptName.txt";
...
:set fileMessage "routerName=$routerName\r\nuptime=$uptime\r\ndate=$date\r\ntime=$time"
PS
Since you’ll need a system log parser or other NTP event monitor, just don’t do anything that requires accurate time. Until the time is synchronized. Why do you need an outdated file? ![]()
# SaveNTP
:local scriptName "SaveNTP";
:local fileName "$scriptName.csv";
:local fileMessage [/system clock get];
:local errorMessage "";
/log debug "$scriptName started";
# file add
do {
:if ([/file find name=$fileName]) do={} else={/file add name=$fileName};
} on-error={
:set errorMessage "$scriptName file add error";
/log warning $errorMessage;
:return false;
};
:delay 1s;
# file set
:do {
:if ([/file find name=$fileName]) do={
/file set $fileName contents=$fileMessage;
} else={
:set errorMessage "$scriptName file find error";
/log warning $errorMessage;
:return false;
};
/log debug "$scriptName executed";
:return true;
} on-error={
:set errorMessage "$scriptName file set error";
/log warning $errorMessage;
:return false;
};
# LoadNTP
:local scriptName "LoadNTP";
:local fileName "SaveNTP.csv";
:local errorMessage "";
:local fileContents "";
/log debug "$scriptName started";
# LoadValue function
: local LoadValue do={
:local start [:find $1 "$2="];
:set start ($start + [:len $2] + 1);
:local end [:find from=$start $1 ";"];
:return [:pick $1 $start $end];
};
# file contents
:do {
:if ([/file find name=$fileName]) do={
:set fileContents [/file get [/file find name=$fileName] contents];
} else={
:set errorMessage "$scriptName file find error";
/log warning $errorMessage;
:return false;
};
} on-error={
:set errorMessage "$scriptName file contents error";
/log warning $errorMessage;
:return false;
};
# date time set
:do {
/system/clock/set date=[$LoadValue $fileContents date];
/system/clock/set time=[:totime [$LoadValue $fileContents ";time"]];
/log debug "$scriptName executed";
:return true;
} on-error={
:set errorMessage "$scriptName date time set error";
/log warning $errorMessage;
:return false;
};
too many useless steps…
as I already said, just create the file, and then re-read the time and date of the file,
without wasting time writing and reading inside the file…
It is better to run time-sensitive scripts after checking the NTP status:
/system/ntp/client/get status as-string
ROS saves current time on user initiated reboot or shutdown, unless is power outage. In such case not sure how practical is to restore some interval saved time since it will be wrong anyway because you don’t know how long device was off. If this is because of some PKI then you will need to setup NTP sync over LAN or WAN to ensure that time drifts cannot cause issues with certificate validation. If you cannot setup NTP for some reason on your network, use at least UPS to avoid power outages and manually correct time drift occasionally…
As already said, there are other more urgent problems then timestamps if there is no network connection…
thank you for your replying
here is the easiest way to do this
save the time and date in text file time.txt and put it in the root directory interval 10m for example:
/system scheduler add name=save-time start-time=startup interval=10m on-event=“:delay 60s\r
\n:local currentTime [/system clock get time]\r
\n:local currentDate [/system clock get date]\r
\n/file print file=time.txt where name="time.txt"\r
\n/file set time.txt contents="$currentDate $currentTime"”
and restore it by using this:
/system scheduler add name=restore-time start-time=startup on-event=“:local savedTime [/file get time.txt contents]\r
\n:local savedDate [:pick $savedTime 0 10]\r
\n:local savedClock [:pick $savedTime 11 19]\r
\n/system clock set date=$savedDate time=$savedClock”
note:
if you use another time format like v6 (jul/02/2024) you need to change the pick value to 0 11 and 12 20