├── README.md ├── rsyncd.conf └── start-rsync-android /README.md: -------------------------------------------------------------------------------- 1 | # rsync-for-android 2 | 3 | Transfer files over USB at the speed of lightning with rsync (needs Termux). 4 | 5 | 6 | 7 | ## Overview 8 | 9 | Transfer files to and from your Android device much faster than over MTP (the default) or even ADB pull/push using rsync. 10 | 11 | ## Why? 12 | 13 | Quite simply put, `libmtp` and `adb` provide terrible performance. Windows' implementation of MTP is better but it still keeps overwriting files unnecessarily and can't detect changes. 14 | 15 | We need a better solution. Luckily there's already `rsync` which gives us delta-compression. Only problem is that it's a client-server program, needing an `rsync` server running on the phone. 16 | 17 | For copying a large number of files like a Music library or Photo gallery, `rsync` provides massive performance gains especially when only a few files have changed. 18 | 19 | ## Requirements 20 | 21 | 1. Termux app installed and initialised (run at least once) 22 | 2. ADB command in $PATH 23 | 3. USB debugging enabled on your phone 24 | 4. An rsync client on your PC ([GRsync](http://www.opbyte.it/grsync/) recommended for GUI) 25 | 26 | ## Usage 27 | 28 | 1. Run `adb devices` to ensure that your phone shows up in the list. Authorize on the phone if necessary. 29 | 30 | 2. Run the script with `./start-rsync-android`. This will perform the one-time-setup (if necessary) and forward the port 8873 on your phone to localhost:8873, enabling you to access your phone's rsync server at localhost:8873. 31 | 32 | ## Example 33 | Backup all your photos to your Backup HDD: 34 | 35 | ```bash 36 | rsync -avPh --info=progress2 rsync://localhost:8873/sdcard/DCIM /media/Backup/Phone/DCIM 37 | ``` 38 | -------------------------------------------------------------------------------- /rsyncd.conf: -------------------------------------------------------------------------------- 1 | address = 127.0.0.1 2 | port = 8873 3 | 4 | [root] 5 | path = / 6 | use chroot = false 7 | read only = false 8 | 9 | [sdcard] 10 | path = /sdcard 11 | use chroot = false 12 | read only = false 13 | -------------------------------------------------------------------------------- /start-rsync-android: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | # Copyright (C) 2020 reisub0 3 | # 4 | # Distributed under terms of the MIT license. 5 | 6 | FIRSTRUNFILE=/var/tmp/.rsync-for-android-first-run-complete 7 | 8 | isFirstRun() { 9 | if [ ! -e "$FIRSTRUNFILE" ] || [ "$1" = 'init' ] ; then 10 | echo "FIRST RUN" 11 | return 0 12 | fi 13 | return 1 14 | } 15 | 16 | isDeviceConnected() { 17 | if [ "$(adb devices | wc -l)" -lt "3" ]; then 18 | false 19 | else 20 | true 21 | fi 22 | } 23 | 24 | textEscape() { 25 | # From https://gist.github.com/Pistos/0bf26f46c04bc43cc95c224d264e9f39 26 | text=$(printf '%s%%s' ${@}) # concatenate and replace spaces with %s 27 | text=${text%%%s} # remove the trailing %s 28 | text=${text//\'/\\\'} # escape single quotes 29 | text=${text//\"/\\\"} # escape double quotes 30 | text=${text//\&/\\\&} # escape ampersands 31 | text=${text//\;/\\\;} # escape semicolons 32 | text=${text//\(/\\\(} # escape opening parentheses 33 | text=${text//\)/\\\)} # escape closing parentheses 34 | text=${text//\|/\\\|} # escape pipes 35 | echo "$text" 36 | } 37 | 38 | # Wait 39 | waitForCommand() { 40 | read -p "Press ENTER to continue once the command is done." 41 | } 42 | 43 | # Run the command through adb shell input 44 | runCommandInTermux() { 45 | echo "Running command: $@" 46 | adb shell input text $(textEscape "$@") && adb shell input keyevent 66 47 | } 48 | 49 | set -e 50 | if ! isDeviceConnected; then 51 | echo "There are no ADB devices connected. If that seems wrong, try 'adb kill-server'" 52 | exit 1 53 | fi 54 | # Launch Termux activity (If it fails, terminate script execution 55 | adb shell <&1 | grep Error >/dev/null ; then 57 | echo "Termux doesn't seem to be installed. Please install it from the play store and initialise it." 58 | exit 1 59 | fi 60 | EOF 61 | 62 | if isFirstRun "$@"; then 63 | runCommandInTermux 'termux-setup-storage' 64 | waitForCommand 65 | runCommandInTermux '(apt update) && (yes | apt upgrade) && 66 | (yes | apt install dropbear rsync)' 67 | waitForCommand 68 | runCommandInTermux 'curl https://gitlab.com/reisub0/rsync-for-android/raw/master/rsyncd.conf 69 | -o /data/data/com.termux/files/usr/etc/rsyncd.conf' 70 | waitForCommand 71 | touch "$FIRSTRUNFILE" 72 | fi 73 | 74 | runCommandInTermux 'rsync --daemon; exit' 75 | echo "Forwarding port TCP:8873 to localhost:8873" 76 | adb forward tcp:8873 tcp:8873 77 | echo 78 | echo "Successfully set up. You can now sync files using rsync://localhost:8873" 79 | --------------------------------------------------------------------------------