Skip to content

Instantly share code, notes, and snippets.

@AnatomicJC
Last active May 19, 2024 09:35
Show Gist options
  • Save AnatomicJC/e773dd55ae60ab0b2d6dd2351eb977c1 to your computer and use it in GitHub Desktop.
Save AnatomicJC/e773dd55ae60ab0b2d6dd2351eb977c1 to your computer and use it in GitHub Desktop.
Backup android app, data included, no root needed, with adb

Backup android app, data included, no root needed, with adb

Note: This gist may be outdated, thanks to all contributors in comments.

adb is the Android CLI tool with which you can interact with your android device, from your PC

You must enable developer mode (tap 7 times on the build version in parameters) and install adb on your PC.

Don't hesitate to read comments, there is useful tips, thanks guys for this !

Fetch application APK

To get the list of your installed applications:

adb shell pm list packages -f -3

If you want to fetch all apk of your installed apps:

for APP in $(adb shell pm list packages -3 -f)
do
  adb pull $( echo ${APP} | sed "s/^package://" | sed "s/base.apk=/base.apk /").apk
done

To fetch only one application, based of listed packages results:

adb pull /data/app/org.fedorahosted.freeotp-Rbf2NWw6F-SqSKD7fZ_voQ==/base.apk freeotp.apk

Backup applications datas

To backup one application, with its apk:

adb backup -apk org.fedorahosted.freeotp -f freeotp.adb

To backup all datas at once:

adb backup -f all -all -apk -nosystem

To backup all datas in separated files:

for APP in $(adb shell pm list packages -3)
do
  APP=$( echo ${APP} | sed "s/^package://")
  adb backup -f ${APP}.backup ${APP}
done

Restore Applications

First, you have to install the saved apk with adb:

adb install application.apk

Then restore the datas:

adb restore application.backup

Extract content of adb backup file

You will need the zlib-flate binary. You will able to use it by installing the qpdf package.

apt install qpdf

Then, to extract your application backup:

dd if=freeotp.adb bs=24 skip=1 | zlib-flate -uncompress | tar xf -

If adb backup doesn't work

If you have problems using adb backup (empty files, no prompts, other oddness), you can give a try to bu backup through adb shell. Thanks to @nuttingd comment.

Backing up

adb shell 'bu backup -apk -all -nosystem' > backup.adb

Restoring

adb shell 'bu restore' < backup.adb

Miscellaneous: remove non-wanted applications

Sometimes, you have already installed (but non wanted) apps when you buy an Android Smartphone. And you can't uninstall these apps. So Bad. And some pre-installed app are not present on PlayStore. WTF.

You can remove them with this command, and root is not needed:

adb shell pm uninstall --user 0 non.wanted.app

You can first disable them, then when you are sure, you can remove them.

To list disabled apps:

adb shell pm list packages -d

Example:

# Google Chrome
adb shell pm uninstall --user 0 com.android.chrome
# Gmail
adb shell pm uninstall --user 0 com.google.android.gm
# Google Play Films et Séries
adb shell pm uninstall --user 0 com.google.android.videos
# Youtube
adb shell pm uninstall --user 0 com.google.android.youtube
# Google Play Music
adb shell pm uninstall --user 0 com.google.android.music
# Google Hangouts
adb shell pm uninstall --user 0 com.google.android.talk
# Google Keep
adb shell pm uninstall --user 0 com.google.android.keep
# Google Drive
adb shell pm uninstall --user 0 com.google.android.apps.docs
# Google Photos
adb shell pm uninstall --user 0 com.google.android.apps.photos
# Google Cloud Print
adb shell pm uninstall --user 0 com.google.android.apps.cloudprint
# Google Actualités et météos
adb shell pm uninstall --user 0 com.google.android.apps.genie.geniewidget
# Application Google
adb shell pm uninstall --user 0 com.google.android.googlequicksearchbox

Resources

https://stackoverflow.com/questions/4032960/how-do-i-get-an-apk-file-from-an-android-device https://androidquest.wordpress.com/2014/09/18/backup-applications-on-android-phone-with-adb/ https://stackoverflow.com/questions/34482042/adb-backup-does-not-work https://stackoverflow.com/questions/53634246/android-get-all-installed-packages-using-adb https://www.dadall.info/article657/nettoyer-android https://etienne.depar.is/a-ecrit/Desinstaller-des-applications-systemes-d-android/index.html

@John-Gee
Copy link

John-Gee commented Jun 1, 2023

Restore works with bu or straight adb here, but it does not seem to actually change anything in the apps... Does it not include settings and such of apps?

@ArRiMartinez
Copy link

ArRiMartinez commented Jul 11, 2023

/* Add File Name Parameter */

$adbExe = "(location of adb.exe)"
$params = $adbExe + ' shell pm list packages -3 -f'

Invoke-Expression $params | Foreach-Object{
   $packageName = $_.Substring(0,$_.IndexOf("-")) -replace "^package:/data/app/(.*)$", "`$1"
   $apkName = $_.Substring(0,$_.IndexOf("base.apk"))+$_.Substring($_.IndexOf("base.apk"),8) -replace "^package:(.*)$", "`$1"
   $apkFileName = $_.Substring($_.IndexOf("base.apk="),($_.Length - $_.IndexOf("base.apk="))).replace("base.apk=","")
   Write-Host $apkName 
   $params = $adbExe + ' pull ' + $apkName.Trim() + ' ' + $apkFileName + '.apk'
   Invoke-Expression $params
}

Powershell to get all .apk. Inspired by @ToniCipriani's script above!

$adbExe = "(path to adb.exe)"

adb shell pm list packages -3 -f | Foreach-Object{
    $packageName = $_.Substring(0,$_.IndexOf("-")) -replace "^package:/data/app/(.*)$", "`$1"
    $apkName = $_.Substring(0,$_.IndexOf("base.apk"))+$_.Substring($_.IndexOf("base.apk"),8) -replace "^package:(.*)$", "`$1"
    Write-Host $packageName
    $params = $adbExe + ' pull ' + $apkName.Trim() + ' ' + $packageName + '.apk'
    Invoke-Expression $params
}

it is a bit 'weird' that you use (i know, you copied from @ToniCipriani) "adb shell" as the first command and not using the $adbExe variable, so that implies that adb.exe is in the current folder or it is in your PATH. either way, you don't need the $adbExe variable then. so i would suggest this small improvement;

$adbExe = "(location of adb.exe)"
$params = $adbExe + ' shell pm list packages -3 -f'

Invoke-Expression $params | Foreach-Object{
    $packageName = $_.Substring(0,$_.IndexOf("-")) -replace "^package:/data/app/(.*)$", "`$1"
    $apkName = $_.Substring(0,$_.IndexOf("base.apk"))+$_.Substring($_.IndexOf("base.apk"),8) -replace "^package:(.*)$", "`$1"
    Write-Host $packageName
    $params = $adbExe + ' pull ' + $apkName.Trim() + ' ' + $packageName + '.apk'
    Invoke-Expression $params
}

@seanybiker
Copy link

Wait so

adb backup -f all -all -apk -nosystem

is only the data thats saved to the apps and not the data of the app itself? Like I have to also do another command to save backup apps. Hmmmm.

It days on my phone screen thats its been asked todo "full backup" I tapped on "backup data" it cant be tapped again but theres nothing on my phone screen or pc letting me know if its actually backing up.

@nightpool
Copy link

When I try unpacking this data with zlib-flate, I get the following: "zlib-flate: flate: inflate: data: incorrect header check". I'm using ADB on MacOS using the 10.5.0 version of qpdf from Homebrew. Any thoughts?

@kelvinkad
Copy link

How to restore all apps at once?

@druckgott
Copy link

druckgott commented Aug 25, 2023

I use: adb backup -f all -all -apk -nosystem to backup all datas and then I use --> a fill called all is generated
then I use:
adb restore application.backup --> adb restore all and restore is running. But then I open the app all apps are like they installed. So there are not datas inside. I have a Huawei P20 and transfer it to a samsung s23

I also tested it like this:

adb devices
adb backup -apk -shared -nosystem -all -f C:\adb\backup.ab
adb devices
adb restore C:\adb\backup.ab

But did not work

@Peter-Ries
Copy link

Need to backup my Pixel 4a as it's now out of support.

Running Linux with 'adb backup ...' and tried 'adb shell 'bu backup...'

both give me a hint that backup is deprecated and resulting DATA files have 47 bytes.

What has changed? How can I backup DATA of my apps as of today?

Thanks!

@DDvO
Copy link

DDvO commented Oct 7, 2023

adb backup was made essentially useless, for 'security reasons' since Android 12 👎 👎
And most of those HowTo articles were not updated and thus have become misleading 👎

@DDvO
Copy link

DDvO commented Oct 8, 2023

Of course, it would be possible to provide secure ways of doing data backups.
For instance, simply define a backup password to be used with adb backup and other tools.
If a device encryption password is set, which is recommended, this could be reused.
Or more elegantly, one could set up an asymmetric key pair, as can be done with ssh,
and use this, which boils down to a very simple form of device owner certificate.

@DDvO
Copy link

DDvO commented Oct 8, 2023

Since Google etc. simply crippled adb backup without providing a user-controllable backup mechanism,
one can only conclude that they do not want us users have control over our data on our devices 👎 👎 👎

@XdekHckr
Copy link

Need to backup my Pixel 4a as it's now out of support.

Running Linux with 'adb backup ...' and tried 'adb shell 'bu backup...'

both give me a hint that backup is deprecated and resulting DATA files have 47 bytes.

What has changed? How can I backup DATA of my apps as of today?

Thanks!

you have to root your device and use app like swiftbackup or something else I guess

@XdekHckr
Copy link

Since Google etc. simply crippled adb backup without providing a user-controllable backup mechanism, one can only conclude that they do not want us users have control over our data on our devices 👎 👎 👎

then how apps developers are able to create backup and restore their applications if it's deprecated by google?!

@XdekHckr
Copy link

Backup process is starting for all apps but when I try with only one app it says "creating backup" and after 1 second it's closing and only makes 1 kb file, what a crap

@nombre10apellido10
Copy link

nombre10apellido10 commented Nov 2, 2023

The method works.

Remember to enable "Stay awake" and "Enable usb debug" in "Developer options"
Check your syntaxis. Look at this example.

In your terminal, go to a directory where the backup file will be stored IN YOUR COMPUTER
adb usb

adb backup -user 0 -apk -obb -shared -keyvalue com.nintendo.zaka

must specify user
I suggest you include apk
You most probably want the app libraries/databases (obb)
Just to be on the safe side, include shared storage
I have no idea what keyvalue means, but it looks like encryption
Finally, the package name.

Now unlock your phone screen as requested and accept

The file should be located in the computer folder you are in. Check the file size. For the app in the example it as over 1 GB

If you are successful, don't ask me how to restore it. Read TFA
adb shell bu help

edit: spelling

@Bobesmj
Copy link

Bobesmj commented Nov 17, 2023

After extract .adb file, how I can compress it back? I've tried to ark but can't restore that file

@DyKey13
Copy link

DyKey13 commented Nov 19, 2023

Hi. I want to extract the backup, but it is not working on CMD, what app shoould I use to run this then?

@meoow
Copy link

meoow commented Nov 22, 2023

Backing up the base.apk is not enough. The script didn't consider the split apks which many apps on the Google Play store are in this format now. the split apks which can be installed using adb install-multiple. you probably need to use adb shell pm path {appid} to get the paths of the split apks first, and backup each one of them.

@Cr4z33
Copy link

Cr4z33 commented Nov 23, 2023

I wish all the good old ADB restore methods worked for WSL too!

For all the apps not synced with the cloud I had to do everything from scratch by hand... 😫

@weskerty
Copy link

weskerty commented Jan 4, 2024

I came here with the idea of cloning WhatsApp from a phone with PlayIntegrity and then restoring it to one with root and I read that WhatsApp cannot be cloned

@0nlyflash
Copy link

Thank you it worked.

@krupennikov
Copy link

krupennikov commented Feb 9, 2024

For those who don't have bash for Windows I recoded commands to work for powershell:

Backup apk of all installed apps

foreach ($APP in $(adb shell pm list packages -f -3)) {Invoke-Expression $($APP.replace('package:','adb pull ').replace('base.apk=','base.apk ')+'.apk')}

Backup data of all installed apps

foreach ($APP in $(adb shell pm list packages -3)) {Invoke-Expression $($APP.replace('package:','adb backup -f ')+'.backup '+$APP.replace('package:',''))}

Thanks for the description of the commands for powershell, but you must use .\ before adb. Otherwise it won't work.

@oxij
Copy link

oxij commented Feb 15, 2024

I made abarms, which a tool that helps to simplify a lot of this.

`abarms` is a *handy* Swiss-army-knife-like tool/utility/console app for POSIX-compatible systems for manipulating Android Backup files (`*.ab`, `*.adb`) produced by `adb backup`, `bmgr`, and similar tools.

With abarms you can do a full-system adb backup and split into per-app pieces later.

It's a pure Python tool, you don't need a heavy Java VM to run it, like with android-backup-extractor and android-backup-toolkit.

@LoneDev6
Copy link

@oxij sadly I doubt that works on Android 12 due to limitations.

To help protect private app data, Android 12 changes the default behavior of the adb backup command. For apps that target Android 12 (API level 31) or higher, when a user runs the adb backup command, app data is excluded from any other system data that is exported from the device.

@robt24
Copy link

robt24 commented Mar 14, 2024

@krupennikov , your scripts are working, what to do for restore? just replace pull with install and backup with restore ?

--
adb backup -f all -all -apk -nosystem
give me:
Now unlock your device and confirm the backup operation.

@krupennikov
Copy link

@robt24, I have not checked this script for restoring.

@AnatomicJC
Copy link
Author

Thanks all for your comments. I wrote this gist some years ago and I am not using it anymore, as backup doesn't work for some apps.

@Amandeepyadav85630
Copy link

adb pull $( echo ${APP} | sed "s/^package://" | sed "s/base.apk=/base.apk /").apk

'sed' is not recognized as an internal or external command,
operable program or batch file.

@NguyenQuocVuong-git
Copy link

Is there any way to backup apps to my device? I want to use ADB to save backup files to my device and get the path of the backed-up file. Does anyone have a solution to this issue?

@XdekHckr
Copy link

Is there any way to backup apps to my device? I want to use ADB to save backup files to my device and get the path of the backed-up file. Does anyone have a solution to this issue?

Just root your phone and use swift backup

@NguyenQuocVuong-git
Copy link

Is there any way to backup apps to my device? I want to use ADB to save backup files to my device and get the path of the backed-up file. Does anyone have a solution to this issue?

Just root your phone and use swift backup

Is there any way to solve the problem without rooting? I have two current issues:

1.Backup the app into devices.
2.Get the path of the app that has just been backed up."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment