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
--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.
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/
UpdateI 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.
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.
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.
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:
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 {} \;
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.
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.
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
Set up openssh
Install openssh
$ apt install openssh
Start the ssh daemon
$ sshd
It listens on port 8022 by default.
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:
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:
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.
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)
go in there. Assuming you have one mtp device, this should work:
Find where you want to transfer the files too, and then rsync them to it
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.--verbose
,--progress
,--recursive
but they're pretty self-documenting.--omit-dir-times
--no-perms
are required because mtp doesn't support that.Using sshelper
I found this solution:
Edit local ssh-config, to alter the default port for host "android"
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.
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.:
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.
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:
Grsync output for command line TO android:
where run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C013%5D my android phone mount point.
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:I just dropped this in
~/.local/bin
where I've got other scripts (your system's defaultPATH
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:
size-only
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: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: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 ofsyncthing
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.Using Termux
Termux brings a whole lot of Linux command line utilities to Android, including
rsync
andopenssh
.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:
Set up Termux
Storage
permission) from Android settingsSet up
openssh
8022
by default.Install
rsync
To get started, all you need 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:When prompted for a password, enter the one you set earlier.
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 thefuser
command.First, you need to find your device with the
lsusb
command:Here we need to copy the
2717:ff48
MAC address of the Xiaomi device. Run the following lines and replace2717:ff48
with your device's MAC address:Then you can run the binary file and specify your path for the mounted folder:
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.