Adventures in Android backup
I've always been annoyed by the lack of a true simple backup option for Android. A recent device failure reminded me of this situation. The purpose of this post is to share my experience, and a solution that seems to work for me.
Google has a built-in backup capability in Android that backs up your contacts (assuming that you keep your contacts with Google), and it'll keep track of what apps you have installed, and it may even back up app settings if the app developer has chosen to opt into that feature. That's a few "if's" already here. Things that will not
get backed up for sure are your photos, movies, and anything on your SD card. Also, various customizations, such as home screen layout and even other Android settings don't seem to be restored. Various carriers seem to have their own cloud backup options, but this seems vaguely redundant with some aspects of the Android-level backup, it's not immediately clear if it backs up *everything*, and it may cost extra money every month to have stuff backed up online. No thanks.
So how do you back up your own Android phone? The requirements before proceeding:
- Root Access
- A custom recovery ROM, such as CWM
Does anybody else find this somewhat ridiculous?
My first venture into backing up into the phone was to go the CIFS route
so that stuff could be easily moved off of the phone. (The LG G2 doesn't have an external card). One of the problems with this was that if I didn't unmount the share before leaving the wifi network, some things could behave differently. Back to the drawing board...
First, in order to get as close to a "bare metal" backup, you'll need to make a nandroid backup. And in order to do this, you'll need a recovery rom. There are supposedly apps that do an online nandroid backup, but I've heard that this technique can be problematic at times. The two popular recovery roms are CWM and TWRP. I attempted to load these roms manually, but I'd get an error: "Loki aboot version does not match device". I didn't feel like digging into this much. Some other options, such as ROM Manager said that my phone wasn't supported. In the end I used FreeGee, which can install CWM or TWRP with a single touch.
One other speed bump that I ran into is that at least with the CWM and TWRP versions installed by FreeGee (220.127.116.11, and 18.104.22.168, respectively at this point), If you encrypt your phone, you cannot make a nandroid backup.
Really? You have to choose between extra security or having a backup? Android seems to still have a lot of growing up to do. But I digress...
One notable difference that I ran into between TWRP and CWM is that TWRP puts your nandroid backups out in the open on the SD card, while CWM puts them in a protected area. Either technique has a downside. This means that with TWRP backups, a rogue application might be able to access sensitive bits of your phone. With CWM backups, this means that a non-root-enabled application won't be able to access the backups, even if you want it to. We'll run into this later. The other thing I ran into with my LG phone is that backups are dated 1970. Apparently LG doesn't expose time capability to the environment that CWM/TWRP run in. My workaround here is to use ROM Manager to rename the backup accordingly.
Now we've got a nandroid backup, and the next step is to get the data off of the phone. Rsync backup for Android
works quite well. The default sdcard backup field used by this app ended up backing up a single symlink to my NAS, so I ended up changing it to back up /storage/emulated/legacy instead, and that works fine. Now with a single click I can sync my photos and anything else that lives on the SD card. And rsync over ssh is quite efficient as well.
Now, how to get the nandroid backups from CWM to rsync. While they're physically on the SD card storage of the phone, they're logically protected and not visible on /sdcard for security reasons. How to we back them up? We need rsync to have root access. I've tried a couple of options
to make this happen, either by playing with the command-line options in the rsync android app, as well as by trying to create a wrapper for the rsync binary. No luck with either route. While not as simple, my fallback technique was to run a script to relax the permissions on the directory where the CWM nandroid backups live. And then once I'm done rsyncing them, I run a different script to restore the permissions.
mount -o rw,remount /
chmod 755 /mnt/shell
mount -o ro,remount /
mount -o rw,remount /
chmod 700 /mnt/shell
mount -o ro,remount /
You can use whichever android script runner app you're comfortable with. The (separate) rsync job that I created for CWM backups works on: /mnt/shell/emulated/clockworkmod.
That's about it. What's a sane backup strategy to use for your phone? Once you've got your phone set up mostly how you like, reboot to CWM and create a backup of the OS. Rsync this as described above. As you normally use your phone, rsync the sdcard periodically. If you want to have backups of your apps and system data as well, use something like TitaniumBackup to back them up to the SD card. Encrypt them if you want them to be protected. Now, every time you rsync the SD card, you'll have backups of that app data as well.
So in the end, you'll have a nandroid backup as your "bare metal" backup. I'd only expect you to create these when you do major OS changes. The usual backup will be the normal sdcard rsync job, which functions as an incremental backup. Run this job as frequently as you like. Now if you ever need to restore your phone, go into CWM and restore the backup. Then go restore the SD card contents, and restore the Titanium Backup. You'll obviously need CWM on the new phone (if it was replaced), and if you've cleared the CWM backup off of the phone itself (e.g. due to space constraints) you'll need to copy it back to the phone (or otherwise make it available, e.g. via USB OTG) before proceeding.