GitLab Commit is coming up on August 3-4. Learn how to innovate together using GitLab, the DevOps platform. Register for free: gitlabcommitvirtual2021.com

rshare 1.11 KB
Newer Older
Ambrevar's avatar
Ambrevar committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/sh

PORT=8888
CONFIG=$HOME/.config/rsync/rsync.conf
CACHE_DIR=$HOME/.cache/rsyncd

IP=$(ip addr | awk '/state UP/ {getline; getline; $0=$2; gsub(/\/.*/, "");print; exit}')

usage () {
	cat <<EOF>&2
Usage: ${0##*/} PATH

Startup an rsync daemon and share PATH as read-only under the 'files' module.
Clients can sync with, for instance:

  rsync -iavzzP rsync://$IP:$PORT/files

Options:

  -p PORT: Specify a port number (default: $PORT).
           Must be above 1024 to run without privileges.
EOF
}

while getopts ":hp:" opt; do
	case $opt in
	h)
		usage
		exit ;;
	p)
		PORT="$OPTARG" ;;
	\?)
		usage
		exit 1 ;;
	esac
done

shift $(($OPTIND - 1))

[ $# -ne 1 ] && usage && exit 1
[ "$1" = "-h" ] && usage && exit
[ "$1" = "--" ] && shift

mkdir -p "$(dirname "$CONFIG")" "$CACHE_DIR"

TARGET=$(realpath "$1")

cat<<EOF>"$CONFIG"
pid file = $CACHE_DIR/rsyncd.pid
lock file = $CACHE_DIR/rsyncd.lock
log file = $CACHE_DIR/rsyncd.log
port = $PORT
use chroot = false

[files]
path = $TARGET
comment = Rsync share
read only = true
timeout = 300

EOF

rsync --daemon --config="$CONFIG" && \
	echo "rsync daemon listening on $IP:$PORT"