80

How can I connect to my android device to rsync music (or other stuff)?

guettli
  • 1,765

13 Answers13

82

Actually using rsync over MTP/usb

It's easier than everyone is saying, first notice that when GVFS mounts the MTP mount it'll be available under. You can force this by opening the phone up in a graphical file-browser (thunar/nautilus/etc)

/run/user/$UID/gvfs

go in there. Assuming you have one mtp device, this should work:

$ cd /run/user/$UID/gvfs/mtp*

Find where you want to transfer the files too, and then rsync them to it

$ cd SanDisk\ SD\ card/Movies/

$ pwd # prints "/run/user/1000/gvfs/mtp:host=%5Busb%3A003%2C096%5D/SanDisk SD card/Movies"

$ rsync --verbose --progress --omit-dir-times --no-perms --recursive --inplace ~/Videos/ ./

Rsync options

  • --inplace: I highly suggest using --inplace without which mtp may want to copy the file a new, and then rename it to the old one. That may result in copying the file to the SD card twice: once for the mtp transfer to the SD card, and another time because the MTP driver may not support (mv), it may (cp/rm) under the hood to be safe.
  • read man rsync for a description of --verbose, --progress, --recursive but they're pretty self-documenting.
  • --omit-dir-times --no-perms are required because mtp doesn't support that.
Evan Carroll
  • 7,703
45

Using sshelper

I found this solution:

  • Install sshelper on the device (no rooted device needed, available from google play market)
  • In my WLAN the device is called "android". But you can use the IP, if you can't give the device a hostname.

Edit local ssh-config, to alter the default port for host "android"

.ssh/config
host android
    Port 2222
  • Start sshelper on device.
  • Connect android device to you WLAN.

rsync -rvlc Music android:SDCardLink/

Update I prefere -rvlc to -a since you get a lot of warnings since setting permissions and time-stamps does not work. The option -c makes the second sync much faster.

I prefere -rvl --size-only to -a since you get a lot of warnings since setting permissions and time-stamps does not work. The option --size-only makes the second sync much faster.

Unfortunately it needs some time for music apps to see the new files. Restarting the device helps.

guettli
  • 1,765
5

None of the above did quite what I was looking for. Specifically part of the OP's question: how do I rsync my music to my Android phone? Well, I took parts of the suggestions from these answers and wrote my own Bash script:

#!/bin/bash

echo "Starting rsync with Android."

src_dir="/home/joshua/Music"
dst_dir="/run/user/$UID/gvfs/mtp:host=OnePlus_HD1925_SERIAL/Internal shared storage"
rsync --progress \
      --human-readable \
      --omit-dir-times \
      --no-perms \
      --recursive \
      --inplace \
      --size-only \
      --exclude='*.jpg' \
      --exclude='*.JPG' \
      --exclude='*.jpeg' \
      --exclude='*.JPEG' \
      --exclude='*.png' \
      --exclude='*.bmp' \
      --exclude='*.txt' \
      --exclude='*.pdf' \
      --delete \
      "$src_dir" "$dst_dir"

echo "rsync operation is done. Enjoy the music :)"

I just dropped this in ~/.local/bin where I've got other scripts (your system's default PATH should actually already have this directory). You just have to make sure that File Transfer is on and that the storage is actually mounted before you run it. Nothing bad will happen if you don't: rsync will just yell at you.

This script:

  • Takes into account that Androids can't retain ext4 style metadata on files
  • really is faster than the other options in my experience, using size-only
  • Excludes album covers and scans from flooding your phone's file system :)
  • Really syncs! It deletes stuff when you delete from the source, like fixing song metadata and replacing it with the correct file instead of keeping two copies.

Also, if you choose this answer specifically because you like the --exclude tags, you might also need a way to do clean-up on your phone if it already synced a whole bunch of unwanted images:

# Remove all *.jpg and *.JPG from the current directory, recursively.
find . -iname *.jpg -exec rm {} \;
4

Using Termux

Termux brings a whole lot of Linux command line utilities to Android, including rsync and openssh.

You can set up an SSH server on your Android device, then use rsync over SSH, provided both your PC and your Android device are on the same WLAN connection.

The Termux Wiki clearly explains the steps required to set up SSH and use rsync.

To sum up:

  1. Set up Termux

    • Install Termux
    • Grant Termux filesystem access (Storage permission) from Android settings
    • Open Termux
    • Set up a password
      $ passwd
      New password: 
      Retype new password: 
      New password was successfully set.
      
    • Update the list of packages
      $ apt update
      
    • Optionally, upgrade the existing packages as well (this solved a compatibility issue in my case)
      $ apt upgrade
      
  2. Set up openssh

    • Install openssh
      $ apt install openssh
      
    • Start the ssh daemon
      $ sshd
      
      It listens on port 8022 by default.
  3. Install rsync

    $ apt install rsync
    


To get started, all you need is:

  • Your username on Termux
    $ whoami
    u0_a223
    
  • The IP address of the Android device
    $ ip -4 a | grep wlan0 | tail -1
        inet 192.168.1.101/16 brd 192.168.255.255 scope global wlan0
    
    which, in this case, is 192.168.1.101.


Now, you can do pretty much anything with rsync. For example, if you wish to copy the ~/Music directory from your PC to the Android device's /sdcard, you could do something like:

$ rsync -e 'ssh -p 8022' -aP ~/Music u0_a223@192.168.1.101:/sdcard

When prompted for a password, enter the one you set earlier.

4

rsync backup for Android can be configured to sync in reverse, that is from a Linux host to Android device.

And with the help from LlamaLab Automate one can configure the Android device to rsync automatically on specific conditions, e.g.:

  1. at 5 am,
  2. when the phone is connected to a specific Wi-Fi, and
  3. when it is charging.
sondra.kinsey
  • 322
  • 3
  • 19
3

I was looking for a solution to rsync FROM android to NAS for images backup.

Grsync, graphical version of rsync, worked just fine.

Fortunately, it also can rsync TO android. The only problem -- it can not preserve time stamps so all transferred files will have time stamps of the moment they were rsynced.

    sudo apt install android-file-transfer
    sudo apt-get install grsync

It seems that -a key I was using in the command line to rsync from android does not work with mtp: when I check "preserve permissions" and "preserve owner" and "preserve group" in grsync no file is transferred. Default grsync settings "preserve time", "Verbose" and "Show transfer progress" just works.

While rsyncing to android -t key should be ommited: files will be transferred but rsync will give errors on not preserving time stamps.

Grsync output for command line FROM android:

    rsync -r -t -v --progress -s /run/user/1000/gvfs/mtp\:host\=%5Busb%3A001%2C013%5D/Micro\ SD/test /home/FREENAS/

Grsync output for command line TO android:

   rsync -r -v --progress -s /home/FREENAS/test /run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C013%5D/Micro\ SD/

where run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C013%5D my android phone mount point.

And78ru
  • 31
2

I know that the OP wanted to use rsync, but if the idea is to sync directories, I strongly recommend using syncthing. Many, many, benefits:

  • it will run continuously, no need to fire it from time to time;
  • it will sync many different folders, to many different devices, at the same time;
  • you can use syncthing as part of a backup solution for your cellphone, in two steps: (1) you sync all important data from your cellphone to an external host (2) you setup a backup solution on that host.

I personally sync all important data from any device I own to at least another device, possibly two, located in physically different places; at least one of these devices has raid disks; I then add snapper to one device, so that old versions of all files are kept; or you can use a feature of syncthing to accomplish the same results.

One last note of warning. If you run out of space on one device, that device will stop syncthing, but you will not get any error from it, so you should check from time to time. If you use snapper it is quite easy to fill up disks, caveat emptor.

am70
  • 143
2

The foldersync app can use the SFTP protocol and is very configurable. Works like a charm.

1

I tried rsync over MTP, but in my case, the speed was really slow. If you want to try it, here is a suggestion. My solution is based on the following source:

https://bbs.archlinux.org/viewtopic.php?id=261904

I use Kubuntu, so, there is no MTP folder in the /run/user/1000/gvfs/ folder. I downloaded the following package:

https://li.nux.ro/download/nux/dextop/el7/x86_64/simple-mtpfs-0.2-3.el7.nux.x86_64.rpm

There is the binary file that worked on Kubuntu 20.04. You can find it in the bin folder and unpack it into a folder. Unfortunately, on Plasma, it may not mount the device until we kill a specific process. Then you can use the fuser command.

First, you need to find your device with the lsusb command:

$ lsusb
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 005: ID 8087:0a2a Intel Corp. Bluetooth wireless interface
Bus 001 Device 011: ID 2717:ff48 Xiaomi Inc. Mi/Redmi series (MTP + ADB)
Bus 001 Device 003: ID 04f2:b3fd Chicony Electronics Co., Ltd HD WebCam (Asus N-series)
Bus 001 Device 006: ID 04fc:05d8 Sunplus Technology Co., Ltd Wireless keyboard/mouse
Bus 001 Device 004: ID 1bcf:08a0 Sunplus Innovation Technology Inc. Gaming mouse [Philips SPK9304]
Bus 001 Device 002: ID 05e3:0608 Genesys Logic, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Here we need to copy the 2717:ff48 MAC address of the Xiaomi device. Run the following lines and replace 2717:ff48 with your device's MAC address:

DATA=`lsusb | grep '2717:ff48'`
echo $DATA
# Reading the bus number:
BUS=`echo ${DATA:4:3}`
# Reading the device number:
DEV=`echo ${DATA:15:3}`
# Killing the process which is using the device (if any)
fuser -k /dev/bus/usb/$BUS/$DEV

Then you can run the binary file and specify your path for the mounted folder:

./simple-mtpfs --device 1 /tmp/xiaomi

However, as the speed was really slow, I just gave up, used a card reader, and synchronized the files from a local folder to the USB device. Maybe, that was the easiest option. In my case, this is the best option because the destination folder has some files to be deleted, and to extend the life of an SD card, it is better to not delete or create new files when it is not necessary.

gearcoded
  • 231
1

My Two Cents:

termux only works for integrated storage and a certain directory on external cards in recent devices (mine's running MIUI14) or with rooted devices.

just opening the mtp device in any filemanager to open a terminal from there did not work for me (thunar)

the /run/... directory for gvfs was empty for me despite the device being unlocked and open in the filemanager

I solved this for me by unmounting the mtp device (using the filemanager) and remounting it (using jmtpfs)

1

I'd suggest to avoid using rsync via mtp (media transfer protocol) and use adb - android debug bridge if possible.

First install the cli tool, download form here
(in my case I just wanted the cli tool since I'm not an android dev)

Here are the direct steps, in my case I'm on linux:

$ curl -sSL https://dl.google.com/android/repository/platform-tools-latest-linux.zip-linux.zip -o platform-tools.linux.zip
$ unzip platform-tools.linux.zip
$ cd platform-tools/
$ ./adb --version
Android Debug Bridge version 1.0.41
Version 34.0.5-10900879
Installed as /path/to/adb
Running on Linux 5.18.10-76051810-generic (x86_64)

Now connect your phone via USB and activate USB debugging on developer options in android settings

Check your phone is connected

$ ./adb devices -l
List of devices attached
....REDACTED....         device usb:1-1 product:...REDACTED... model:...REDACTED... device:...REDACTED... transport_id:1

Now you can copy local content to the phone via push command

Here is the info about the command

 push [--sync] [-z ALGORITHM] [-Z] LOCAL... REMOTE
     copy local files/directories to device
     --sync: only push files that are newer on the host than the device
     -n: dry run: push files to device without storing to the filesystem
     -z: enable compression with a specified algorithm (any/none/brotli/lz4/zstd)
     -Z: disable compression

Here is an example of the command call

$ ./adb push /path/from/your/local/ /path/on/your/phone

In my case an example of /path/on/your/phone/.../ looks something like this /storage/emulated/0/.../

I got the info from this reddit comment and this part from the android docs.

manus
  • 113
0

You can use the rsync server app on Android available on F-Droid. See: https://github.com/ktsr42/RsyncServerApp

0

Rsync and MTP only, no errors (so far)

This minimal combination of options did not give me errors on rsync via MTP. Test using only these options first before adding your other preferences.

  • --update, -u skip files that are newer on the receiver
  • --recursive, -r recurse into directories
  • --omit-dir-times, -O omit directories from --times
  • --inplace update destination files in-place

Command examples:

  • Short: rsync -urO --inplace "${src}" "${dst}"
  • Long: rsync --update --recursive --omit-dir-times --inplace "${src}" "${dst}"

Where:

  • ${src} is the source folder, e.g. "${HOME}/Music"
  • ${dst} is the destination gvfs mount point, e.g. '/run/user/1000/gvfs/mtp:host=Brand_Model_hash/Internal shared storage'
Majal
  • 8,249