Ask Klaus

Ask Klaus

Article from Issue 195/2017
Author(s):

Klaus Knopper answers your Linux questions about Android USB backup.

Android USB Backup

I would like to back up my Android-based smartphone to a large hard disk attached to a Linux computer while the phone is connected via USB. Apart from all the apps out there that use their own archive format, is there something simple that I can do from the Linux desktop for creating a backup that is also readable from Linux without needing a special program or app?

The universal tool for accessing Android at the system level over USB or the network is the adb command, which exists in the android-tools-adb Debian and Ubuntu package.

Install adb (use the "unstable" Debian branch to get the newest version) with:

sudo apt-get install [-t unstable] android-tools-adb

adb has a way to create Android backup archives of apps and associated data via the backup command, but it also allows you to execute shell commands on the phone. If the Android system has the option to run commands as root, a full backup via the standard tar command is possible, otherwise only globally readable data, such as recorded pictures or videos, can be archived.

To access the smartphone, adb must be enabled in Android's "developer options" first. By default, this menu is hidden and only appears if you click seven times on the Build number item in Android's Settings | About device menu. After seven clicks, a popup should tell you that "You are now a developer!" or "Developer mode has been enabled." Back to the main Settings menu, the box under Developer options | USB debugging should be checked to allow you to access the phone over USB (Figure 1).

Figure 1: The enabled Developer options screen.

If after connecting the phone to the computer via USB cable a notification requests your permission to access the phone from that computer, tick the checkbox and click OK. In the Terminal window on the Linux desktop side, you can now start the adb commands for accessing the phone. Table 1 shows a small summary of commands.

Table 1

adb Commands

Command

Description

adb root

Give root permissions for subsequent commands on the smartphone (requires a rooted phone and may ask for permission).

adb shell

Log in to a remote shell on the smartphone; feels almost like a shell on a desktop computer (cd, ls, …, type exit to leave).

adb backup <options>

Back up apps and app data in encrypted format.

adb restore <options>

Restore apps and app data in encrypted format.

adb exec-out "<command>"

Run command on Android and capture its unfiltered binary output (stdout) – undocumented feature.

adb exec-in "<command>"

Run command on Android and feed the unfiltered console input (stdin) – undocumented feature.

adb help

Short help (man page is more verbose).

The exec-in and exec-out feature, which is not mentioned in the man page or in help yet, provides an easy and fast way to transfer binary data between Android and the attached Linux computer. After entering

adb root

(works only for rooted phones), you can create a compressed tar stream of the phone's critical data partition. To transfer and store it to a backup file on your computer, issue:

adb exec-out "GZIP=-1 tar -zcpf - /data 2>/dev/null" > android-data.tar.gz
  • GZIP=-1 tar -zcpf - creates (-c) and streams a tar archive to standard output (-f -), which is compressed (-z) with GZIP level 1 (-1) for fastest compression, while also archiving the secure Linux contexts and special permissions (-p). Secure Linux contexts, by the way, are important; they allow apps to access their specific data files, because Android in "strict" SELinux mode will in fact deny access to files if these special modes are not set correctly, causing apps to crash. Of course, it's a security feature, so tar on newer Android versions has the (also yet undocumented) -p option for storing additional permission information.
  • /data is the partition containing settings and app data. (The stuff you definitely want to keep, even if you decide to upgrade the /system partition with a new Android version.)
  • 2>/dev/null discards error messages. If the command terminates very quickly, you might want to remove that option to see what happens (maybe the -p option is not supported in older Android versions), but error messages and notices can pollute the tar archive data if inserted into the stream, so it's recommended to send them to /dev/null if the command is otherwise OK.
  • > android-data.tar.gz sends the captured tar archive stream from standard output to the file android-data.tar.gz. No intermediate data is stored on the Android side.

To check the archive content, run:

tar -ztvf android-data.tar.gz

(No adb here; it's local!) To restore parts of the archive back to the phone, the command is:

adb exec-in "tar -zxpf - data/media/0/Music" < android-data.tar.gz
  • tar -zxpvf - decompresses (-z), extracts (-x), and restores data from the archive streamed as input (-f -), also restoring secure Linux contexts (-p).
  • data/media/0/Music indicates the only part of the archive you want to restore (e.g., the Music directory here). Unless you are recovering from a crash or after a "factory reset," you probably don't want to restore the entire archive, because newer versions of settings and files would be overwritten. The default (without specifying a directory) is to extract the entire archive.  
  • < android-data.tar.gz feeds the stored archive as an input stream to adb.

For transferring single files or directories from and to Android, the commands

adb pull <filepath>
adb push <filepath>

are also useful, but they are not as flexible as tar.

Klaus Knopper

Klaus Knopper is an engineer, creator of Knoppix, and co-founder of LinuxTag expo. He works as a regular professor at the University of Applied Sciences, Kaiserslautern, Germany. If you have a configuration problem, or if you just want to learn more about how Linux works, send your questions to: klaus@linux-magazine.com

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News