Android backup using rsync

Today I finally implemented a backup solution for my Android phone after regretting not having done any backups multiple times now.

I'm running Cyanogenmod 7 (Android 2.3.7) with root access, but I'm pretty sure the method should work without root privileges on a conventional Android distribution of pretty much any version. Although, there may be tools missing. Please report if you find out that's the case. (Tools used are: dropbearkey, ssh, rsync, vi, chmod)


For a more convenient interface (at least to have a keyboard) I used the Android Debug Bridge (adb). You do not have to use adb, but could also do the setup using a terminal on your device.

There's a Debian package called android-tools-adb currently in Debian sid as well as Ubuntu quantal and raring. I don't know if there are any packages for other operating systems. If there is non four you, you'll have to download the complete Android SDK for your platform and find the adb tool inside.

Activate USB debugging on your device. You'll find the option in Settings > Applications > Development or Settings > Developer options in Android 4. When plugging it into your computer, the device should show a note about USB debugging.

You can get a shell on your computer after starting the debugging server as a privileged user. The following is an example for a UNIX system with sudo.
sudo adb kill-server   # make sure the server's not already running
                       # (as an unprivileged user)
sudo adb start-server
adb shell
To get a more convenient shell (with e. g. tab-completion), type bash.


I quickly found out, my version of Android (and probably every version) does not support NFS, so I decided to go for rsync over SSH. Since my distribution has a dropbear SSH client out of the box. To improve it, I wanted to get rid of a) the warning about the server's hash not being known and trusted and b) the password prompt. In contrast to standard SSH usage, the warning about the server hash, will not only appear once, since the ssh client is not able to store it on the read-only system memory (at least if you're root and your home directory is /, with a user's home directory set to /mnt/sdcard it's probably working). Fortunately ssh can simply ignore the issue if you provide the switch -y.

To get rid of the password prompt, I generated an SSH key. My Android already had the command dropbearkey for that, so I issued
dropbearkey -t rsa -f /sdcard/id_rsa
to create an RSA key in /sdcard/id_rsa. You can choose any persistent location insted of /sdcard. The key has a different format from what e. g. OpenSSH uses, so you'll have to convert it to add the public key to the server. This can be done using
dropbearkey -y -f /sdcard/id_rsa
On the system you want to connect to, open up the authorized_keys file in the .ssh directory of the user that's going to log in and append the line starting with ssh-key from the dropbearkey output. Neither the headline, nor the fingerprint belong to the public key!

You should now be able to connect now without any prompt by issuing
ssh -y -i /sdcard/id_rsa <user>@<host>


After setting up ssh, you can use rsync on the Android device with the option -e 'ssh -i /sdcard/id_rsa'. To create a little backup script, i issued
vi /sdcard/backup
and put the following line (#!/bin/sh would provoke errors, since Android does not have this path, so just leave it out) into it (backing up /data will need root!):
rsync -ave 'ssh -y -i id_rsa' /data /sdcard schramm@
To make it executable use
chmod +x /sdcard/backup
With a quick Google search I found SManager, which is a file manager able to run scripts and show you the output, so that it's no necessary to type anything into the terminal or connect to another device using adb to trigger the backup. Just open SManager, click on the script and watch your delta being transferred to the backup device.

I hope I'll actually do this before the next mess...

No comments:

Post a Comment