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
-
The GNU Project Celebrates Its 40th Birthday
September 27 marks the 40th anniversary of the GNU Project, and it was celebrated with a hacker meeting in Biel/Bienne, Switzerland.
-
Linux Kernel Reducing Long-Term Support
LTS support for the Linux kernel is about to undergo some serious changes that will have a considerable impact on the future.
-
Fedora 39 Beta Now Available for Testing
For fans and users of Fedora Linux, the first beta of release 39 is now available, which is a minor upgrade but does include GNOME 45.
-
Fedora Linux 40 to Drop X11 for KDE Plasma
When Fedora 40 arrives in 2024, there will be a few big changes coming, especially for the KDE Plasma option.
-
Real-Time Ubuntu Available in AWS Marketplace
Anyone looking for a Linux distribution for real-time processing could do a whole lot worse than Real-Time Ubuntu.
-
KSMBD Finally Reaches a Stable State
For those who've been looking forward to the first release of KSMBD, after two years it's no longer considered experimental.
-
Nitrux 3.0.0 Has Been Released
The latest version of Nitrux brings plenty of innovation and fresh apps to the table.
-
Linux From Scratch 12.0 Now Available
If you're looking to roll your own Linux distribution, the latest version of Linux From Scratch is now available with plenty of updates.
-
Linux Kernel 6.5 Has Been Released
The newest Linux kernel, version 6.5, now includes initial support for two very exciting features.
-
UbuntuDDE 23.04 Now Available
A new version of the UbuntuDDE remix has finally arrived with all the updates from the Deepin desktop and everything that comes with the Ubuntu 23.04 base.