Skip to content

Instantly share code, notes, and snippets.

@ivan-pinatti
Last active September 11, 2020 17:42
Show Gist options
  • Save ivan-pinatti/8aa1b69c0ae83cbac02e261942ffc5e1 to your computer and use it in GitHub Desktop.
Save ivan-pinatti/8aa1b69c0ae83cbac02e261942ffc5e1 to your computer and use it in GitHub Desktop.
Sync a local music library to an Android device through SSH using RSYNC
#!/usr/bin/env bash
: ' Script to sync a music library from local machine to an Android device
through a SSH connection using rsync and checking files using size only
method.
I do recommend SSHelper as a SSH server for Android devices, you can
download and install it from here:
https://play.google.com/store/apps/details?id=com.arachnoid.sshelper
Install it and start the SSH server before running this script.
'
# bash default parameters
set -o errexit # make your script exit when a command fails
set -o pipefail # exit status of the last command that threw a non-zero exit code is returned
set -o nounset # exit when your script tries to use undeclared variables
# default values
DEFAULT_ANDROID_IP_ADDRESS="192.168.1.10"
DEFAULT_ANDROID_SSH_PORT="2222"
DEFAULT_LOCAL_MACHINE_SOURCE_FOLDER="/home/"${USER}"/Music"
DEFAULT_ANDROID_DESTINATION_FOLDER="/sdcard/Music"
# binaries
RSYNC=$(which rsync)
# variables
ANDROID_IP_ADDRESS="${1:-"${DEFAULT_ANDROID_IP_ADDRESS}"}"
ANDROID_SSH_PORT="${2:-"${DEFAULT_ANDROID_SSH_PORT}"}"
SOURCE_FOLDER="${3:-"${DEFAULT_LOCAL_MACHINE_SOURCE_FOLDER}"}"
ANDROID_DESTINATION_FOLDER="${4:-"${DEFAULT_ANDROID_DESTINATION_FOLDER}"}"
# flow
echo "Start syncing songs from "${SOURCE_FOLDER}" to "${ANDROID_DESTINATION_FOLDER}" in device "${ANDROID_IP_ADDRESS}""
"${RSYNC}" --recursive \
--verbose \
--size-only \
--inplace \
--no-whole-file \
--delete-excluded \
--human-readable \
--info=progress2 \
--rsh="ssh -p "${ANDROID_SSH_PORT}"" \
""${SOURCE_FOLDER}"/" \
""${ANDROID_IP_ADDRESS}":"${ANDROID_DESTINATION_FOLDER}""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment