#! /bin/sh
# sync.sh
# Script to synchronise Kindle's documents folder with a file server.
# Relies on jailbreak, usbnetworking, and on having set up passwordless ssh access to the server.
# rsync's --update: "skip files that are newer on the receiver", relies on both devices knowing the time.
# Directory structure on the server is as follows:
# $svrdir/documents $svrdir/newspapers $svrdir/other
# Change the following to match your setup. Note that there is no whitespace, nor trailing / on the directories
#server="jonj.dyndns.org" # IP of server, mine changes so I'm using dynamic dns
svruser="root" # Username on the server
port="22" # ssh port listening on server
svrdir="/DataVolume/shares/Kindle" # Directory to pull data from on the server
idfile="/usr/local/bin/id_rsa" # ID file used by dropbear when authenticating
# Defaults are probably alright here
locdir="/mnt/us" # Directory on the kindle to sync to
updatetimefirst="true" # Set to false to disable updating the time before sync
sshclient="/usr/bin/dbclient -i $idfile -p $port" # Include -y to disable host checking
ropt="-acqz" # archieve, checksum, quiet, compress
lockfile="/mnt/us/sync.lock"
# Don't change this
direction="$1"
mkdir -p $locdir/documents
mkdir -p $locdir/documents/Newspapers
# Assuming the server normally allows ping; if you can't ping it, you probably can't ssh to it either. So turn off wifi to disable this script.
if ping -c 1 $server > /dev/null
then
if [ ! -e $lockfile ]; then
touch $lockfile
# Verify that known_hosts is still a non-empty file, create it if it isn't. Or use -y on sshclient
if [ ! -s /var/tmp/root/.ssh/known_hosts ] ; then
mntroot rw
echo "-key-goes-here-" > /var/tmp/root/.ssh/known_hosts
chmod 644 /var/tmp/root/.ssh/known_hosts
mntroot ro
fi
if [ "${updatetimefirst}" == "true" ] ;
then
/usr/bin/ntpdate -s europe.pool.ntp.org &> /dev/null
fi
if [ "${direction}" == "pull" ] ; then
# Pull documents from server to kindle
# --no-p stops rsync trying to set permissions on a vfat volume
/usr/bin/rsync "$ropt" --no-p --update -e "$sshclient" $svruser@$server:$svrdir/documents/ $locdir/documents/ &> /dev/null
# Pull newspapers from server to kindle
/usr/bin/rsync "$ropt" --no-p --delete -e "$sshclient" $svruser@$server:$svrdir/newspapers/ $locdir/documents/Newspapers/ &> /dev/null
fi
if [ "${direction}" == "push" ] ; then
# Push documents to server from kindle
/usr/bin/rsync "$ropt" --update --exclude Newspapers -e "$sshclient" $locdir/documents/ $svruser@$server:$svrdir/documents/ &> /dev/null
# Backup everything that isn't documents to the server
/usr/bin/rsync "$ropt" --update --exclude documents --exclude system/collections.json -e "$sshclient" $locdir/ $svruser@$server:$svrdir/other/ &> /dev/null
fi
# /etc/init.d/framework restart
rm $lockfile
fi
# Update the Kindle's document listing
sleep 10
dbus-send --system /default com.lab126.powerd.resuming int32:1
fi
exit