Android auto-phone backup part 2

So, at the end of my previous post, I had rsync compiled and runnable on the phones via ssh.

Some playing about and chatting later, my friend Colin suggested adding the backup into one of the dhcpcd hooks. The idea being that whenever the phone connects to my flat wifi and successfully gets an IP address, it will automatically kick off a backup at the same time.

This is easily achieved by adding the following script into /etc/dhcpcd/dhcpcd-hooks/96-backup on the phone:

case "${reason}" in
BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT)
   if [ x"${new_dhcp_server_identifier}" = x"DHCPSERVERIP" ]; then
      wget -q -O - http://WEBSERVERIP/SOMETHING/backup.cgi?machine=hero-adq
   fi
   ;;
esac

So, when it connects to an access point whose IP is DHCPSERVERIP, it’ll automatically make an HTTP request to a CGI script on WEBSERVERIP. This script uses sudo to kick off the backup. I decided the server should control the backup process rather than the phone as it makes it much easier to maintain. Also, the backup has to run as root on the server so it can maintain the userid/groupid and access rights on the files as this is important for android to function correctly if I were to restore something.

The script, ‘rsynchero’, which runs on the server and actually does the backups is as follows:

#!/bin/bash

SRCDIR=$1
DESTDIR=$2

/usr/bin/rsync -a \
        --force \
        --ignore-errors \
        --delete-excluded \
        --delete \
        --exclude '**/dalvik-cache/**' \
        --exclude '**/cache/**' \
        --exclude '/proc/**' \
        --exclude '/sys/**' \
        --exclude '/cache/**' \
        --exclude '/dev/**' \
        --exclude '/sdcard/newsrob/**' \
        --exclude '/sdcard/mp3/**' \
        --exclude '/sdcard/acv/**' \
        -e '/usr/bin/ssh -p 2222' \
        $SRCDIR $DESTDIR

I’ve added the server’s ssh public key to /data/dropbear/authorized_keys on the phone so it can login securely without needing to supply a password.

So, this is it done. The backup has already proved itself useful when a file was accidentally removed from nicola’s phone yesterday (something went wrong with the ‘Secrets’ app).

The only other thing that would be nice is a multi-day rotating backup in case a file is removed and only noticed some time later.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: