ksynwa.xyz (◕ᴗ◕✿)

Syncing Between Android and Linux with rsync

Jun 14, 2019 // posted on

Introduction

This is a guide to setting up rsync on Android and Linux. I use this to sync music and it has served me well.

Primer

  1. Install Termux on Android.
  2. Install openssh and rsync on both devices. Praise be to package managers.
  3. On Termux, run passwd to have a password.
  4. On Termux, run termux-setup-storage to grant Termux access to your phone's storage.

SSH

The device being ssh'd into needs to have the daemon running. I prefer to run it on my phone.

On Termux, sshd defaults to port 8022. The network addresses can be identified via ip a.

Extensive documentation on ssh for Termux is available on the wiki page.

Generating ssh Keys

To generate ssh keys on either device, run the following command:

ssh-keygen [-t rsa] [-b 4096]

Default location for ssh stuff is ~/.ssh/.

  1. id_rsa is the private key. Never share this with anyone.
  2. id_rsa.pub is your public key.
  3. authorized_keys is a list of public keys are the allowed to ssh into the device.
  4. known_hosts is a list of known hosts that have been previously ssh'd into.

Here is a tidy high-level explanation to public-private asymmetrical key pairs.

Copying the ssh Public Key

This makes life ridiculously convenient.

To copy a device's public key to a target:

  1. Make sure target has the ssh daemon running.
  2. Run this command: ssh-copy-id [-p <port>] <target_address>

I wasn't able to copy my public key from my computer to my phone without having run passwd on my phone.

This can also be done manually. The goal is to append destination's ~/.ssh/id_rsa.pub to target's ~/.ssh/authorized_keys as a single line.

Testing the Waters

Try sshing into a device like so:

$ ssh [-p 8022] <target_address>

Ayy we did it! (Hopefully.)

Meet rsync

rsync - a fast, versatile, remote (and local) file-copying tool

It is a great tool. I love it.

The command that I run on my computer looks like this:

$ rsync --recursive --verbose --progress --ignore-existing\
      [--delete]\
      -e 'ssh -p 8022'\
      ~/Music/synced_music/\
      <phone_address>:/sdcard/Music/synced_music
  1. --delete deletes files that I have deleted on my computer.
  2. The target folder ends with /. This is intended. Different behaviours are exhibited with and without it:
    • With / suffixed, rsync makes sure destination and target are synced.
    • Without /, rsync copies destination into target.

From here on, everything can be configured and automated to any extent. For example, I have a function called musicsync in my .zshrc (RAW).

Issues

This whole thing is a hack with a downside. sshd is notoriously slow over local WiFi connections. See HERE and HERE. This isn't too big a deal when the transfer size is small but, otherwise, it can be frustrating.

Github user Neo-oli has suggested workarounds HERE . I'll copy and paste them here for convenience.

ssh + adb

Run adb forward tcp:8022 tcp:8022 to forward the phone's port to localhost's port.

Prevent WiFi from Sleeping

ssh someserver -t -- "while true;do sleep 0.1; date;done";

Alternatives

If ssh and rsync do not float your rafter (cries), other options are available:

  1. adb, packaged with android-tools, on most distros, has sync, pull and push options and can be used over the network. (I think.)
  2. android-file-transfer is available on most distros but is rudimentary.
  3. mtpfs allows mounting MTP devices as file systems.

EDIT: Much better ha!