DEV Community

Cover image for Sync music between your PC and mobile using Rsync
Lovelin
Lovelin

Posted on • Edited on

Sync music between your PC and mobile using Rsync

In the previous post, we saw how to configure a SSH connection between our PC and mobile. Go check that out, if you landed here without checking that that.

In this post, we will see how we can configure rsync to sync our music files between our devices, and finally listen to some music 🎶🎶

What is Rsync??

Rsync is a command-line tool that enables syncing files and directories between the local and remote system. It's highly flexible and it's similar to scp, if you worked on scp before, the rsync is just scp on steroids.

Rsync needs a transporting mechanism to sync files between two systems, and that's why we had SSH configured between our devices on the previous post.

Installing Rsync

Rsync comes pre-installed on most Linux distributions and macOS. For Windows, you can use WSL (Windows Subsystem for Linux) or install Cygwin.

For Android, open Termux and run

pkg install rsync
Enter fullscreen mode Exit fullscreen mode

Verify the installation with:

rsync --version
Enter fullscreen mode Exit fullscreen mode

Create a common directory

We are going to setup a common directory which will hold all our music files, subtitles etc.. on our device.

Create a Music directory in the home folder on both devices. If it doesn't exist, create it with the following commands:

On PC

cd ~ && mkdir Music
Enter fullscreen mode Exit fullscreen mode

On Mobile (on Termux):

cd storage 
mkdir Music
Enter fullscreen mode Exit fullscreen mode

Now, any new MP3 files added to the Music directory on one device can be synced to the other device seamlessly

Let's Rsync!

Let's have a look at the files we are syncing

I have these on my PC
List of files on Music directory

And my Music folder on my mobile is empty.

On PC

rsync -avP -e 'ssh -p 8022' ~/Music/ <Mobile_username>@<Mobile_IPaddress>:storage/Music/
Enter fullscreen mode Exit fullscreen mode

Let's break that down!

  • a - Enables archive mode. It is necessary for syncing directories recursively.

  • v - Enables verbose mode. Does some helpful logging if something goes wrong or right.

  • P - It enables both the progress and partial flag. This progress flag provides a progress bar for the transfers, and the partial flag allows you to resume interrupted transfers.

  • e - Specifies the SSH port to which we are connecting too. The default port that termux uses is 8022.

We used our mobile username and the IP address which we have acquired through termux. If you are clueless, then please read the previous post.

Notice the trailing slash in the Music directory. The trailing slash signifies the content of the Music folder. If we don't specifiy the trailing slash then it would create a heirarchy in our mobile like Music/Music

You will see the progress of each file incremently and all the files would be transfered to our mobile's Music directory.

If you want to transfer the files from your mobile to your PC,

rsync -avP storage/Music/ <PC_username>@<PC_IPaddress>:Music/
Enter fullscreen mode Exit fullscreen mode

We had our host aliases configured on our previous post. So we can just do

rsync -avP ~/Music/ myGoodPhone:storage/Music/
Enter fullscreen mode Exit fullscreen mode
rsync -avP storage/Music/ myGoodPC:Music/
Enter fullscreen mode Exit fullscreen mode

And that is how you get things done.

Conclusion

Syncing music files over SSH using Rsync might sounds like an overkill, because it is an overkill. Rsync's has a lot of use cases, and it's widely used in automated incremental backups, synchronizing directories between servers etc.. I hope you guys found this series informative

See ya!

Cover image by Namroud Gorguis on Unsplash

Top comments (2)

Collapse
 
mayureshkumbhar profile image
Mayuresh Kumbhar

@lovelindhoni
That's great tutorial! I followed both part step by step (excpet passwordless setup) but I am doing on windows hence for rsync using cygwin terminal and when fired dry run command from Windows to Andriod getting below error.

I made sure on both side SSH running , rsych has same version. What do you suggest



rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
rsync error: error in rsync protocol data stream (code 12) at io.c(231) [Receiver=3.3.0]
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(231) [sender=3.3.0]



Enter fullscreen mode Exit fullscreen mode
Collapse
 
lovelindhoni profile image
Lovelin

Hey @mayureshkumbhar, thanks for checking out this series—it really means a lot to me.

There have been some known issues with rsync’s Cygwin port that are similar to this. I’d recommend using WSL if you can.