Skip to content

How to extract adb backups made with the --twrp option? #898

Open

Description

I have a Samsung Galaxy S2 GT-I9100 smartphone with LineageOS and TWRP. Every week I make a backup with the following command:

adb backup -f twrp-20170322.ab --twrp boot data system

I may optionally use the --compress option, too.

Is there a way to extract the twrp-20170322.ab backup file with stardard GNU/Linux command line tools? There are a number of tutorials online that use dd/tar/openssl, but they don't work when using the --twrp option.

I previously asked this question on XDA Developers and on Stack Exchange without success.

Activity

  1. ClayF commented on May 16, 2017

    @ClayF

    Looking for a way to do this as well.

  2. jontis commented on Jan 29, 2018

    @jontis

    Check your file:
    $ head backup.ab

    Twrp has added some things in the beginning. Skipping those with:
    $ dd if=backup.ab bs=512 skip=3 | head

    Got me to a semiworking extraction.

    $ dd if=backup.ab bs=512 skip=3 | tar -x

  3. heartcomputers commented on Mar 31, 2018

    @heartcomputers

    I also am having trouble with this

  4. tofurky commented on Jun 16, 2018

    @tofurky

    just saw this show up in the comment section of the stackexchange link posted in the issue (on june 9th): https://github.com/arifogel/twrpabx

    it seems to work as advertised. the generated output file causes lots of errors like "tar: Malformed extended header: missing equal sign" when listing/extracting with GNU tar. that's an artifact of the twrp "tar" format though, i think. i just tested and it seems to extract with tar OK even with those error messages. i did not test with a compressed backup.

    to build it on linux:

    autoreconf -i
    ./configure
    make
    

    thanks @arifogel

  5. arifogel commented on Jun 25, 2018

    @arifogel

    @tofurky I'm glad you were able to make use of my tool.
    FWIW I don't recall getting any tar complaints when I ran at least on my stuff.
    I'm open to refining the project if you would be willing to toss me some test cases/issues.

  6. heartcomputers commented on Jul 9, 2018

    @heartcomputers

    @tofurky thanks for sharing this. @arifogel thanks for putting this little program together. Unfortunately, when pointing it at my .ab file (uncompressed), I get "Corrupt start of data header". It outputs a system.ext4.win file which my archive manager complains is corrupt but extraction with the tar command seems to go smoothly. Unfortunately it stops at the system partition.

  7. AlD commented on May 13, 2020

    @AlD

    The reason for the tar errors is the weird block header format that TWRP uses. Unless the format changed in the last 2 years, this small Python script will convert a TWRP .ab backup to .tar: https://gist.github.com/AlD/cb443fe4ba152e1de8a42fa72721552b

  8. CoREse commented on Jun 17, 2020

    @CoREse

    The reason for the tar errors is the weird block header format that TWRP uses. Unless the format changed in the last 2 years, this small Python script will convert a TWRP .ab backup to .tar: https://gist.github.com/AlD/cb443fe4ba152e1de8a42fa72721552b

    this simple script helps a lot! thank you!
    and of course, it works!

  9. unilock commented on Jan 29, 2021

    @unilock

    Here's how I extracted my multi-partition ADB / TWRP backup when nothing else worked:

    • Opened "backup.ab" in HxD (or other hex editor)
    • Searched for "data.ext4.win"
    • Looked for "TWRP....twfilename" shortly before the occurence of "data.ext4.win"
    • Placed cursor directly before the occurence of "TWRP....twfilename"
    • Shift-clicked (selected) the location directly before the first occurence of "TWRP....filename" in "backup.ab" (near "system.ext4.win")
    • Deleted all selected data
    • Saved file as "backup2.ab"
    • Used twrp2tar (linked above) on "backup2.ab" to get "backup2.tar" -- this is the data partition

    Hopefully these shoddy, hastily written instructions can help someone else in the future.

  10. Jindey commented on Mar 2, 2021

    @Jindey

    I have a TWRP backup of my data partition @unilock what should I select from the begin to the first system.ext4.win??

  11. ChrizZz90 commented on Oct 5, 2021

    @ChrizZz90

    Is there a new way to extract the data? I tried @unilock guide and also @AlD python script but without success. I have a backup.ab file with several partitions. In both ways I get a tar file but inside the tar file is just the backup.ab and I can't unpack it due to a checksum error.

  12. unilock commented on Oct 7, 2021

    @unilock

    @Jindey @ChrizZz90 I made a short video showing my process in more detail. You'll need to download Python 2 and twrp2tar, then open your backup.ab file in a hex editor, e.g. HxD.

    2021-10-07.11-19-34.final.mp4
  13. ChrizZz90 commented on Oct 8, 2021

    @ChrizZz90

    @unilock thanks for the video, I really made a mistake and selected the wrong block. But I may have strange backups, but I have "TWRP....twfilename" only once in my *.ab files. In my understanding, I wouldn't remove anything in this case? But this will lead to tar issues and the Python script runs in a never-ending loop. I have 2 backups, one has only the data partition, the other one system, boot, recovery, vendor and data. During the conversion to a tar file, I don't have the case of "TWRP\x00\x00\x00\x00".

  14. unilock commented on Oct 11, 2021

    @unilock

    @ChrizZz90 If you have a backup containing only the data partition, I believe you should only need to run twrp2tar, without deleting anything in the backup. But it sounds like you already tried that, so I'm not sure where to go from there.

  15. ChrizZz90 commented on Oct 11, 2021

    @ChrizZz90

    indeed, I tried that already. Same today, just with python3 and some minor changes that print is working with python3. Converting my 30gb backup takes 20min but the script is in an endless loop. I need to stop the script manually but the tar file is corrupt after that. I just have a lib folder and the AndroidManifest.xml in the tar file.

  16. sandros87 commented on Jan 24, 2022

    @sandros87

    I have TWRP adb backup with all partitions but I only have two TWRP....twfilename for vendor and data however when I restore this update it also contains boot efs and (I hope) system. Why do you think is that? I would like to extract system or at least make sure it's actually there!

  17. bigbiff commented on Jan 25, 2022

    @bigbiff
    Member

    The intent of adb backup, if you want to restore individual partitions, you should backup only that partition over adb backup and name the file appropriately.

  18. unilock commented on Feb 11, 2022

    @unilock

    @sandros87 I'm not familiar with the way TWRP creates backups, but perhaps boot (ramdisk?) and efs ("Encrypted File System" - stores unique device identifiers) are stored differently than the ext4 partitions? Not sure about system though, that should be stored as ext4... maybe your backup doesn't include it?

    In any case, you could do a text search for "system" in your TWRP backup file, or try restoring the backup on a spare phone with TWRP.

  19. jerrykan commented on Mar 9, 2022

    @jerrykan

    I wrote a short python script to extract files/image from a mutli partition TWRP backup file. It isn't especially robust, so there are probably backup files that it will fail on, but it might be a good starting point for someone else who comes across this thread looking for help.

    https://gist.github.com/jerrykan/b3b2f7f43b5e1d4d5b5198748be16bb2

    The extracted files were just TAR files.

  20. Abrolkirat commented on Feb 17, 2025

    @Abrolkirat

    @unilock thanks for the video, I really made a mistake and selected the wrong block. But I may have strange backups, but I have "TWRP....twfilename" only once in my *.ab files. In my understanding, I wouldn't remove anything in this case? But this will lead to tar issues and the Python script runs in a never-ending loop. I have 2 backups, one has only the data partition, the other one system, boot, recovery, vendor and data. During the conversion to a tar file, I don't have the case of "TWRP\x00\x00\x00\x00".

    Please use this code:

    Follow the video of @unilock (thank you), and then based on code of @jerrykan

    use this python

    with open('backup.ab', 'rb') as inf:
        # Open the output .tar file for writing in binary mode
        with open('backup.tar', 'wb') as out:
            # Read the first 512 bytes (header)
            buf = inf.read(512)
    
            # Continue reading the .ab file and write to .tar
            while buf:
                # If we detect the TWRP header, skip this chunk
                if buf[:8] == b'TWRP\x00\x00\x00\x00':
                    print("Skipping header: %r" % buf[:16])
                    buf = inf.read(512)
                    continue
    
                # Write the remaining data to the output file
                out.write(buf)
    
                # Read the next 512-byte chunk
                buf = inf.read(512)
    
    print("Conversion from .ab to .tar is complete.")
    
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions