Skip to content

Instantly share code, notes, and snippets.

@fxthomas
Created July 3, 2019 22:31
Show Gist options
  • Save fxthomas/af51b48c28d1a4cec8391812c475a91f to your computer and use it in GitHub Desktop.
Save fxthomas/af51b48c28d1a4cec8391812c475a91f to your computer and use it in GitHub Desktop.
Android <> local directory synchronization script using rsync
#!/bin/bash
# Usage: adbsync <remote path> <local path>
# <remote path> defaults to /sdcard/DCIM
# <local path> defaults to .
REMOTE_PATH=${1:-/sdcard/DCIM}
LOCAL_PATH=${2:-.}
REMOTE_PORT=1873
LOCAL_PORT=6010
echo "Checking adb device status..."
if ! adb get-state 1>/dev/null; then
exit 1
fi
echo "Writing remote rsync configuration..."
cat <<EOF | adb shell 'cat > /sdcard/rsyncd.conf'
address = 127.0.0.1
port = $REMOTE_PORT
[root]
path = /
use chroot = false
read only = true
list = yes
EOF
echo "Forwarding local port $LOCAL_PORT to remote port $REMOTE_PORT..."
adb forward tcp:$LOCAL_PORT tcp:$REMOTE_PORT
echo "Starting rsync daemon on remote port $REMOTE_PORT..."
adb shell -tt 'rsync --daemon --no-detach --config=/sdcard/rsyncd.conf --log-file=/proc/self/fd/2' &
echo "Waiting for rsync daemon..."
sleep 3s
echo "Starting transfer from '$REMOTE_PATH' to '$LOCAL_PATH'..."
rsync -a --progress --exclude .thumbnails "rsync://localhost:$LOCAL_PORT/root/$REMOTE_PATH" "$LOCAL_PATH"
echo "Terminating rsync daemon (local pid: $!)..."
kill $!
echo "Removing port forward on port $LOCAL_PORT..."
adb forward --remove tcp:$LOCAL_PORT
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment