# Linux recovery file from android device or sd card 💻 📱

This is a quick overview from

roubert android file recovery main source (opens new window)

last one is useful for android internal memory here i'll highlight also for sd card memory recovery 🤘

# Prerequirement

apt-get install testdisk pv extundelete

# Instructions

  1. Activate USB Debug on your device (opens new window) (in order to connect with adb)

  2. Attach your device to the computer via the USB cable

  3. Open a terminal on your computer, go to the Android SDK directory's subdirectory sdk/platform-tools (where adb is located)

  4. Log in to your phone with adb shell:

    ./adb shell
    

    (NB: You might have to implicitly call adb start-server as root first if the command above fails.)

  1. Now we can either use the mount command to find out what partition holds the SD Card data and dump that one, or we just dump out the entire memory content and skip the thinking part. I find the latter preferable, since the thinking part involved in the first approach is always prone to fail. So then you want to search for a block device called something like

    VERY IMPORTANT Unlike roubert recovery method which is useful for internal memory, here i also identify it for sd card.

    by convetion internal memory is

    /dev/block/mmcblk0
    

    sd card is

    /dev/block/mmcblk1
    
    shell@android:/ # ls /dev/block
    dm-0
    dm-1
    dm-2
    loop0
    loop1
    loop2
    loop3
    loop4
    loop5
    loop6
    loop7
    mmcblk0
    mmcblk0boot0
    mmcblk0boot1
    mmcblk0p1
    mmcblk0p10
    mmcblk0p11
    mmcblk0p12
    mmcblk0p13
    mmcblk0p14
    mmcblk0p2
    mmcblk0p3
    mmcblk0p4
    mmcblk0p5
    mmcblk0p6
    mmcblk0p7
    mmcblk0p8
    mmcblk0p9
    ram0
    ram1
    ram10
    ram11
    ram12
    ram13
    ram14
    ram15
    ram2
    ram3
    ram4
    ram5
    ram6
    ram7
    ram8
    ram9
    vold
    zram0
    shell@android:/ #
    

Here we see that there are several partitions within mmcblk0, but we leave it for TestDisk to take care of the partitions later.

  1. Exit the shell to go back to the Android SDK directory's subdirectory sdk/platform-tools on your computer.

  2. Now let us dump the content of that /dev/block/mmcblk0 that we found to the computer. With adb shell we can become superuser and execute cat to dump the content like this:

 $ ./adb shell su -c "cat /dev/block/mmcblk0" | pv > mmcblk0.raw

Pipe Viwer (pv) is optional, but I like to see the transfer progress information it provides. (And of course you can change mmcblk0.raw to some other directory/filename if you want to.)

Addition: André Paixão wrote to me that he just got an empty file with the command above. He solved it by using adbd insecure (opens new window).

Addition: Daniel Jeliński wrote to me that he ran into issues with LF encoding (opens new window). The solution that worked for him was:

./adb shell su -c "cat /dev/block/mmcblk0" | pv | sed 's/^M$//' > mmcblk0.raw

...where ^M is what you get by pressing Ctrl+V followed by Ctrl+M.

Addition: Marc also ran into the LF problems, but solved it this way:

./adb shell "su -c 'stty raw; cat /dev/block/mmcblk0'" | pv > mmcblk0.raw

Addition: Tim de Waal wrote to me that he prefers using netcat/gzip instead:

On the Android device (adb shell with su), run:

dd if=/dev/block/mmcblk0 | gzip -9 | nc -l 5555

On the computer, run:

nc [AndroidIP] 5555 | pv -b > mmcblk0.img.gz
  1. Now wait until the transfer is done. For a 8 GB device it does not take too long, but still there is time to check some e-mails and browse the web while it finishes.

  2. With the dump on the Linux machine's file system, now we can let TestDisk take it from there:

$ testdisk mmcblk0.raw
  1. First restore the GPT partition table. Select mmcblk0.raw in the TestDisk interface and choose Proceed:

Choose EFI GPT partition map:

image 1

Then select Analyse in order for TestDisk to find the partitions:

image 2

Choose Quick Search:

image 3

Hopefully the search will return a nice table like this:

image 4

Then just press enter to continue.

image 5

Select Write to write this new(ly recovered) partition table. Then confirm with Y:

image 6

You will then be informed that you need to reboot. But in our case there is no need for that. Just press enter to continue:

image 7

Now the partition table is restored, and we can go on and try to recover the files.

image 8

Select Advanced to use TestDisk's Filesystem Utils section:

image 9

Select the partition where you want to do the recovery. In this example, we want to recover photos from the FAT32 SD Card partition with the Undelete option:

image 10

Now we can browse the filesystem for files that can be recovered:

image 11

In the /DCIM/100ANDRO directory, where my Android device stores its camera images, we find a lot of files that can be recovered. Select all or some of them, and use either C or c to recover them to one of the Linux computer's file systems (e.g. your home directory):

image 12

Done!

Optional: To recover files from an ext3/ext4 partition, TestDisk has an option Image Creation that will write an image called image.dd of that partition to a file on the Linux computer's file system:

image 13

Then you can use extundelete in your terminal window to recover files from the image:

extundelete --restore-all image.dd

Thank you very much Mathias Brodala, Piotr Biesiada, André Paixão, Marc, Daniel Jeliński and Tim de Waal for sharing your valuable update suggestions and corrections.

Last Updated: 4/29/2019, 3:03:44 AM