Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: unpack android backup?

I'd like to unpack an Android backup file using python.

According to http://nelenkov.blogspot.com/2012/06/unpacking-android-backups.html an unencryted adb file can be decompressed using

dd if=mybackup.ab bs=24 skip=1|openssl zlib -d > mybackup.tar

and

tar xvf mybackup.tar

Can these can be done in python? Python has zlib, gzip and tarfile, which seem as if they should be usable. In any event, if they can be done, how to?

Would tarfile.open('filename.tar', 'r:') work for the second step?

I'm on windows, btw.

like image 786
foosion Avatar asked Sep 23 '26 13:09

foosion


1 Answers

If the file is not too big for everything to fit comfortably in memory, after the needed imports from the standard library:

with open('mybackup.ab', 'rb') as f:
    f.seek(24)  # skip 24 bytes
    data = f.read()  # read the rest

tarstream = zlib.decompress(data)
tf = tarfile.open(fileobj=io.BytesIO(tarstream))

Now in tf you have a TarFile instance as documented in https://docs.python.org/2/library/tarfile.html#tarfile-objects , so you can e.g get a list of its contents, extract one or more members, &c.

If the backup is too big for all these bits to fit comfortably in memory, you can of course write any or all of the intermediate results to disk; but if it's small enough keeping everything in memory should result in faster execution.

like image 125
Alex Martelli Avatar answered Sep 25 '26 03:09

Alex Martelli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!