8

Currently, TWRP backups don't include /data/media, and this doesn't appear to be on the developers' radar at all (the issue for it over at GitHub is just a bunch of +1s with no official response). Now that we have SELinux to contend with, simply copying the files back and forth is apparently no longer an option, and there are a number of different ways to access /data/media throughout the filesystem, each with different effective owners and permissions.

In excruciating detail, how should I approach backing up and restoring /data/media so everything is restored exactly as it was and my apps don't have trouble with the resulting permissions?

11
  • 4
    Isn't /data/media just an alias for /storage/emulated/0? Permissions and ownership are always restored with Android .tar archives. Mar 18, 2016 at 7:25
  • It's not possible to dd /data/media since it's recognized as a directory. I've not tried, but I think that you can dd /dev/block/platform/msm_sdcc.1/by-name/userdata, instead.
    – Grimoire
    Mar 18, 2016 at 14:19
  • @DeathMaskSalesman dd is surely a bad idea. It's even worse than TWRP & CWM's default backup method, tar. In case you're very wealthy and bought a 128GB phone and you only stored a few files in it, dd will still backup the whole partition, block by block, resulting in an image of 128GB in size. Also another serious problem is that you can't dd directly onto your computer, so you have to find another 128GB storage on your phone (SD card?) to hold the inage temporarily.
    – iBug
    Jan 22, 2017 at 0:48
  • @iBug Of course it's a bad idea. If it were good, I would've posted an answer. By the way, you can dd directly to the computer by means of adb shell, so that the final command becomes adb shell "dd if=partition" > out.img.
    – Grimoire
    Jan 22, 2017 at 1:19
  • 1
    @iBug So do I. Let me upvote your answer, by the way.
    – Grimoire
    Jan 22, 2017 at 16:02

2 Answers 2

6

I recommend directly copying files from/to /sdcard.
The sdcard service will handle all the permissions and SELinux contexts for you.
The /storage/emulated/0, /mnt/shell/emulated/0 and /storage/emulated/legacy are all shadow reflexes of /data/media (typically /data/media/0). In the /init.rc file you can find out that the /system/bin/sdcard is a service that controls all of them, so you can just copy the files in /storage/emulated/0 with ease. All other directories like /sdcard and /storage/sdcard0 are symbolic links. They don't matter at all.

1
  • But what if I want an 1:1 image of that partition?
    – neverMind9
    Apr 7, 2019 at 12:39
0

I just adb pull /data/media/. media to copy the files to my pc adb directory. Then I do a TWRP backup everything to sdcard. Restore from sdcard backup, reboot recovery, then adb push media/. /data/media. Make certain to wipe sdcard from recovery, not formatted from pc. Works for me! Wrong format gives lots of error 255's.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .