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
-
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.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.