[HOWTO] Backup and Restore Data on Android Devices

After some tests on 2 devices (Lenovo YogaTab 3 and Xiaomi Redmi 4X), I come back to propose some adjustments. I could restore from a corrupted ROM image using this method.

I’ll be very grateful if somebody with a testing device or similar experience could confirm, so we may evolve the HowTo :slight_smile:

1/ Restore

  • Unless necessary, only restore the specified partitions, even if more are included in backup. I tried to restore others (EFS, persistent, recovery, images), every time my device rebooted abruptly in fastboot and eventually I had to reinstall TWRP.
  • It is safer to restore /data/media before first system reboot (see below for backup). If you used a tgz archive, something like cd / then tar -xzvf /path/to/backup/storage/data_media.tgz. Also don’t forget to check owners and use chown -R user:user * if necessary.
  • Before first reboot to system, you may want to remove /data/system/locksettings.db (or similar). If your device was protected by pattern/pin/password, you may not be able to unlock while rebooted in system.
  • Proposed sequence :
    • reboot to TWRP with backup files available
    • format data
    • restore boot/system/data
    • restore data/media from hand-made archive
    • delete /data/system/locksettings.db (or similar)
    • reboot system (Android may encrypt at first boot)
    • set lock (pattern/pin/password) again, and optionally activate Secure Boot
    • reboot again so system

2/ Backup

  • As a matter of precaution, I strongly suggest to remove any lock in Security before backup (don’t forget to reboot to system to check). You will lose your fingerprints records.
  • If your phone has secure Boot activated (prompting at boot with black background), it may persist even after removing lock (it happened to me …). You can try the following :
    • set lock to pattern, activate Secure Boot, reboot system
    • set lock to swipe, reboot system
    • set lock to none, reboot system
  • Boot to system or TWRP should not require any code/pattern/password, Files app should show your files.
  • As suggested in previous posts, uninstall Magisk and Shelter.
  • I also advise to make a gzipped tar backup of /data/media while in TWRP :
    • do the TWRP backup, do not reboot
    • from an adb-enabled computer, launch an adb shell (as always, adb devices first, if no device shown you may have to play with MTP in TWRP Mount)
    • Alternatively, you can use the TWRP Terminal.
    • identify a target storage (for example, you can use the mount command to find path to your SDCard)
    • launch cd / then tar -czvf /path/to/backup/storage/data_media.tgz . (note : some TWRP may have zip, bzip2, GNU tar, … I’ll stick on ToyBox/BusyBox tar as it seems to be widely available)
    • also record owners using ls -l /data/media > /path/to/backup/storage/data_media_owners.txt
  • On my Lenovo YT3, TWRP backup to SDCard never succeeded (stuck somewhere in middle of nowhere). I could use an USB OTG, but it was much slower as internal storage.
  • In any case, take the time to copy your backups to another storage (PC). You may have to move files to /data/media/... if using internal storage, before rebooting to system.
  • If enough space is available, feel free to backup more partitions. But be aware that only some can be restored without using any “magic”.
  • Have the TWRP image file on hand, just in case …
1 Like