summaryrefslogtreecommitdiffstats
path: root/rsyncd-scp.sh
blob: de54a8e063eb7bd2a1b476545d57bb6f936530e4 (plain)
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
#!/bin/bash

set -e

host=${1%%:*}
path=${1#*:}
dst=$2

if [[ -z $1 || -z $2 ]]; then
	echo "${0%%*/}: <host:src> <dst>"
	exit 0
fi

while :; do
	port=$RANDOM
	[[ $port -gt 1024 && $port -lt 65535 ]] && break
done

rm_tmpdir=$(ssh $host 'mktemp -d /tmp/rsyncd-scp.XXXXXX')

cleanup() {
	ssh $host "kill \`<'$rm_tmpdir/pid'\`" &>/dev/null || true
	ssh $host "rm -rf $rm_tmpdir"
}

trap cleanup EXIT INT TERM

ssh $host 'cat <<EOF >'"$rm_tmpdir"'/rsyncd.conf
max connections = 1
pid file = '"$rm_tmpdir"'/pid
lock file = '"$rm_tmpdir"'/lock
use chroot = no
log file = /dev/null
port = '$port'
[share]
  path = '"$path"'
EOF
rsync --daemon --config='"$rm_tmpdir"'/rsyncd.conf;
'

rsync --progress --port $port "rsync://$host/share/" -rt "$dst"