Timesave with date v.8.23

Posted by mrd0ll4r 
This forum is currently read only. You can not log in or make any changes. This is a temporary situation.
Now, this forum is in read-only mode. You find details Details hereContinue on /r/PirateBox
Timesave with date v.8.23
October 12, 2014 03:01PM
Heyho!

I fiddled around with my RPi some more ad tested the forum, which reported every post as a flood. With a lot of digging I think the problem is the clock, which is reset to Wed Dec 31 17:00:00 MST 1969 at every reboot - at least that's the issue I want to have solved ;-)

We have the timesave feature, which I activated after setting the correct time. The timesave_file currently has "201410121620" in it.
crontab -l says:
blabla...
*/5 * * * * /opt/piratebox/bin/timesave.sh /opt/piratebox/conf/piratebox.conf save


Which is working. Time is saved every 5 minutes. Now the problem is: it's not restored. I already applied the latest commit to my timesave.sh (missing 'then'), so the script runs now, but date reports:
date: invalid date '201410121620'


Now I messed around with the date command, and the problem is:
date +%Y%m%d%H%M -s "201410121620" gives me "date: invalid date '2014.....'"
date +%C%g%m%d%H%M -s "201410121620" gives me "date: invalid date '2014.....'" (version used in the timesave.sh script)


Now I'm pretty much out of ideas. What worked for me was setting date and time in two commands:
date +%Y%m%d -s 20141012
date +%T -s 16:58:00

What I could do now is:
-seperate date and time in two files/seperate by line or something else
-possible introduce another variable in piratebox.conf
-do a lot of stuff with the timesave.sh script to work with the two files and set the time correctly

Another big question I'm having is: When is the timesave.sh script called to restore the clock?

mrd0ll4r

EDIT: I now understand why this issue comes up now:
In the months before today I had a spare ethernet cable and the RPi also had internet access. I guess it synced the time via NTP... but I don't have that cable anymore, and I just bought a USB power pack to go mobile with the whole thing.. so I still need the timesave :-/



Edited 1 time(s). Last edit at 10/12/2014 03:06PM by mrd0ll4r.
Re: Timesave with date v.8.23
October 12, 2014 06:06PM
Hi,
which image version do you run?
I added the following file to the new RPi-image-version, that is started on bootup:

/etc/systemd/system/timesave.service
[Unit]
Description=Restore fake RTC-time
[Service]
ExecStart=/bin/bash /opt/piratebox/bin/timesave.sh /opt/piratebox/conf/piratebox.conf recover
[Install]
WantedBy=multi-user.target

And I think I enabled it per default:
systemctl enable timesave.service

That was missing on the old image.

On testing with librarybox I encountered the "recover" problem two or three times, and AFTER I fixed it, it worked for me with the default - whatever it was eye rolling smiley

So, maybe you can simply run the following command to change the format for a working recovery:
sed 's|date `c|date -s `c|' -i /opt/piratebox/bin/timesave.sh
sed 's|date +%C%g%m%d%H%M|date|' -i /opt/piratebox/bin/timesave.sh
(I added those fixes to the package-prepare of a REAL archlinux package file, so the file is fixed for the archlinux version)

Can you check if that is fixing the issue for you?
Matthias
Re: Timesave with date v.8.23
October 12, 2014 08:26PM
Hey Matthias,

I run the very first ArchLinux Image we had...
Thanks for the timesave.service, I hope it does its job ;-)

I got the timesave script working for me now, changing the format to the "default" kina worked, but introduced a new problem: the timesave script compares the two dates and checks if we change the time backwards (which is clever) -> it can't do that correctly with a date that looks like "Sun Oct 12 22:20:34 MDT 2014" because... how the hell should it know how to do that.

In the end, I removed all these checks. Manually calling the timesave script works (save and restore), so I guess it works automatically as well. Here is the current version running:

#!/bin/sh

#  This script enables a sort of timerescue System
#  for Systems without a Realtime Clock
#  like TP-Link MR3020 , RaspberryPI
#
#  It does not reflect the real time, but 
#  gives a sort of stability to complete standalone 
#  systems.
#
#  Licenced under GPL-2  @ 2012
#    Matthias Strubel    matthias.strubel@aod-rgp.de

##function for similar saving & getting time
get_datetime() {
	date  
}


# Load configfile

if [ -z  $1 ] || [ -z $2 ] ; then
  echo "Set up a crontab entry for regulary saving the time"
  echo "Usage $0 <path to piratebox.conf> <step>"
  echo "    Valid steps are:"
  echo "       install    - installs the needed parts into crontab"
  echo "       save       - saves time into file"
  echo "       recover    - recovers the time from a file"

  exit 1
fi

. $1


if [ "$2" = "install" ] ; then
    crontab -l   >  $PIRATEBOX_FOLDER/tmp/crontab 2> /dev/null
    echo "#--- Crontab for PirateBox-Timesave" >>  $PIRATEBOX_FOLDER/tmp/crontab
    echo " */5 * * * *   $PIRATEBOX_FOLDER/bin/timesave.sh $PIRATEBOX_FOLDER/conf/piratebox.conf save "  >> $PIRATEBOX_FOLDER/tmp/crontab
    crontab $PIRATEBOX_FOLDER/tmp/crontab

    echo  "initialize timesave file"
    touch $TIMESAVE
    chmod a+rw $TIMESAVE
    get_datetime  > $TIMESAVE


    echo "Remember MAY have to cron active..."
    echo "  on OpenWrt run:  /etc/init.d/piratebox enable"
 
    exit 0
fi

if [ "$2" = "save" ] ; then
#    if [ -e $TIMESAVE ] ; then
#	if [ `get_datetime` -lt  `cat $TIMESAVE` ] ; then
#		 logger -s "$0 : sorry, current date-time is lower then saved one, don't save it this time"
#		 exit 1
#	fi
#    fi

    #Save Datetime in a recoverable format...
    get_datetime  > $TIMESAVE
    exit 0
fi

if [ "$2" = "recover" ] ; then
#    if [ `get_datetime` -lt  `cat $TIMESAVE` ] ;
#then
	    date -s "`cat $TIMESAVE `"
	    [ "$?" != "0" ] &&  echo "error in recovering time" && exit 255
	    echo "Time recovered"
	    exit 0
#    else
#	   echo "Sorry, changing timebackward via timesave is not possible"
#	   exit 1
#    fi
fi

Thanks for your help! I think it's working now tongue sticking out smiley
Re: Timesave with date v.8.23
October 12, 2014 08:28PM
Hey,
ok I'm glad it is working and thank you for sharing the code!

Do you have spare SD-Card where you can try to verify the timesave stuff on a new image?

best regards Matthias
Re: Timesave with date v.8.23
October 12, 2014 08:31PM
Hey Matthias,

just verified it: Working :-)

Sadly no, sorry... but I checked my version: piratebox-ws_1.0.0

I'll keep my eyes and ears open for a SD card though ;-)

Btw, is there a straightforward way on how to update the piratebox? I don't mean the ArchLinuxARM packet updates, that's easy - how to update the piratebox scripts?

mrd0ll4r
Re: Timesave with date v.8.23
October 12, 2014 08:36PM
exchange the scripts in /opt/piratebox with the one of downloads.piratebox.de (piratebox_ws-current.tar.gz)

BUT, I'm working on a ArchLinux package for LibraryBox right now, that can be later used for PirateBox as well (https://github.com/LibraryBox-Dev/package-arch-librarybox)

Matthias