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
Direct Download
Read full article as PDF:
Price $2.95
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Find SysAdmin Jobs
News
-
SparkyLinux 6.6 Now Available for Installation
The Debian-based SparkyLinux has a new point release that retools the live USB desktop creator and other changes that give it shiny new-ness.
-
SparkyLinux 6.6 Now Available for Installation
The Debian-based SparkyLinux has a new point release that retools the live USB desktop creator and other changes that give it shiny new-ness.
-
Escuelas Linux 8.0 Now Available
Just in time for its 25th anniversary, the developers of Escuelas Linux have released the latest version.
-
LibreOffice 7.5 Loaded with New Features and Improvements
The favorite office suite of the Linux community has a new release that includes some visual refreshing and new features across all modules.
-
The Next Major Release of Elementary OS Has Arrived
It's been over a year since the developers of elementary OS released version 6.1 (Jólnir) but they've finally made their latest release (Horus) available with a renewed focus on the user.
-
KDE Plasma 5.27 Beta Is Ready for Testing
The latest beta iteration of the KDE Plasma desktop is now available and includes some important additions and fixes.
-
Netrunner OS 23 Is Now Available
The latest version of this Linux distribution is now based on Debian Bullseye and is ready for installation and finally hits the KDE 5.20 branch of the desktop.
-
New Linux Distribution Built for Gamers
With a Gnome desktop that offers different layouts and a custom kernel, PikaOS is a great option for gamers of all types.
-
System76 Beefs Up Popular Pangolin Laptop
The darling of open-source-powered laptops and desktops will soon drop a new AMD Ryzen 7-powered version of their popular Pangolin laptop.
-
Nobara Project Is a Modified Version of Fedora with User-Friendly Fixes
If you're looking for a version of Fedora that includes third-party and proprietary packages, look no further than the Nobara Project.