Today I had to update the music collection that I keep on my phone. But no matter what, I always have issues with the MTP protocol - it never seems to work properly. I used a wide range of phones with a wide range of devices - PC, laptops, Macs - and each time something stops me from copying my files easily.

So today I plugged my phone into my PC, but nothing happened. I plugged it again, this time it got detected (but only after I told my phone to go into charge-only mode, and then switch back to file-transfer mode), but the connection lasted a couple of minutes and then stopped working.

During debugging, I decided to check whether my PC even has a connection to the phone using adb devices. It turned out it had. Then it struck me - I could totally use the shell that my phone has to achieve what I want! I used adb shell to check if rsync was there:

$ which rsync
/system/xbin/rsync

I tried starting up the daemon mode, but it turned out there is no configuration

$ rsync --daemon
Failed to parse config file: /etc/rsyncd.conf

so I created a file in my /sdcard directory

# rsyncd.conf
address = 127.0.0.1
port = 2137
[root]
path = /sdcard
use chroot = false
read only = false

I put the config in there, since I didn’t want the above as my default config for rsync on my whole phone. I also didn’t want to accidentally destroy my system, so I specified a path in the config. Then I ran rsync like so1:

rsync --daemon --config=/sdcard/rsyncd.conf --no-detach

I thought --no-detach is a good choice since I didn’t want to leave it there running after I ^C the process. The last preparation step is port forwarding:

adb forward tcp:2137 tcp:2137

And I was able to copy my files

rsync -av --progress --stats /mnt/data-ssd/MUZYKA/ rsync://localhost:1873/root/MUZYKA

😀


  1. If you want some logs, you can also add --log-file=/proc/self/fd/0 to see what happens.↩︎

Tags: english technical shell bash android adb

Posted on: 2021-07-21, last edited on: 2021-08-08, written by: Kavelach