Ask Klaus
Ask Klaus
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).
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, sotar
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 fileandroid-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., theMusic
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 toadb
.
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
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.