DD-WRT. How to recover an actual date when the router will suffer a power outage

Sometimes when there will suffer a power outage in some versions of routers, there lose all data stored in the RAM, like dates stored, it kept back to those wonderful 1970. This causes issues in some services, the most affected is the OpenVPN service, as it causes that the services doesn’t start disabling external access to the local network.
In this small tutorial, I will indicate how to solve this small contingency of the easiest way, without using the NTP service, since in some cases can not recover a date until the restart an Internet connection, such ADSL or 3G.
To begin we will open a browser to access The Web Administration of our router with DD-WRT.
Now clicks to the option ‘Administration’ → ‘Commands’. Once located in this part of backbend. Clicking on the button ‘Edit’ from ‘Startup’ Section.
And add the next commands in ‘Startup’ Shell Script. In the end of entry we deailled how works this script.
sleep 15
v_value=`nvram get date_file`
v_date=`date +%m%d%H%M%Y`
if [ $v_value > $v_date ]
then
date $v_value
fi
echo "v_value=\$(nvram get date_file)
v_date=\$(date +%m%d%H%M%Y)
if [ \$v_value \< \$v_date ]
then
nvram set date_file=\$v_date
fi" > /tmp/.sh.cron.date_set
echo '*/15 * * * * root /bin/sh /tmp/.sh.cron.date_set' >> /tmp/crontab
It is the time to save the script, we will go to bottom page and clicks on “Save Startup” button.
Enable a Cron Services
If the Cron Service is disabled, we recommend enabling it. as the way keep in memory the system date through Cron Script that it is generated in the ‘Startup’.
To enable this option we will go to the backend web and direct ‘Administration’ → ‘Management’.
In the ‘cron’ section will enable clicking in ‘Enable’ Option.
Once selected we will click in the the buttons ‘Save’ and ‘Apply Settings’, in htat order, located in the bottom of page. And the cron service will be enabled on our router.
Save date a first time
But we have one last step, there set a default variables for the first time. We will open a terminal application as ‘putty’ for windows or a terminal application in GNU/Linux or OSX and accesed via SSH service to router. We will use the following the next command.
Connect and enter a right password.
Once inside we will set a system date with a date command following a next structure.
Every valour main:
- MM: Month. From 01 to 12.
- DD: Day. From 01 to 31.
- HH: Hours. From 00 to 23.
- MM. Minutes. From 00 to 59.
- YYYY. Year. For example 2010.
For example if we use the following date, at 21:14 of April 11, 2010, the result will be.
Press ‘Enter’ Key and we have a actual date in the router, now we need only put in the memory of it, we use the following command.
And we have a date saved, to check it we will execute the following command.
If this returns the date that we introduced to execute the command ‘date’, everything went successfully. Now we will test all functionality, shutting down and restarting the router by removing from the power supply. If everything works right then that means that the efficiency of the script is correct.
¿What exactly does this script?
Now its discovered that does exactly the script detailed above. This script is divided into three blocks, which we shall call:
- Update a system date.
- Creation script that sets date in memory.
- Update a Cron.
Update a System Date
v_value=`nvram get date_file`
v_date=`date +%m%d%H%M%Y`
if [ $v_value \> $v_date ]
then
date $v_value
fi
This part of code gets a stored date from memory of router. If it is greather than system date, the script will update it.
Creation script that sets date in memory.
v_date=\$(date +%m%d%H%M%Y)
if [ \$v_value \< \$v_date ]
then
nvram set date_file=\$v_date
fi" > /tmp/.sh.cron.date_set
This part generate the script that will be user later for the cron service, makes a ‘echo’ into temporary file.
Update a Cron.
In the end do a ‘echo’ in the crontab file, adding on line to execute the previously generated script every 15 minutes.
Things to improve
Last note, the cron script store the system date every 15 minutes, may do slight delays, but it can work right. To improve it, there would be desirable to introduce as a recovery process by NTP service automatically, since by now we have to do manually.
Share Our Article
Comments
Trackbacks
There are no trackbacks on this entry.




Helpful blog, bookmarked the website with hopes to read more!
Really nice information, thanks!
Couldnt agree more with that, very attractive article
Thank you, I have recently been searching for information about this topic for ages and yours is the best I have discovered so far.
Really nice post,thank you