-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrsync-pi.sh
executable file
·131 lines (105 loc) · 3.53 KB
/
rsync-pi.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
#!/bin/bash -e
#
# Copyright (c) 2018 Chen-Ting Chuang
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
PROGNAME="$(basename "$0")"
if [[ $(uname) == "Darwin" ]]; then
GETOPT="/usr/local/opt/gnu-getopt/bin/getopt"
if [[ ! -x "${GETOPT}" ]]; then
echo "Error: cannot find gnu-getopt"
echo "$ brew install gnu-getopt"
exit 1
fi
RSYNC="/usr/local/bin/rsync"
if [[ ! -x "${RSYNC}" ]]; then
echo "Error: cannot find Homebrew rsync"
echo "$ brew install rsync"
exit 1
fi
else
GETOPT="getopt"
RSYNC="rsync"
fi
OPTS=$("${GETOPT}" -o c:s:dh -l code:,host:,delete,help -n "${PROGNAME}" -- "$@")
if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi
eval set -- "$OPTS"
declare -a HOSTS=()
CODE=
HELP=false
REMOTE_PATH=/home/pi/yolo3-camera
while true; do
case "$1" in
-h | --help ) HELP=true; shift ;;
-c | --code ) CODE="$2"; shift; shift ;;
-s | --host ) HOSTS=("${HOSTS[@]}" "$2"); shift; shift ;;
-d | --delete ) EXTRA_RSYNC_OPTION="${EXTRA_RSYNC_OPTION} --delete"; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
done
if [ $HELP = true ]; then
cat <<EOF
${PROGNAME} [OPTIONS]
-h, --help
Show this help
-c, --code [CODE_PATH]
Specify local code path of yolo3-camera.
-s, --host [HOSTNAME]
Specify SSH hostname.
You can specify this option more than once to
deploy multiple hosts.
-d, --delete
Enable --delete rsync option (use with caution).
This script will rsync CODE_PATH into HOSTNAME:${REMOTE_PATH}.
You need to add your Raspberry Pi in ~/.ssh/config first.
Example ~/.ssh/config:
Host pi
HostName 192.168.1.102
User pi
Example command:
\$ ${PROGNAME} -c ~/Code/yolo3-camera -s pi
EOF
exit 0
fi
if [[ -z "${CODE}" ]]; then
echo "${PROGNAME}: --code argument is missing." >&2
exit 1
fi
if [ ${#HOSTS[@]} -eq 0 ]; then
echo "${PROGNAME}: --host argument is missing." >&2
exit 1
fi
# rsync acts differently with and without trailing slash.
# We always append trailing slash.
CODE=$(echo $CODE | sed 's#\/*$#\/#g')
echo CODE=$CODE
echo HOSTS="${HOSTS[@]}"
echo REMOTE_PATH=$REMOTE_PATH
echo
for HOST in "${HOSTS[@]}"
do
echo ================================================
echo "[${HOST}]"
"$RSYNC" -ave ssh \
--exclude '.*' --exclude 'TAGS' \
--exclude '*.pyc' --exclude '__pycache__' \
--exclude '*.so' --exclude '*.a' --exclude '*.o' \
"${CODE}" "${HOST}:${REMOTE_PATH}"
done