This is an rsync shell script that I wrote to keep a directory of music files in sync between my Linux server/workstation and my android device. The script simply downloads any files which it doesn't already have, and deletes files which have been deleted from the server.
The server here must be running an ssh/scp daemon, and I use ssh key authentication. I don't document how to set up ssh key authentication here, as that is well documented elsewhere.
Most of this script is actually the process of making sure that the sync worked correctly, and it will retry until it succeeds, up to a maximum of six tries. This is because my wifi often goes out during long transfers, and the rsync process fails. The script will run in a loop for as many times as it is configured, until it succeeds or gives up.
Finally, I run this script with a push of a button, by setting up an SMWidget to run the script. This makes it very convenient to run.
#!/system/bin/sh
#
# rsync audio files from server to android
#
RSYNCATTEMPTS=6
echo ""
echo "Start date is: $(date)"
echo ""
echo "Running as $(id)"
echo ""
until [ "$RSYNCATTEMPTS" -eq 0 ] ; do
# Note that dropbear ssh requires fully qualified path to key file
rsync -rltD -v --delete --progress -e "ssh -i /data/local/rsync_host_key" \
user@myhost.net:~/audio/android-sync/my-music /mnt/sdcard/media/audio/
RSYNCEXIT=$?
if [ "$RSYNCEXIT" == 0 ] ; then
echo ""
echo "RESULT: Success."
echo ""
break
else
RSYNCATTEMPTS=$(( "$RSYNCATTEMPTS" - 1 ))
if [ "$RSYNCATTEMPTS" -gt 0 ] ; then
echo ""
echo "Attempt failed. $RSYNCATTEMPTS attempts remaining. Will retry..."
echo ""
sleep 5
continue
else
echo ""
echo "RESULT: Failure."
echo ""
echo "All attempts failed. Giving up."
echo ""
fi
fi
done
echo ""
echo "Final rsync exit code was: $RSYNCEXIT"
echo ""
echo ""
echo "End date is: $(date)"
echo ""
exit 0
Note that I used "rsync -rltD" instead of "rsync -a", because the -p argument was causing trouble for me. This is because my sdcard is FAT filesystem, which does not support posix file permissions, and thus rsync was giving me trouble about it.
I am happy to reply to intelligent questions or feedback.