Friday, October 2, 2009

Synchronizing Cellular Phone Date and Time with Gammu

After becoming the proud owner of a Bluetooth to USB adaptor, I started looking around for a way to automate the backup process of our cellular phones.

I've installed Wammu and was pleasantly surprised by how easy it was to get it up and running. But as soon as I had it configured to talk with both our phones, I realized that what I really wanted was to use Gammu, its command line alter ego.

My plan is to run a backup script every night that will copy contents and settings from the phones to my laptop over Bluetooth. I'll post my backup script as soon as it's up and running for a few days.

In the meantime, let me present another script that I run as a scheduled task every night. It's job is to synchronize the date and time on both our phones to the laptop clock. This has several benefits:
  1. the phone clocks are more accurate (because the laptop clock is updated by NTP)
  2. the phone clocks switch automatically to and from Daylight savings time,
  3. the phone clocks become synchronized to each other
And here it is:

#! /bin/bash
section=0
grep port= ~/.gammurc | cut -d= -f2 |
while read phone
do
name="$(grep -m $(( $section + 1 )) name= ~/.gammurc | tail -1 | sed s/name=//)"
echo $name
gammu -s $section setdatetime
let section++
done

I run the script as a Cron job at 4:05 AM. The idea is to synchronize clocks after 2:00AM, which is when the switch to/from Daylight savings time occurs where I live:
  1. launch crontab:
    crontab -e
    this should launch a text editor (you can configure which one by modifying the EDITOR environment variable)
  2. add the following line
      5 4    *   *   *   /full/path/to/sync-phones.sh
  3. save the file and exit the editor

No comments:

Post a Comment