A couple instant-rsyncd improvements:
[rsync/rsync.git] / support / instant-rsyncd
CommitLineData
794d0339 1#!/bin/bash -e
45574a73
MM
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
45574a73
MM
15dir="$(pwd)"
16
794d0339
WD
17echo
18echo "This will setup an rsync daemon in $dir"
19
20if [ $# = 0 ]; then
21 IFS='' read -p 'Module name to create (or return to exit): ' module
22 [ ! "$module" ] && exit
23else
24 module="$1"
25 shift
45574a73
MM
26fi
27
794d0339
WD
28if [ $# = 0 ]; then
29 IFS='' read -p 'Port number the daemon should listen on [873]: ' port
30else
31 port="$1"
32 shift
45574a73 33fi
794d0339 34[ "$port" ] || port=873
45574a73 35
794d0339
WD
36if [ $# = 0 ]; then
37 IFS='' read -p 'User name for authentication (empty for none): ' user
38else
39 user="$1"
40 shift
41fi
45574a73 42
794d0339
WD
43if [ "$user" ]; then
44 IFS='' read -s -p 'Desired password: ' password
45 echo
46fi
45574a73 47
794d0339
WD
48rsync="$1"
49[ "$rsync" ] || rsync=rsync
50
51moduledir="${dir%/}/$module"
45574a73
MM
52
53mkdir "$module"
54
55cat >rsyncd.conf <<EOF
56log file = rsyncd.log
57pid file = rsyncd.pid
58port = $port
59use chroot = no
60
61[$module]
794d0339
WD
62 path = $module
63 read only = false
45574a73
MM
64EOF
65
794d0339
WD
66if [ "$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@"
75fi
45574a73
MM
76
77cat >start <<EOF
78#!/bin/bash
79set -e
80cd \`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
86EOF
87chmod +x start
88
89cat >stop <<"EOF"
90#!/bin/bash
91set -e
92cd `dirname $0`
93! [ -e rsyncd.pid ] || kill -s SIGTERM $(< rsyncd.pid)
94EOF
95chmod +x stop
96
794d0339 97path="rsync://$user$(hostname):$port/$module/"
45574a73
MM
98
99if ./start; then
100 sleep .2
101 echo
794d0339 102 echo "I ran the start command for the daemon. The log file rsyncd.log says:"
45574a73
MM
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
794d0339
WD
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
45574a73
MM
118 echo '$' $rsync --list-only "$path"
119 $rsync --list-only "$path"
120 echo
121 echo "You should see an empty folder; it's $moduledir."
122else
123 echo "Something went wrong. Do you see an error message?"
124fi