Saturday, April 21, 2018

Samsung Android Full Device Backup with TWRP

Warning

Following these instructions, correctly or incorrectly, may leave you with a completely broken or bricked device. Furthermore, flashing your device may void your warranty - Samsung uses eFuses to permanently flag occurrences of a device running non-Samsung software, such as TWRP.
I take no responsibility for what may come of using these instructions.

With the warning out of the way, I will say that I tested this process with the following environment:
  • Android Device: Samsung Galaxy S3 (i9300)
  • TWRP: 3.2.1-0
  • Desktop OS: openSUSE Leap 42.3

Flashing and Booting into Recovery

  • Download the official TWRP image for your device, and corresponding PGP signature
    • https://dl.twrp.me
  • Use gpg to verify your TWRP image
  • Download and install Heimdall on your Linux or Windows PC
  • Boot your Samsung device into Download Mode
    • Simultaneous hold the Volume-down + Home/Bixby + Power buttons
  • Using Heimdall on your desktop, flash the TWRP image to your device's recovery partition:
    • heimdall flash --no-reboot --RECOVERY <recovery.img>
    • Wait for Heimdall to output "RECOVERY upload successful"
  • From Download Mode, boot your Samsung device into TWRP
    • Simultaneous hold the Volume-up + Home/Bixby + Power buttons
    • If you accidentally boot into regular Android, then you'll likely have to boot into Download Mode and reflash, as regular boot restores the recovery partition to its default contents

Exposing the Device as USB Mass Storage

  • Unmount all partitions:
    • From the TWRP main menu, select Mount, then uncheck all partitions
  • Bring up a shell
    • From the TWRP main menu, select Advanced -> Terminal 
    • adb shell could be used instead here, but the adb connection from the desktop to the device will be lost when all USB roles are disabled
  • Determine which block device you wish to backup
  • # cat /etc/fstab
    
    • In my case (i9300), all data is stored on /dev/block/mmcblk0 partitions
  • Check the current state of the TWRP USB gadget
  • # cat /sys/devices/virtual/android_usb/android0/functions
    mtp,adb
    
  • Configure a read-only USB Mass Storage gadget
  • # echo 1 > /sys/devices/virtual/android_usb/android0/f_mass_storage/lun0/ro
    # echo /dev/block/mmcblk0 > /sys/devices/virtual/android_usb/android0/f_mass_storage/lun0/file
    
  • Disable all USB roles
  • # echo 0 > /sys/devices/virtual/android_usb/android0/enable
    
  • Enable the Mass Storage gadget USB role
  • # echo mass_storage,adb > /sys/devices/virtual/android_usb/android0/functions
    # echo 1 > /sys/devices/virtual/android_usb/android0/enable
    
  • If not already done, connect the device to your desktop or laptop
    • The attached device should appear as regular USB storage

Backup

Any Linux, Windows or macOS program capable of fully backing up a USB storage device should be usable from this point. The procedure below uses the dd command on Linux.
  • From your computer, determine which USB storage device to back up
  • ddiss@desktop:~> lsscsi
    ...
    [2:0:0:0]    disk    SAMSUNG  File-Stor Gadget 0001  /dev/sdb 
    
  • As root, start copying the data from the device
  • ddiss@desktop:~> sudo dd if=/dev/sdb of=/home/ddiss/samsung_backup.img bs=1M
    
  • dd will take a long time to complete, depending on the size of your device, USB connection speed, etc.
  • Once completed, unplug your Android device and reboot it
  • The image file can be compressed
With the image now obtained, you could mount it on your desktop, or restore it to the device at a later date. I'll hopefully get around to writing separate posts for both in future.

4 comments:

  1. Unfortunately, not all phones offer access to TWRP ;(

    ReplyDelete
  2. What's the point of using TWRP as a mere placeholder process to UNMOUNT all the OS drives, just to be able to run "dd" on them? TWRP has a single button to take a "backup" of existing system! AM i missing something here?

    ReplyDelete
    Replies
    1. My goal was to backup the entire device, including partition tables, etc. and have the ability to mount it locally. If TWRP's built-in backup suits your needs then by all means use it :-)

      Delete
  3. Thank you. I used so far TeamWin to backup... and wasnt aware of this method. That fits me better. Thank you.

    ReplyDelete

Comments are moderated due to spammer abuse.