Sauvegarde des applications depuis adb - adb backup (Obsolète)

Dernière mise à jour : LineageOs15 - Android 9

Ce document indique comment sauvegarder et restaurer les applications et leurs données au moyen de adb backup, adb restore

Sommaire

1 - Présentation

1.1 - Commandes et options de sauvegarde

Repris de https://forum.xda-developers.com/galaxy-nexus/general/guide-phone-backup-unlock-root-t1420351

$ adb backup \
[-f <file>] \ [-all | <packages...>] \ [-apk |-noapk] \
[-shared |-noshared] \ [-system |-nosystem] \

1.2 - Limitations

adb backup permet de sauvegarder les applications et leurs données du périphérique vers un fichier archive (.ab) sur le PC et inversement de restaurer les applications et leurs données à partir du fichier archive.

Attention : Cette méthode possède une limitation importante

<application label="Firefox" ....allowBackup="false" ... >      
$ adb backup com.google.android.diskusage
Now unlock your device and confirm the backup operation...

$ du -b backup.ab
7922 backup.ab
$ adb backup com.booking_data_apk.ab
Now unlock your device and confirm the backup operation...

$ du -b backup.ab
47 backup.ab

2.3 - Contenu de la sauvegarde

$ ( printf "\x1f\x8b\x08\x00\x00\x00\x00\x00" ; tail -c +25 \
<fichier_archive.ab> \
) | tar xfvz -
$ tree  apps
apps
└── com.google.android.diskusage
├── a
│ └── base.apk
├── _manifest
├── r
│ └── app_binary
│ └── scan5
└── sp
└── settings.xml

3 - Sauvegarde

3.1 - Sauvegarde de toutes les applications

$ adb backup -all

$ du -h backup.ab
11M backup.ab
$ adb backup -all -apk -f BU_data_apk.ab

$ du -h backup.ab 120M BU_data_apk.ab
$ adb backup -all -apk -shared -f BU_data_apk_SD.ab

$ du -h BU_data_apk_SD.ab
1,2G BU_data_apk_SD.ab

3.2 - Sauvegarde d'applications utilisateur

$ adb backup com.google.android.diskusage -f diskusage_data_noapk.ab 
$ adb backup com.google.android.diskusage -apk -f diskusage_data_apk.ab 
$ du -b *
252315 com.google.android.diskusage_3080.apk
225426 diskusage_data_apk.ab
7921 diskusage_data_noapk.ab
$ adb shell cmd package list packages -3 |cut -d: -f2|while read apk;\
do adb backup $apk -f ${apk}_data.ab ;done
Now unlock your device and confirm the backup operation...
Now unlock your device and confirm the backup operation...
Now unlock your device and confirm the backup operation...
.....

Variante de commande

$ for i in cat $(adb shell cmd package list packages -s |cut -d: -f2);\
do adb backup $i -f ${i}_data.ab; done
$ adb shell cmd package list packages -3 |cut -d: -f2|while read apk;\
do adb backup $apk -apk -f ${apk}_data_apk.ab ;done
Now unlock your device and confirm the backup operation...
Now unlock your device and confirm the backup operation...
Now unlock your device and confirm the backup operation...
.....
$ du -b *.ab
1346 com.artifex.mupdf.viewer.app_data.ab
10617826 com.artifex.mupdf.viewer.app_data_apk.ab
1884615 com.aurora.adroid_data.ab
7104031 com.aurora.adroid_data_apk.ab
60089 com.aurora.store_data.ab
5984956 com.aurora.store_data_apk.ab
47 com.booking_data.ab
47 com.booking_data_apk.ab
6605 com.flinkapps.safteynet_data.ab
4351548 com.flinkapps.safteynet_data_apk.ab
7921 com.google.android.diskusage_data.ab
225426 com.google.android.diskusage_data_apk.ab
85434 com.ldf.tele7.view_data.ab
30095336 com.ldf.tele7.view_data_apk.ab
47 com.termoneplus_data.ab
47 com.termoneplus_data_apk.ab
195056 com.topjohnwu.magisk_data.ab
2208106 com.topjohnwu.magisk_data_apk.ab
47 com.tripadvisor.tripadvisor_data.ab
47 com.tripadvisor.tripadvisor_data_apk.ab
941 diskinfo_data.ab
971205 diskinfo_data_apk.ab
941 diskinfo_data_noapk.ab
7921 diskusage_data.ab
225426 diskusage_data_apk.ab
7921 diskusage_data_noapk.ab
47 flar2.devcheck_data.ab
47 flar2.devcheck_data_apk.ab
40865 fr.neamar.kiss_data.ab
321219 fr.neamar.kiss_data_apk.ab
10414 game.puzzle.blockpuzzle_data.ab
9975685 game.puzzle.blockpuzzle_data_apk.ab
3671816 io.yuka.android_data.ab
27753214 io.yuka.android_data_apk.ab
47 it.pinenuts.linuxnews_data.ab
47 it.pinenuts.linuxnews_data_apk.ab
47 it.pinenuts.rassegnastampa_data.ab
47 it.pinenuts.rassegnastampa_data_apk.ab
47 it.pinenuts.technews_data.ab
47 it.pinenuts.technews_data_apk.ab
941 me.kuder.diskinfo_data.ab
971205 me.kuder.diskinfo_data_apk.ab
1477 me.zhanghai.android.files_data.ab
2545585 me.zhanghai.android.files_data_apk.ab
47 net.programmierecke.radiodroid2_data.ab
47 net.programmierecke.radiodroid2_data_apk.ab
1675027 org.fdroid.fdroid_data.ab
7617044 org.fdroid.fdroid_data_apk.ab
13290 org.freeandroidtools.safetynettest_data.ab
2138524 org.freeandroidtools.safetynettest_data_apk.ab
47 org.mozilla.firefox_data.ab
0 org.mozilla.firefox_data_apk.ab

Conclusion : sur les 22 applications utilisateur, 7 ne sont pas sauvegardables par adb backup ..... Lorsque les données ne sont pas sauvegardables, les applications non plus et réciproquement

3.3 - Sauvegarde d'applications système

$ adb shell cmd package list package -s |cut -d: -f2|while read apk;\
do adb backup $apk -f sys_${apk}_data_noapk.ab ;done
$ du -b sys*
47 sys_android.auto_generated_rro___data_noapk.ab
47 sys_android_data_noapk.ab
47 sys_com.android.backupconfirm_data_noapk.ab
1044 sys_com.android.bips_data_noapk.ab
....

$ du -b sys* |wc -l
121

$ du -b sys*noapk.ab |grep 47| wc -l
63

Conclusion : sur les 121 applications système, 63 n'ont pas leur données sauvegardables par adb backup

$ adb shell cmd package list package -s |cut -d: -f2|while read apk;\
do adb backup $apk -apk -f sys_${apk}_data_apk.ab ;done
Now unlock your device and confirm the backup operation...
Now unlock your device and confirm the backup operation...
Now unlock your device and confirm the backup operation...
.....
$ du -b sys*.ab
47 sys_android.auto_generated_rro___data_apk.ab
47 sys_android_data_apk.ab
47 sys_com.android.backupconfirm_data_apk.ab
1044 sys_com.android.bips_data_apk.ab
47 sys_com.android.bluetooth_data_apk.ab
1067 sys_com.android.bluetoothmidiservice_data_apk.ab
.......

$ du -b sys*.ab |wc -l
121

$ du -b sys*.ab |grep 47 |wc -l
63

Conclusion sur les 121 applications système, les données et applications de 63 applications ne sont pas sauvegardables par la commande adb backup

4 - Restauration

4.1 - Principe

- If the backup contains apk+data for an app, restore will work. If contains only the data, you must install the app first on the device and then restore. Installing the app later won't work.

- If you have the device encrypted with a password, you must use that particular one for backup creation and restore. You can't create a backup without password or a different one in that particular case, or if you try to restore will fail. (non testé)

$ adb restore <fichier_sauvegarde>

4.2 - Test Restauration application utilisateur

$ adb shell cmd package list packages -3 disk
package:me.kuder.diskinfo
package:com.google.android.diskusage
$ adb backup -apk com.google.android.diskusage
Now unlock your device and confirm the backup operation...

$ du -b backup.ab
225382 backup.ab
$ adb uninstall com.google.android.diskusage
Successs

$ adb shell cmd package list packages diskusage
$ adb restore backup.ab
Now unlock your device and confirm the restore operation.
$ adb shell cmd package list packages diskusage

On constate que l'application n'a pas été réinstallée. Problème d'accès ? de version ?