-
Notifications
You must be signed in to change notification settings - Fork 3
/
sync_sftp
executable file
·55 lines (46 loc) · 1.66 KB
/
sync_sftp
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
#!/bin/bash
configfile=server.conf
usage() { echo "Usage: $0 [-c <configfile>] [ -N <newerthan> ] [ -e <exclude-rx-file> ] [filter [...]]" 1>&2; exit $1; }
while getopts "c:N:e:h" o; do
case "${o}" in
c) configfile=${OPTARG}
if [[ ! -r ${configfile} ]]; then
echo "Cannot read ${configfile}" 1>&2
usage 1
fi
;;
e) exrxfile="${OPTARG}" ;;
N) newerthan="${OPTARG}" ;;
h) usage 0 ;;
*) usage 1 ;;
esac
done
shift $((OPTIND-1))
. ${configfile}
: ${fileserver:?}
# ${srvport}
# ${prefix}
: ${expname:?}
: ${basedir:=$(pwd)}
: ${download:?}
: ${parallel:=16}
: ${contimeout:=300}
: ${retries:=10}
: ${iotimeout:=300}
if [[ $( < ~/.netrc ) =~ machine[[:space:]]+${fileserver}[[:space:]]?login[[:space:]]+([^[:space:]]+) ]]; then
username="${BASH_REMATCH[1]}"
elif [[ $( < ~/.ssh/config ) =~ Host[[:space:]]+${fileserver}[[:space:]]+ ]]; then
username=
else
echo "cannot find login for machine ${fileserver} in ~/.netrc nor ~/.ssh/config" >&2
exit 1
fi
if (( ${#@} )); then
dir=( "${@/#/ --directory=${prefix:+${prefix}/}${expname}/}" )
source="${dir[*]}"
else
source="--directory=${prefix:+${prefix}/}${expname}/*"
fi
umask 0002
#exec echo "${exrxfile:+ --exclude-rx-from="${exrxfile}"}"
exec lftp -c "set cmd:move-background false; set net:timeout $(( contimeout / retries)); set net:max-retries ${retries}; set net:reconnect-interval-base 8; set xfer:timeout ${iotimeout}; connect sftp://${username:+${username}@}${fileserver}${srvport:+:${srvport}}; mirror --continue --no-perms --parallel=${parallel} --loop --target-directory=${basedir}/${download} ${source} ${newerthan:+ --newer-than="'${newerthan}'"}${exrxfile:+ --exclude-rx-from="'${exrxfile}'"}"