| 1 | #!/bin/bash |
| 2 | |
| 3 | # instant-rsyncd lets you quickly set up and start a simple, unprivileged rsync |
| 4 | # daemon with a single module in the current directory. I've found it |
| 5 | # invaluable for quick testing, and I use it when writing a list of commands |
| 6 | # that people can paste into a terminal to reproduce a daemon-related bug. |
| 7 | # Sysadmins deploying an rsync daemon for the first time may find it helpful as |
| 8 | # a starting point. |
| 9 | # |
| 10 | # Usage: instant-rsyncd MODULE PORT RSYNCD-USERNAME [RSYNC-PATH] |
| 11 | # The script asks for the rsyncd user's password twice on stdin, once to set it |
| 12 | # and once to log in to test the daemon. |
| 13 | # -- Matt McCutchen <matt@mattmccutchen.net> |
| 14 | |
| 15 | set -e |
| 16 | |
| 17 | dir="$(pwd)" |
| 18 | |
| 19 | echo |
| 20 | echo "This will setup an rsync daemon in $dir" |
| 21 | |
| 22 | if [ $# = 0 ]; then |
| 23 | IFS='' read -p 'Module name to create (or return to exit): ' module |
| 24 | [ ! "$module" ] && exit |
| 25 | else |
| 26 | module="$1" |
| 27 | shift |
| 28 | fi |
| 29 | |
| 30 | if [ $# = 0 ]; then |
| 31 | IFS='' read -p 'Port number the daemon should listen on [873]: ' port |
| 32 | else |
| 33 | port="$1" |
| 34 | shift |
| 35 | fi |
| 36 | [ "$port" ] || port=873 |
| 37 | |
| 38 | if [ $# = 0 ]; then |
| 39 | IFS='' read -p 'User name for authentication (empty for none): ' user |
| 40 | else |
| 41 | user="$1" |
| 42 | shift |
| 43 | fi |
| 44 | |
| 45 | if [ "$user" ]; then |
| 46 | IFS='' read -s -p 'Desired password: ' password |
| 47 | echo |
| 48 | fi |
| 49 | |
| 50 | rsync="$1" |
| 51 | [ "$rsync" ] || rsync=rsync |
| 52 | |
| 53 | moduledir="${dir%/}/$module" |
| 54 | |
| 55 | mkdir "$module" |
| 56 | |
| 57 | cat >rsyncd.conf <<EOF |
| 58 | log file = rsyncd.log |
| 59 | pid file = rsyncd.pid |
| 60 | port = $port |
| 61 | use chroot = no |
| 62 | |
| 63 | [$module] |
| 64 | path = $module |
| 65 | read only = false |
| 66 | EOF |
| 67 | |
| 68 | if [ "$user" ]; then |
| 69 | cat >>rsyncd.conf <<-EOF |
| 70 | auth users = $user |
| 71 | secrets file = $module.secrets |
| 72 | EOF |
| 73 | touch "$module".secrets |
| 74 | chmod go-rwx "$module".secrets |
| 75 | echo "$user:$password" >"$module".secrets |
| 76 | user="$user@" |
| 77 | fi |
| 78 | |
| 79 | cat >start <<EOF |
| 80 | #!/bin/bash |
| 81 | set -e |
| 82 | cd \`dirname \$0\` |
| 83 | ! [ -e rsyncd.pid ] || { |
| 84 | echo "Is the daemon already running? If not, delete rsyncd.pid." |
| 85 | exit 1 |
| 86 | } |
| 87 | $rsync --daemon --config=rsyncd.conf |
| 88 | EOF |
| 89 | chmod +x start |
| 90 | |
| 91 | cat >stop <<"EOF" |
| 92 | #!/bin/bash |
| 93 | set -e |
| 94 | cd `dirname $0` |
| 95 | ! [ -e rsyncd.pid ] || kill -s SIGTERM $(< rsyncd.pid) |
| 96 | EOF |
| 97 | chmod +x stop |
| 98 | |
| 99 | path="rsync://$user$(hostname):$port/$module/" |
| 100 | |
| 101 | if ./start; then |
| 102 | sleep .2 |
| 103 | echo |
| 104 | echo "I ran the start command for the daemon. The log file rsyncd.log says:" |
| 105 | echo |
| 106 | cat rsyncd.log |
| 107 | echo |
| 108 | echo "You can start and stop it with ./start and ./stop respectively." |
| 109 | echo "You can customize the configuration file rsyncd.conf." |
| 110 | echo |
| 111 | echo "Give rsync the following path to access the module:" |
| 112 | echo " $path" |
| 113 | echo |
| 114 | if [ "$user" ]; then |
| 115 | echo "Let's test the daemon now. Enter the password you chose at the prompt." |
| 116 | else |
| 117 | echo "Let's test the daemon now." |
| 118 | fi |
| 119 | echo |
| 120 | echo '$' $rsync --list-only "$path" |
| 121 | $rsync --list-only "$path" |
| 122 | echo |
| 123 | echo "You should see an empty folder; it's $moduledir." |
| 124 | else |
| 125 | echo "Something went wrong. Do you see an error message?" |
| 126 | fi |