1

If I use

adb push mydir/ /sdcard/Documents/

the result is as expected: all files from within mydir/ end up inside /sdcard/Documents/ (just the permissions are not kept, even not when using -a; on the internal sdcard everything is turned into 0660 while on the external sdcard everything is set to 0771 – which might be a limitation of sdcardfs as not even chmod 0764 on the device has any effect). But the other direction is weird: using

adb pull /sdcard/Documents/ mydir/

results in the files showing up inside mydir/Documents/, which is rather very unexpected. I've played with all combinations of trailing slashes (including without any trailing slashes), no dice. Even tried different versions of adb (thinking the one used might have a bug). Same happens when using the external sdcard, so it's not the symlink (/sdcard -> /storage/self/primary) fooling it.

So any clue what could be wrong here? When I search on the net, all findings suggest it should work as I expect, but it somehow does not. How would I need to adjust above adb pull command so all files from inside /sdcard/Documents/ show up locally inside mydir/, but not the Documents/ directory itself as subdir? And: How to keep permissions as they are?

4
  • adb for some reason doesn't accept wildcards, though they should be processed from shell. nice find for the dot trick ;) stackoverflow.com/q/23242004
    – alecxs
    Commented Jan 20, 2023 at 17:49
  • @alecxs credits for the dot-findings go to Nicolas (see the link). And as for permissions: for the example given I agree the FS is the cause. It's nasty though, and one must be aware of it. If you use a spare old device for backups that way and rely on the permissions to stay correct, this could be quite an issue. I've not tested how it would work with ADB running in root mode and the target being e.g. inside /data (say, /data/data/mybackups).
    – Izzy
    Commented Jan 20, 2023 at 22:55
  • you could use tar adb shell "tar -ch sdcard | gzip" > sdcard_backup.tar.gz
    – alecxs
    Commented Jan 21, 2023 at 10:40
  • 1
    @alecxs Nope, I should not. That might be a work-around for copying, but doesn't work with rsync ;)
    – Izzy
    Commented Jan 22, 2023 at 14:09

1 Answer 1

2

Took me almost a full day to find a partly solution rather by accident. While adding a wildcard (adb pull /sdcard/Documents/*) just leads to an error, adding a dot leads to the intended result:

adb pull /sdcard/Documents/. mydir/

has the files (and sub-directories) from within /sdcard/Documents showing up in mydir/, but not the Documents directory itself (found here when writing my question and checking suggested "similar questions").

Also helpful in this context: adb push supports a --sync option (to only transfer newer and updated files) – and while not documented, adb pull supports it as well¹ (but needs -a in addition to keep timestamps intact). So the rather complicated work-arounds or external tools of the past are no longer needed.

adb push --sync local/ device/
# adb pull --sync -a device/. local/

No --delete feature, though (as e.g. supported by rsync – if someone needs such, maybe give 3rd-party-tools like better-adb-sync a try).

As to permissions, it seems keeping them is not possible. I gladly stand corrected on that point if someone has a solution to it.

¹ Update: running a few tests it seems --sync is simply ignored silently with adb pull even if no error is thrown: newer files on target have been overwritten. Not sure if implementation is planned, but with r27 (the first version adb pull no longer complained --sync would be an invalid option) being released in 2018 I'd not place a bet on that. With push it works as advertized, though.

You must log in to answer this question.

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