Skip to content

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

19 October 2010 5 comments Articles, DD-WRT, GNU/Linux, OpenWRT, SysOp

WRT54GL

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.

Main page

Now clicks to the option ‘Administration’ → ‘Commands’. Once located in this part of backbend. Clicking on the button ‘Edit’ from ‘Startup’ Section.

Script Page

And add the next commands in ‘Startup’ Shell Script. In the end of entry we deailled how works this script.

# Update Date
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’.

Cron Page

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.

ssh root@[dirección del router]

Connect and enter a right password.

Once inside we will set a system date with a date command following a next structure.

date MMDDHHMMYYYY

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.

date 041121142010

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.

sh /tmp/.sh.cron.date_set

And we have a date saved, to check it we will execute the following command.

nvram get date_file

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:

  1. Update a system date.
  2. Creation script that sets date in memory.
  3. Update a Cron.

Update a System Date

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

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.

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

This part generate the script that will be user later for the cron service, makes a ‘echo’ into temporary file.

Update a Cron.

echo '*/15 * * * * root /bin/sh /tmp/.sh.cron.date_set'  >> /tmp/crontab

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


Related Posts

There are no related posts on this entry.

Comments

  1. roclafamilia October 21, 2010

    Helpful blog, bookmarked the website with hopes to read more!

  2. GreenGoblin October 21, 2010

    Really nice information, thanks!

  3. auto repair November 10, 2010

    Couldnt agree more with that, very attractive article

  4. student grants November 14, 2010

    Thank you, I have recently been searching for information about this topic for ages and yours is the best I have discovered so far.

  5. Ron Tedwater November 18, 2010

    Really nice post,thank you

Trackbacks

There are no trackbacks on this entry.

Add a Comment

Required

Required

Optional