Skip to content

Instantly share code, notes, and snippets.

@AnatomicJC
Last active August 12, 2023 04:08
Star You must be signed in to star a gist
Embed
What would you like to do?
Backup android app, data included, no root needed, with adb

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

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

@akshaynkulkarni
Copy link

I had issue performing backup of specific app using :
adb backup -f freeotp.adb -apk org.fedorahosted.freeotp

It should be:

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

This gave me valid backup file, the previous gave empty file

@nuttingd
Copy link

I've had problems using adb backup (empty files, no prompts, other oddness), but had success using bu backup through adb shell.

Backing up

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

Restoring

adb shell 'bu restore' < backup.adb

@AnatomicJC
Copy link
Author

Thank you @akshaynkulkarni , I got an empty backup some weeks ago too, I edited the gist

@AnatomicJC
Copy link
Author

Thank you @nuttingd, I didn't know the bu tool 👍

@waseeld
Copy link

waseeld commented Mar 29, 2022

that is gonna take full backup for my phone ? or just for the data apps ?

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

@Ananthusubramanian
Copy link

that is gonna take full backup for my phone ? or just for the data apps ?

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

From source code , it should backup apk's too . Need to try
Source Code :https://android.googlesource.com/platform/frameworks/base/+/ccbf84f/cmds/bu/src/com/android/commands/bu/Backup.java

@dephunk
Copy link

dephunk commented Apr 12, 2022

I've had problems using adb backup (empty files, no prompts, other oddness), but had success using bu backup through adb shell.

Backing up

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

Restoring

adb shell 'bu restore' < backup.adb

"inaccessible or not found" error :(

@giupardeb
Copy link

I used this adb backup -f all -all -apk -nosystem to backup all apk and its data. How can I restore it?

@Adrianowsky94
Copy link

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:',''))}

@Velosofy
Copy link

@dephunk use double-quote instead of single-quote

@yfdyh000
Copy link

The "dd if=freeotp.adb bs=24 skip=1 | zlib-flate -uncompress | tar xf -" is very slow, about 50 kB/s.

With the dd if=freeotp.adb bs=32MB skip=24 iflag=skip_bytes | zlib-flate -uncompress > your.tar, you can get x MB/s.

Ref: https://0xffff.one/d/900 (Chinese).

@kyoyacchi
Copy link

How can I backup one app with adb shell?

@rocket-pig
Copy link

rocket-pig commented Sep 4, 2022

I was so excited for this to work, to escape the "need root for full backup / getting root requires factory reset" catch 22.

Unfortunately, does not (Samsung Galaxy a03s). adb reports "root access forbidden in production releases" womp womp. There is also no setting available in dev mode to allow root access.

Moral of story I suppose is - to immediately root phone upon purchase, or just expect at some point you're going to end up here.

Oh. I did learn you can boot into recovery and get read access to /system, which you could then use adb (I assume) and back that up...but it's not /data and therefore still defeats purpose.

@ThemGo
Copy link

ThemGo commented Sep 18, 2022

This worked for me with WSL on windows.

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

adb=/mnt/c/Program\ Files\ \(x86\)/android-platform-tools/adb.exe
for APP in $("$adb" shell pm list packages -3 -f)
do
  "$adb" pull $(  echo ${APP} | sed "s/^package://" | sed "s/base.apk=/base.apk /" | sed 's/\r//g').apk
done

It blocked for some apks, so I skipped and continued with Ctrl-C.

To backup all datas in separated files:

adb=/mnt/c/Program\ Files\ \(x86\)/android-platform-tools/adb.exe
for APP in $("$adb" shell pm list packages -3)
do
  APP=$(  echo ${APP} | sed "s/^package://" | sed "s/base.apk=/base.apk /" | sed 's/\r//g')
  "$adb" backup -f ${APP}.backup ${APP}
done

Little problem: android prompts for passphrase for each backup file...

@Eddict
Copy link

Eddict commented Oct 19, 2022

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
}

@EslamSad3
Copy link

how to restore

@EslamSad3
Copy link

@aiyagari
Copy link

@desai-hardik
Copy link

To take back of all app make sh script
backup.sh with below code

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

Thanks to @AnatomicJC for code

then run above script from your terminal e.g. sh backup.sh

Note
1 Backup path: It will take the folder in the same folder from where you run this script.
2 Tested on mac os Big Sur & Android 11

@desai-hardik
Copy link

Any help for get Application Name(application label)

e.g. 
Package name = com.example.app
Application name = Example App

@elunesgraces
Copy link

how to restore

https://github.com/nelenkov/android-backup-extractor

in PowerShell using this gives me the "'<' operate is reserved for future use." error.

Hunted around and found out that you need to use it in cmd instead. However using it there gives me a "< was unexpected at this time" error instead. Any ideas?

@knoxell
Copy link

knoxell commented Apr 5, 2023

I came across how to know what apps have the allow backup flag set, so all credits go to St0rm.

Sadly that doesn't mean you can get these apps data (e.g. whatsapp, netflix ... )

adb shell '
for package in $(pm list packages -3 | cut -f2 -d":"); do
     dumpsys package $package | grep "ALLOW_BACKUP" > /dev/null  && echo $package;
done'

@YuJin44
Copy link

YuJin44 commented May 23, 2023

Thanks, I just tried this, but unfortunatelly out of 50 apps, I could backup only 5. I have tried options above, but majority of the apps just didn't work. After some search on the web I found that developers of apps can disable the backup posibility:
https://developer.android.com/guide/topics/manifest/application-element#allowbackup
if they do that it seems that without rooting the device you have no chance creating backup with adb.
Why they do that?
I guess maybe because for some apps, like games, you can pay in google playstore to disable ads and that information is then stored in userdata, so then they want to protect it from duplicating and "pirating", but I don't actually know, especially since only 10% of apps on my phone could actually be backed up.
So than the only thing you probably can do without rooting the device is factory reset the new phone and use the backup restore function while setting it up.

@plmi
Copy link

plmi commented May 26, 2023

@YuJin44 This is how the security model of the linux kernel works. Every app gets it's unique UID and an application folder in /data/data/. To this folder only the application itself has access. If other applications need to access it the developer must either implement a ContentProvider or the user needs to grant specific permissions. adb backup can only backup the data if the application developer wants you to backup data. If you could backup everything why would you need the linux's permission model in the first place. The purpose is so that the app developer can exclude sensitive information from backups.

@VPraharsha03
Copy link

@AnatomicJC

adb backup -f freeotp.DataOnly.adb -noapk org.fedorahosted.freeotp

could be useful for just backing up only the app data

@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?

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