Hello everyone.
This is a small backup script I wrote for my Nexus One phone. I am using Cyanogenmod 7.2 at this time.
I use SSHDroidPro as an ssh/scp server and I regularly log into my phone over wireless to manage files and do other tasks. SManager makes it easy to run the scripts I create via the phone's GUI, browse and manage files, and schedule jobs to run (like cron would).
This script runs via the default shell and the syntax should be portable without too much trouble. It requires a modern version of rsync, busybox, netcfg, and dumpstate. It uses ssh key authentication for the remote access. I don't document how to create or configure ssh keys here, as that is well-known information you can get from any search engine.
I have configured this script to run via SManager's built-in scheduling tool. I have the script configured to run once a week, each night while I am sleeping. I assume that my device is being charged and has wifi access to my access point during that time.
My use of dumpstate, and how I check wifi access and AC/USB power may not work on other devices. I have added comments at these locations to point them out.
It is very likely that some paths will need to be modified to be appropriate for your device, but it should be pretty obvious. You will definitely want to test this before assuming it will work for you.
Rsync will keep a backup of all files in the destination "current" directory, and during each backup, will keep a copy of the changed files in an "RSYNC-BAK" directory. These RSYNC-BAK directories need to be cleaned up on the destination host, unless you want them to build up forever.
I currently don't have the rsync process set retry if the first backup attempt fails, but I am likely to add this feature later, as I've already added it to another one of my rsync scripts.
The script takes a single argument; the word "scheduled" or "manual".
"rsync-backup.sh scheduled" is for the automated backup and will check to make sure that my device is not running on battery power, and that it has 802.11/wifi access. This prevents the script from running in case I am not home. Otherwise, it might drain the battery, and it could run over my carrier network, which I don't want to happen.
"rsync-backup.sh manual" is for when I manually want to run the script. It simply skips the wifi and power check.
The first time you run the script, it may take a LONG time to finish, as your entire filesystem must be transferred. However, subsequent backups only uploads new files and will be much faster. It might be a good idea to copy your filesystem directly via USB to make the initial backup.
Finally, I only wrote this script in the last two weeks. It may have bugs that I have yet to discover.
I am happy to reply to intelligent comments and questions, but I won't address linux-noob-101 stuff that google can tell you.
—
#!/system/bin/sh
#
# Full system backup for android device
# NOTICE: Unless the remote host user is root, file permissions and ownership is not being replicated.
#
DSTDIR="~/bak/android-rsync"
BAKDST="user@myhost.net:$DSTDIR"
BAKSRC="/*"
RSYNCEXCLUDEFILE=/data/local/rsync_backup_excludes.txt
RSYNCSSHKEY=/data/local/rsync_host_key
LOGDIR=/mnt/sdcard/rsync_logs
f_power_require() {
# If running on battery power, abort run
# Hopefully when the battery is at 100%, it still says "Charging". I do not know.
# This may not work on all devices. Be sure to check that this /sys file exists!
if [ ! $(cat /sys/class/power_supply/battery/status) == "Charging" ] ; then
echo ""
echo "ERROR: Device is currently running on battery power."
echo "Quitting."
echo ""
exit 1
fi
}
f_wifi_require() {
# Test to verify that 802.11 wifi is up and running. If not, quit.
# This may not work on all devices, and is dependent upon netcfg
if ! (netcfg | egrep "^eth0 +UP " > /dev/null 2>&1) ; then
echo ""
echo "ERROR: 802.11 WiFi is required for the backup to proceed, but isn't UP."
echo "Quitting."
echo ""
exit 1
fi
}
f_rsync_run() {
BAKVER=$(date '+%Y%m%d%H%M%S')-$$
RSYNCLOG=$LOGDIR/rsync-backup-$BAKVER.log
echo "BAKVER is: $BAKVER"
echo ""
# Note that dropbear ssh seem to require a fully qualified path to key file
# --chmod needed because some dirs are u-rwx.
time rsync -a -v --progress --delete --delete-excluded -e "ssh -i $RSYNCSSHKEY" --chmod=Du+rwx,Fu+rw \
--exclude-from=$RSYNCEXCLUDEFILE \
--exclude="$RSYNCLOG" \
--backup --backup-dir=$DSTDIR/RSYNC-BAK-$BAKVER \
--log-file=$RSYNCLOG \
$BAKSRC $BAKDST/current/
RSYNCEXIT=$?
echo ""
echo "rsync exit code was: $RSYNCEXIT"
echo ""
}
f_startup() {
echo ""
echo "Start date is: $(date)"
echo "Running as: $(id)"
# If $LOGDIR does not already exist, create it
if [ ! -d "$LOGDIR" ] ; then
mkdir "$LOGDIR" || echo "ERROR: Unable to create log dir. I quit." ; exit 1
fi
# Do a dumpstate save. Dumpstate saves a lot of information about your device, including
# the currently installed packages, your hardware info, and other debugging info.
# You might need to periodically clean up these backups, lest they take up too much space
dumpstate -z -o /mnt/sdcard/dumpsaves/dumpstate-$(date '+%Y%m%d%H%M%S')-$$.txt 2> /dev/null
}
f_finishup() {
echo "End date is: $(date)"
echo "Done."
echo ""
}
#--
case $1 in
scheduled)
# For regular over-night scheduled invocation
f_power_require
f_wifi_require
f_startup
f_rsync_run
f_finishup
;;
manual)
# For manual do-it-now invocation, we don't do sanity checking
f_startup
f_rsync_run
f_finishup
;;
*)
# Default error
echo ""
echo "ERROR: Unknown argument."
echo ""
echo "Valid arguments: scheduled, manual"
echo ""
exit 1
;;
esac
exit 0