-
Notifications
You must be signed in to change notification settings - Fork 12
/
rsyncStatic.sh
140 lines (118 loc) · 2.63 KB
/
rsyncStatic.sh
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#/usr/bin/env bash
CURDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
MYNAME="${0##*/}"
LOGFILE="$CURDIR/app.log"
g_THREADS=3
RET_OK=0
RET_FAIL=1
tmpfile="$CURDIR/$$.fifo"
mkfifo $tmpfile
exec 4<>$tmpfile
rm -rf $tmpfile
_usage() {
cat << USAGE
Usage: bash ${MYNAME} [options] rsynclist.
Options:
-c, --concurrent num Thread Nummber for run the command at same time, default: $g_THREADS.
Require:
rsynclist Rsync command list for operation.
Notice:
please check the result output ${LOGFILE}.
USAGE
exit $RET_OK
}
_report_err() { echo "${MYNAME}: Error: $*" >&2 ; }
_is_number() {
local re='^[0-9]+$'
local number="$1"
if [ "x$number" == "x" ]; then
_report_err "_is_number need one parameter"
exit $RET_FAIL
else
number=${number//[[:space:]]/}
fi
if [[ $number =~ $re ]] ; then
return 0
else
_report_err "${number} not a number" >&2
exit $RET_FAIL
fi
}
readlinkf() { perl -MCwd -e 'print Cwd::abs_path shift' $1;}
#
# Parses command-line options.
# usage: _parse_options "$@" || exit $?
#
_parse_options()
{
declare -a argv
while [[ $# -gt 0 ]]; do
case $1 in
-c|--concurrent)
g_THREADS="${2}"
_is_number "${g_THREADS}"
shift 2
;;
-h|--help)
_usage
exit
;;
--)
shift
argv=("${argv[@]}" "${@}")
break
;;
-*)
_report_err "command line: unrecognized option $1" >&2
return 1
;;
*)
argv=("${argv[@]}" "${1}")
shift
;;
esac
done
case ${#argv[@]} in
1)
g_RSYNCLIST=$(readlinkf "${argv[0]}")
;;
0|*)
_usage 1>&2
return 1
;;
esac
}
function exec_cmd()
{
local line="$@"
if [ x"$line" = "x" ]; then
return 1
fi
echo timeout -s 9 40 "$line" | "/bin/sh"
if [ $? -ne 0 ]
then
echo ERROR : $line >> "${LOGFILE}"
return 1
fi
}
##################################################
### Main
##################################################
echo "======== start time : `date`" >> "$LOGFILE"
_parse_options "${@}" || _usage
for ((i=1;i<=${g_THREADS};i++))
do
echo >&4
done
cat "${g_RSYNCLIST}" | grep -v "^ *#" | grep -v "^ *$" | while read line
do
read <&4
{
exec_cmd "$line"
echo >&4
} &
done
sleep 5s
wait
exec 4<&-
echo "======== end time : `date`" >> "${LOGFILE}"