HOWTO: Back up your Android device with native rsync

Sunday, September 25th, 2016 at 7:20 pm | 8,725 views | trackback url

Android
Recently, one of my Android devices stopped reading the memory card. Opening the device, the microSD card was so hot I couldn’t hold it in my hand. The battery on that corner of the device had started to swell slightly. I’ve used this device every day for 3+ years without any issues. Until this week.

I also use TitaniumBackup to back up my Android to this external memory card, but since the device can’t read the card, I can’t back it up to the card.

The card is fine, and works in my other devices, as well as being seen from the desktop. Other, blank microSD card can’t be read in the device and similarly overheat within seconds. It’s bad.

Enter rsync, the Swiss-Army Knife of power, to back up my Android device!

Here’s how:

  1. Download the Android SDK and unpack or install it on your machine.
  2. Plug in your Android via USB cable to your machine (Mac, Windows or Linux).
  3. Go into Settings → About, and tap on the Build Number section until you’ve enabled “Developer Mode”.
  4. Exit out of the “About” section, and now you’ll see a new menu option: “Developer Options”.
  5. Select “Developer Options” and go to “USB Debugging”, and enable it. This will enable you to manage your Android device over USB.
  6. cd into the ‘platform-tools’ directory and run “adb”, so we can find your device:
    $ ./adb devices
    List of devices attached
    d682520f	device
  7. Let’s use “adb” again and open up a shell on your Android device as root. We’re going to run the rsync process as root in a moment. We could run it as a normal user, but would be restricted in the files we are permitted to read from the device. To make things easier, we’ll just do this all as root:
    $ ./adb -s d682520f shell
    shell@hlteatt:/ $ su -
    root@hlteatt:/ # id
    uid=0(root) gid=0(root) context=u:r:init:s0
    root@hlteatt:/ #
  8. Now we need to create a configuration file for the rsyncd server process to use in a moment. Using the ‘vi’ editor, create the following file, call it “rsyncd.conf” and put it wherever you think it appropriate. In my example, I put mine in /data/local/tmp/rsyncd.conf. The contents looks like this:
    address = 127.0.0.1
    port = 1873
    
    [root]
    path = /
    use chroot = false
    read only = false
    uid = root
    gid = root
  9. We need to download the ARM version of the rsync binary and push it to the device:
    wget -O rsync.bin http://github.com/pts/rsyncbin/raw/master/rsync.rsync4android
    $ file rsync.bin
    rsync.bin: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, stripped
    
  10. Let’s push that binary to the Android, using “adb” again:
    $ ./adb -s d682520f push rsync.bin /data/local/tmp/rsync
    2357 KB/s (793148 bytes in 0.328s)
  11. To execute rsync as root, we have to adjust the permissions of that binary:
    $ ./adb -s d682520f shell su - -c "chmod 755 /data/local/tmp/rsync"
  12. Run the rsync server process, listening on the standard port, by chaining the command through “adb”. You can also do this by logging into the Android via “adb shell” and running the same command directly. I’ve added the end-of-line continuation markers here so it doesn’t wrap wrong in this post:
    $ ./adb -s d682520f shell su - -c 
    "/data/local/tmp/rsync                 \
    --daemon --no-detach                   \
    --config=/data/local/tmp/rsyncd.conf   \
    --log-file=/proc/self/fd/2"
    
    2016/09/25 23:05:17 [21300] rsyncd version 3.1.1 starting, listening on port 1873

    All in one line here:

    $ ./adb -s d682520f shell su - -c "/data/local/tmp/rsync --daemon --no-detach --config=/data/local/tmp/rsyncd.conf --log-file=/proc/self/fd/2"
  13. In another terminal/shell on your host machine, forward the rsyncd port from inside your Android device to a listening port on your host machine. This command should produce no output:
    ./adb forward tcp:6010 tcp:1873
  14. Now we can run rsync on the host to pull the data from your Android over to your local disk. Create a directory to hold your backup and cd into it, then run the following:
    sudo rsync -avP --delete --inplace \
    --stats --exclude=/proc --exclude=/sys  \
    rsync://localhost:6010/root .
  15. It should then show your device starting to back up to your local disk. When it completes, you’ll see a summary, similar to the following:
    Number of files: 85920
    Number of files transferred: 3040
    Total file size: 39514100245 bytes
    Total transferred file size: 770291255 bytes
    Literal data: 215397682 bytes
    Matched data: 554901587 bytes
    File list size: 2311538
    File list generation time: 10.609 seconds
    File list transfer time: 0.000 seconds
    Total bytes sent: 1529010
    Total bytes received: 218618474
    
    sent 1529010 bytes  received 218618474 bytes  1842238.36 bytes/sec
    total size is 39514100245  speedup is 179.49

Why would I want to do this instead of using “adb backup”? That style backup packs files into a single, binary container. Using rsync, you have direct access to individual files on your device, without having to restore them first.

That’s it! You now have a full (and I mean FULL) backup of every bit of data on your device. You can now push that to an SD card, thumb drive, storage media or another Android device.

Have fun!

Last Modified: Wednesday, September 12th, 2018 @ 15:00

Leave a Reply

You must be logged in to post a comment.

Bad Behavior has blocked 3377 access attempts in the last 7 days.