How to backup and sync your rooted Android phone automatically

1 03 2012

I have for a while now been working on a custom automatic sync solution for my HTC Hero running Froyo (Froydvillain ROM). As I am a Linux junkie and love scripts and hacks I wanted to do it all via cunning hacks and I’ve finally got it nailed.

This solution uses Scripting Layer 4 Android (SL4A) and Tasker alongside a custom ROM with rsync (any ROM should do so long as it has rsync). For those that don’t know, rsync is an awesome application that allows for remote backup and sync across machines. It turns out you don’t even need a ROM with rsync built in, as you can install an app that provides rsync, the app is called rsync backup for android and can be found here: https://market.android.com/details?id=eu.kowalczuk.rsync4android&hl=en

The only issue is you can’t call rsync from the command line using simply “rsync” since it isn’t in your systems path. However, if you use the following string instead,replacing calls to “rsync” with the following, the scripts still work: /data/data/eu.kowalczuk.rsync4android/files/rsync

SL4A is used to set out what to do via a script. You can write scripts in various languages in SL4A but I am using Bash as I am familiar with it. Rsync is used to actually handle the sync / backup and Tasker is used to launch the scripts when certain conditions are met.

I have created two scripts in SL4A, one backups my photos folder to my main photo folder on my server. The server runs the rsync daemon which rsync on the phone connects to. The other script does the reverse and copies a remote folder in my server that contains a bunch of music to my phone.

Tasker is set up with a profile that activates when my phone is plugged in and it’s between midnight and 7.00 am. This then connects to my WiFi network and then runs the two scripts via the SL4A plugin. Since I charge my phone each night this is effectively automatic.

The key here is getting permissions correct with rsync during the file transfer, as the memory card uses fat32 it hasn’t got any permissions. The rsync daemon doesn’t like this and errors out, hence the need for various settings. The second key here is exporting your password as an environmental variable. This is inherently insecure but since my server has multiple redundant backups and is only locally accessible I don’t care much. I could use trusted keys but I’m too lazy.

Here are the two scripts. First the music script that syncs from server to phone:

#rsync sync
export RSYNC_PASSWORD=password
DATE=$(date)
LOG=/mnt/sdcard/rsyncmusic.txt
echo rsync started $DATE > $LOG
TRY=1
rsync_com ()
{
DATE=$(date)
if [ $TRY = 15 ]; then
echo rsync failed, quitting on $DATE >> $LOG
exit
fi
sleep 10
echo rsync attempt $TRY started $DATE >> $LOG
rsync --progress -vHrltD --chmod=Du+rwx,go-rwx,Fu+rw,go-rw --no-perms --stats --password-file=/mnt/sdcard/scrt prupert@prupert::amusic /mnt/sdcard/amusic >> $LOG 2>&1
EXIT=$?
TRY=`expr $TRY + 1`
echo exit code is $EXIT >> $LOG
echo "********************" >> $LOG
}
rsync_com
while [ $EXIT != 0 ]; do
rsync_com
done
echo rsync finished $DATE >> $LOG
exit

The second script syncs the phones photos folder to my server:

#rsync sync photo
export RSYNC_PASSWORD=password
DATE=$(date)
LOG=/mnt/sdcard/rsyncphoto.txt
echo rsyncphoto started $DATE > $LOG
TRY=1
rsync_com ()
{
DATE=$(date)
if [ $TRY = 15 ]; then
echo rsync failed, quitting on $DATE >> $LOG
exit
fi
sleep 10
echo rsync attempt $TRY started $DATE >> $LOG
rsync -vHrltD --chmod=Du+rwx,go-rwx,Fu+rw,go-rw --no-perms --stats --password-file /mnt/sdcard/scrt /mnt/sdcard/DCIM prupert@prupert::apics >> $LOG 2>&1
EXIT=$?
TRY=`expr $TRY + 1`
echo exit code is $EXIT >> $LOG
echo "********************" >> $LOG
}
rsync_com
while [ $EXIT != 0 ]; do
rsync_com
done
echo rsync finished $DATE >> $LOG
exit

I have put some logging in to check progress and also some retry code that retries the sync if it timesout. It seems my HTC Hero’s WiFi connection claps out after a while so the script retries up to 15 times to run successfully based on the rsync exit code.

About these ads

Actions

Information

3 responses

20 04 2012
Debian Updates

Debian Updates…

How to backup and sync your rooted Android phone automatically « prupert…

27 04 2012
Rebooting Windows Servers

Rebooting Windows Servers…

How to backup and sync your rooted Android phone automatically « prupert…

27 04 2012
Cloud Management

Cloud Management…

How to backup and sync your rooted Android phone automatically « prupert…

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.