A couple instant-rsyncd improvements:
[rsync/rsync.git] / support / instant-rsyncd
1 #!/bin/bash -e
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 dir="$(pwd)"
16
17 echo
18 echo "This will setup an rsync daemon in $dir"
19
20 if [ $# = 0 ]; then
21         IFS='' read -p 'Module name to create (or return to exit): ' module
22         [ ! "$module" ] && exit
23 else
24         module="$1"
25         shift
26 fi
27
28 if [ $# = 0 ]; then
29         IFS='' read -p 'Port number the daemon should listen on [873]: ' port
30 else
31         port="$1"
32         shift
33 fi
34 [ "$port" ] || port=873
35
36 if [ $# = 0 ]; then
37         IFS='' read -p 'User name for authentication (empty for none): ' user
38 else
39         user="$1"
40         shift
41 fi
42
43 if [ "$user" ]; then
44         IFS='' read -s -p 'Desired password: ' password
45         echo
46 fi
47
48 rsync="$1"
49 [ "$rsync" ] || rsync=rsync
50
51 moduledir="${dir%/}/$module"
52
53 mkdir "$module"
54
55 cat >rsyncd.conf <<EOF
56 log file = rsyncd.log
57 pid file = rsyncd.pid
58 port = $port
59 use chroot = no
60
61 [$module]
62     path = $module
63     read only = false
64 EOF
65
66 if [ "$user" ]; then
67         cat >>rsyncd.conf <<-EOF
68             auth users = $user
69             secrets file = $module.secrets
70         EOF
71         touch "$module".secrets
72         chmod go-rwx "$module".secrets
73         echo "$user:$password" >"$module".secrets
74         user="$user@"
75 fi
76
77 cat >start <<EOF
78 #!/bin/bash
79 set -e
80 cd \`dirname \$0\`
81 ! [ -e rsyncd.pid ] || {
82         echo "Is the daemon already running?  If not, delete rsyncd.pid."
83         exit 1
84 }
85 $rsync --daemon --config=rsyncd.conf
86 EOF
87 chmod +x start
88
89 cat >stop <<"EOF"
90 #!/bin/bash
91 set -e
92 cd `dirname $0`
93 ! [ -e rsyncd.pid ] || kill -s SIGTERM $(< rsyncd.pid)
94 EOF
95 chmod +x stop
96
97 path="rsync://$user$(hostname):$port/$module/"
98
99 if ./start; then
100         sleep .2
101         echo
102         echo "I ran the start command for the daemon.  The log file rsyncd.log says:"
103         echo
104         cat rsyncd.log
105         echo
106         echo "You can start and stop it with ./start and ./stop respectively."
107         echo "You can customize the configuration file rsyncd.conf."
108         echo
109         echo "Give rsync the following path to access the module:"
110         echo "    $path"
111         echo
112         if [ "$user" ]; then
113                 echo "Let's test the daemon now.  Enter the password you chose at the prompt."
114         else
115                 echo "Let's test the daemon now."
116         fi
117         echo
118         echo '$' $rsync --list-only "$path"
119         $rsync --list-only "$path"
120         echo
121         echo "You should see an empty folder; it's $moduledir."
122 else
123         echo "Something went wrong.  Do you see an error message?"
124 fi