#!/bin/sh
#
# Use opensync to backup or restore mobile phone data
#
# How does this work:
#
# Backup is easy:
#
# We syncronise the phone with an empty directory, in order to
# get all the entries in a text file form.
#
# Restore is harder because we need to delete the existing data:
# 1) Sync the phone with an empty directory.
# 2) Delete all the files
# 3) Copy in the files to be restored
# 4) Re-synchronise
#
# --clear is like a restore but with no data. I.e. all entries are deleted.
#
# Directories
backup_dir="$HOME/phone/backup"
work_dir="$HOME/phone/.work"
tracedir="/var/tmp/log"
# get the name of this script
this=$0
backup=0
restore=0
clear=0
trace=0
initial=0
phase1=1
phase2=1
# Get default address from gconf (first listed bluetooth device)
address=$(gconftool --get /system/bluetooth/devices | cut -d , -f 1 | tr -d "[ ]")
# Handle callbacks
# (see EDITOR= line below)
while [ $# -gt 0 ]
do
# Callback
if [ "$1" = "--config-file" -a $# -eq 2 ]
then
echo "$work_dirFALSE" >$2
exit 0
elif [ "$1" = "--config-phone" -a $# -eq 2 ]
then
echo "" >$2
echo "2" >>$2
echo "$address" >>$2
echo "11" >>$2
# echo "PC Suite" >>$2
echo "1" >>$2
echo "1" >>$2
echo "1" >>$2
echo "contacts" >>$2
echo "calendar" >>$2
echo "notes" >>$2
echo "" >>$2
exit 0
elif [ "$1" = "--restore" ]
then
restore=1
shift
elif [ "$1" = "--clear" ]
then
clear=1
shift
elif [ "$1" = "--phase-1" ]
then
phase1=1
phase2=0
shift
elif [ "$1" = "--phase-2" ]
then
phase1=0
phase2=1
shift
elif [ "$1" = "--address" -a $# -ge 2 ]
then
address=$2
shift 2
elif [ "$1" = "--backup-directory" -a $# -ge 2 ]
then
backup_dir=$2
shift 2
elif [ "$1" = "--work-directory" -a $# -ge 2 ]
then
work_dir=$2
shift 2
elif [ "$1" = "--trace" ]
then
trace=1
shift
elif [ "$1" = "--help" ]
then
echo "$0 [--help] [--restore] [--clear] [--trace] [--backup-directory ] [--address ] [--work-directory ] [--phase-1] [--phase-2]"
echo " --restore performs a restore (from the backup directory)"
echo " --clear deletes all data from the phone"
echo "If neither of --restore or --clear is specified, a backup is taken"
echo " --trace creates Opensync logs in $tracedir"
echo " --backup-directory backup directory defaults to $backup_dir"
echo " --address address defaults to $address"
echo " --work-directory working directory defaults to $work_dir"
echo " --phase-1 only performs first phase (for testing)"
echo " --phase-2 only performs second phase (for testing)"
exit 2
else
echo "Invalid callback: ($#) $1"
echo "For help use $0 --help"
exit 1
fi
done
if [ $backup -eq 0 -a $restore -eq 0 -a $clear -eq 0 ]
then
# --backup is the default
backup=1
fi
if [ $phase1 -ne 0 ]
then
rm -rf $work_dir
mkdir -p $work_dir
# Create the synchronisation group
msynctool --delgroup backupphone
msynctool --addgroup backupphone
msynctool --addmember backupphone file-sync
msynctool --addmember backupphone syncml-obex-client
# In order to set the configuration we need to call back into this script
# to do the work
EDITOR="$this --config-file" msynctool --configure backupphone 1
sudo hciconfig hci0 up
EDITOR="$this --config-phone" msynctool --configure backupphone 2
if [ $trace -ne 0 ]
then
rm -rf $tracedir
mkdir $tracedir
export OSYNC_TRACE=$tracedir
export SYNCML_TRACE=$tracedir
export SYNCML_LOG=$tracedir
fi
# Step 1 -- Sync once to fill the directory
sudo hciconfig hci0 up
msynctool --sync backupphone
if [ $? -ne 0 ]
then
echo "Initial sync failed."
exit
fi
fi
if [ $phase2 -ne 0 ]
then
# For a backup, save the files
if [ $backup -ne 0 ]
then
# Empty the current backup directory
rm -rf $backup_dir
mkdir -p $backup_dir
cp -fp $work_dir/* $backup_dir/
fi
# Restore
if [ $restore -ne 0 -o $clear -ne 0 ]
then
# Step 2 -- Delete all the files
rm $work_dir/*
# Step 3 -- Copy the files from backup
if [ $restore -ne 0 ]
then
cp -fp $backup_dir/* $work_dir/
fi
# Step 4 -- Sync again to replace entries
# Wait a few seconds for the bluetooth interface to go down before
# we try to bring it up again
sleep 3
read -p "Press OK on the phone and hit enter" ignored
sudo hciconfig hci0 up
msynctool --sync backupphone --conflict 1
fi
fi