79

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

6
  • 2
    I recommend editing this to explain exactly what action produced that error message. (For example, if it's the output of a command, you could include the full command.) Sep 9, 2013 at 20:39
  • 2
    maybe interesting: Syncopoli Aug 1, 2018 at 0:19
  • @DJCrashdummy if you write your comment as answer, I will up-vote it.
    – guettli
    Aug 1, 2018 at 7:26
  • @guettli i wrote this only as comment, because Syncopoli is just a rsync-client (for now). Aug 21, 2018 at 15:54
  • Whatever is the method to run rsync, i recommend to create a hotspot on the machine and connect the Android device to the hotspot. This can drastically increase the synchronization speed.
    – Alexey
    Dec 25, 2020 at 17:44

11 Answers 11

79

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.
14
  • 3
    I add the following options --omit-dir-times --no-perms --ignore-existing. Rsync fails to set directory timestamps over MTP. I'm pretty sure that "no permissions" is the default, but I like to specify it. Ignoring existing files allows it to pick up where it left off. Jul 6, 2016 at 12:21
  • 4
    Why --ignore-existing? That's just losing redundancy. You could always --size-only which reduces the sync to the directory listing and not checksums. I'm going to add the --omit-dir-times and --no-perms because that sounds useful, and I seem to recall getting an error at the end about that. Jul 6, 2016 at 17:41
  • 3
    I also use put the trailing slash on the destination so I don't have to use a star on the source: ~/Videos/ ./ Jul 6, 2016 at 17:46
  • 2
    I'm still getting "rsync: open /run/user/1000/gvfs/... failed: Operation not supported (95)" without sudo or "rsync: ERROR: cannot stat destination /run/user/1000/gvfs/... Permission denied (13)" with sudo. Any tips?
    – Torsten
    Nov 8, 2017 at 20:24
  • 1
    We are in 2023 and it's still a complete foobar. Considering Android is Linux based it is a enourmous travesty towards the decades of Linux development Google scams of off. Windows works seamlessly but Linux is a try and might get lucky or use decades debugging faults. IMO they should have had the courtesy to prioritize the millions of users that have made their system usable in the first place. Apr 4, 2023 at 14:46
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.

13
  • 16
    An alternate way to use rsync with SSHelper without having to modify .ssh/config: rsync -avz -e 'ssh -p 2222' Music android:SDCardLink/ (stackoverflow.com/a/4630407/399105)
    – bmaupin
    Jan 20, 2014 at 4:08
  • 1
    @guettli what for -c checksum option to syncing music files ? this can slow down significantly
    – EdiD
    Feb 14, 2016 at 14:32
  • 5
    @guettli from rsync man : -c, --checksum. Without this option, rsync uses a "quick check" that (by default) checks if each file’s size and time of last modification match between the sender and receiver. This option changes this to compare a 128-bit checksum for each file that has a matching size. Generating the checksum means that both sides will expend a lot of disk I/O reading all the data in the files in the transfer, so this can slow things down significantly.
    – EdiD
    Feb 23, 2016 at 8:21
  • 4
    @guettli I guess that --size-only would be the fastest solution. However it's not so save as -c
    – niels
    Dec 22, 2016 at 19:51
  • 2
    I used USB tethering and got much faster transfers. The following command worked for me after I ran SSHelp and it showing IP 192.168.1.5 which might be different everytime: rsync -avzhP --inplace --info=progress2 -e 'ssh -p 2222' 192.168.1.5:/storage/emulated/0/DCIM/Camera/ /home/username/path/to/pics/ Dec 25, 2016 at 12:03
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

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.
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.

1
  • Why did you install android-file-transfer?
    – jarno
    Oct 5, 2021 at 20:35
3

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 [email protected]:/sdcard

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

2
  • 1
    Thank you. Works for me on android 13 (Galaxy-Note10-5G).
    – spawn
    Aug 27, 2023 at 20:20
  • 1
    Great solution for those that are used to using terminals, you can also setup crond to backup at a set frequency. If the target is also an android, make sure to setup crond at target and run termux-media-scan -r $source_folderto notify apps of new files, for example triggering Google Photos to sync to cloud.
    – pangyuteng
    Oct 9, 2023 at 5:38
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.

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.

1
  • Killing the process using the device part is useful even when using some other mtp-fs program. I'm using Android File Transfer. It works great, but sometimes fails to mount the device as KDE/Gnome grabs the device first. The steps you've mentioned here is useful when that happens. Thanks. Mar 19, 2022 at 16:23
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

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

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.

You must log in to answer this question.

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