Commit | Line | Data |
---|---|---|
45574a73 MM |
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 | if [ "$#" -lt 3 ]; then | |
20 | echo "I would install an rsync daemon in $dir if you gave me" | |
21 | echo "a module name, a port, and an rsync username." | |
22 | exit 1 | |
23 | fi | |
24 | ||
25 | module="$1" | |
26 | port="$2" | |
27 | user="$3" | |
28 | rsync="$4" | |
29 | if [ ! "$rsync" ]; then | |
30 | rsync=rsync | |
31 | fi | |
32 | ||
33 | moduledir="${dir%/}/$module" | |
34 | ||
35 | echo | |
36 | echo "I'm about to install an rsync daemon in $dir." | |
37 | echo "It will listen on port $port for requests giving rsync username $user" | |
38 | echo "and the password you are about to specify. It will serve a module" | |
39 | echo "$module corresponding to $moduledir." | |
40 | echo | |
41 | ||
42 | IFS='' read -s -p 'Desired password: ' password | |
43 | ||
44 | mkdir "$module" | |
45 | ||
46 | cat >rsyncd.conf <<EOF | |
47 | log file = rsyncd.log | |
48 | pid file = rsyncd.pid | |
49 | port = $port | |
50 | use chroot = no | |
51 | ||
52 | [$module] | |
53 | path = $module | |
54 | read only = false | |
55 | auth users = $user | |
56 | secrets file = $module.secrets | |
57 | EOF | |
58 | ||
59 | touch "$module".secrets | |
60 | chmod go-rwx "$module".secrets | |
61 | cat >"$module".secrets <<EOF | |
62 | $user:$password | |
63 | EOF | |
64 | ||
65 | cat >start <<EOF | |
66 | #!/bin/bash | |
67 | set -e | |
68 | cd \`dirname \$0\` | |
69 | ! [ -e rsyncd.pid ] || { | |
70 | echo "Is the daemon already running? If not, delete rsyncd.pid." | |
71 | exit 1 | |
72 | } | |
73 | $rsync --daemon --config=rsyncd.conf | |
74 | EOF | |
75 | chmod +x start | |
76 | ||
77 | cat >stop <<"EOF" | |
78 | #!/bin/bash | |
79 | set -e | |
80 | cd `dirname $0` | |
81 | ! [ -e rsyncd.pid ] || kill -s SIGTERM $(< rsyncd.pid) | |
82 | EOF | |
83 | chmod +x stop | |
84 | ||
85 | path="rsync://$user@$(hostname):$port/$module/" | |
86 | ||
87 | if ./start; then | |
88 | sleep .2 | |
89 | echo | |
90 | echo "I tried to start the daemon. The log file rsyncd.log says:" | |
91 | echo | |
92 | cat rsyncd.log | |
93 | echo | |
94 | echo "You can start and stop it with ./start and ./stop respectively." | |
95 | echo "You can customize the configuration file rsyncd.conf." | |
96 | echo | |
97 | echo "Give rsync the following path to access the module:" | |
98 | echo " $path" | |
99 | echo | |
100 | echo "Let's test the daemon now. Enter the password you chose." | |
101 | echo '$' $rsync --list-only "$path" | |
102 | $rsync --list-only "$path" | |
103 | echo | |
104 | echo "You should see an empty folder; it's $moduledir." | |
105 | else | |
106 | echo "Something went wrong. Do you see an error message?" | |
107 | fi |