-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsyncSSH
executable file
·281 lines (244 loc) · 7.95 KB
/
syncSSH
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/bin/sh
################################################
#
# syncSSH
#
# Version: 0.0.6.1 (2012AUG26)
################################################
# Setup script variables
APP_VERSION="0.0.6.1"
APP_NAME="syncSSH"
APP_DATE="2012AUG26"
APP_DESC="A wrapper script for using rsync over ssh."
# Usage Information
APP_USAGE="$(basename $0) $APP_VERSION $APP_DESC
Usage:
-h,--help Alias of usage (-u, --usage)
-H,--remote-host [user@]host IP or DNS name of the ssh remote host.
*REQUIRED*
-k,--keyfile keyfile Path to the key file to use for
authentication.
(default: $HOME/.ssh/id_rsa)
-l,--local-path localpath Local path, source path for syncing.
(default: current directory)
-L,--log-file logfile Location to save a log of the results.
(default: .\syncSSH.log)
-p,--port port SSH port to use.
-q,--quiet Run quietly with no output.
-r,--remote-path remotepath Remote path, destination path on
remote host for syncing.
*REQUIRED*
-u,--usage Display the help/usage information.
-v,--verbose Verbose mode. Causes script to print
debugging messages about its progress.
This is helpful in debugging connection,
authentication, and configuration problems.
Multiple -v options increase the verbosity.
The maximum is 3.
-V,--version Display the version number and exit.\n"
while [ $# -gt 0 ]
do
case "$1" in
-H | --remote-host)
# remote host
# extract host
SSH_HOST=`expr "$2" : "\(^[^\.\-][a-zA-Z0-9_@\.\-]*\)"`
if [ -z "$SSH_HOST" ]
then
break
else
shift
fi
;;
-k | --keyfile)
# keyfile
# check to make sure the provided argument is a valid keyfile.
if [ -f "$2" ]
then
KEYFILE="$2"
shift
else
APP_PARAM_ERROR="$2 is not a file."
break
fi
;;
-l | --local-path)
# local path
# check to make sure the provided argument is a valid keyfile.
if [ -d "$2" ]
then
LOCAL_PATH="$2"
shift
else
APP_PARAM_ERROR="$2 is not a valid local path."
break
fi
;;
-L | --log-file)
# log file
# check to see if a logfile is provided
# set RAW_LOG_FILE
RAW_LOG_FILE=$2
CLEAN_LOG_FILE=`expr "$RAW_LOG_FILE" : "\(^[^-][^\\:/]*\)"`
# Full path to file
if [ -f "$2" ]
then
LOG_FILE="$2"
shift
# Directory
elif [ -d "$2" ]
then
LOG_FILE="$2/syncSSH.log"
shift
# Create provided filename in the working directory
elif [ ${#RAW_LOG_FILE} = ${#CLEAN_LOG_FILE} ]
then
LOG_FILE=$CLEAN_LOG_FILE
shift
# invalid logfile
elif [ ${#RAW_LOG_FILE} != ${#CLEAN_LOG_FILE} ]
then
APP_PARAM_ERROR="Invalid log file."
break
else
LOG_FILE="syncSSH.log"
fi
#create LOG_FILE_COM
LOG_FILE_COM=" >> $LOG_FILE"
;;
-p | --port)
# SSH port
# make a port is provided and that it is a number between 1-65535
if ! [ "$2" -eq 0 -o "$2" -ne 0 2>/dev/null ]
then
APP_PARAM_ERROR="You must provide the port number with -p."
break
elif [ "$2" -lt 1 -o "$2" -gt 65535 ]
then
APP_PARAM_ERROR="Invalid port, $2, it must be between 1 and 65535."
break
else
SSH_PORT=" -p$2"
shift
fi
;;
-q | --quiet)
# quiet mode
APP_QUIET=">& /dev/null"
;;
-r | --remote-path)
# remote path
if [ -z "$2" ]
then
break
else
REMOTE_PATH="$2"
shift
fi
;;
-u | -h | --usage | --help)
# usage
printf "$APP_USAGE"
exit
;;
-vv* | --verbose)
# verbose
APP_VERBOSE=$1
;;
-V | --version)
# version
printf "$APP_NAME v$APP_VERSION ($APP_DATE)"
exit
;;
*)
# unknown variable
if [ -n "$APP_PARAM_ERROR" ]
then
APP_PARAM_ERROR="$APP_PARAM_ERROR, $1"
else
APP_PARAM_ERROR="Unknown Parameter(s): $1"
fi
;;
esac
shift
done
# Setup Keyfile
#if [ -z "$KEYFILE" && -f "$HOME/.ssh/id_rsa" ]
#then
# KEYFILE="$HOME/.ssh/id_rsa"
#elif [ -z "$KEYFILE" ]
#then
# if [ -n "$APP_PARAM_ERROR" ]
# then
# APP_PARAM_ERROR="$APP_PARAM_ERROR\nDefault keyfile (~/.ssh/id_rsa) could not be found and a key was not provided (-k,--keyfile keyfile)."
# else
# APP_PARAM_ERROR="Default keyfile (~/.ssh/id_rsa) could not be found and a key was not provided (-k,--keyfile keyfile)."
# fi
#fi
#KEYFILE="/home/jpscharf/keys/rsync-svn-serenity"
# Setup Local Path
if [ -z "$LOCAL_PATH"]
then
LOCAL_PATH=${PWD}
fi
# Setup remote path
if [ -z "$REMOTE_PATH" ]
then
if [ -n "$APP_PARAM_ERROR" ]
then
APP_PARAM_ERROR="$APP_PARAM_ERROR\nRemote path is a required parameter (-r,--remote-path remotepath)."
else
APP_PARAM_ERROR="Remote path is a required parameter (-r,--remote-path remotepath)."
fi
fi
# Check SSH Host
if [ -z "$SSH_HOST" ]
then
if [ -n "$APP_PARAM_ERROR" ]
then
APP_PARAM_ERROR="$APP_PARAM_ERROR\nRemote host is a required parameter (-H,--remote-host [user@]host)."
else
APP_PARAM_ERROR="Remote host is a required parameter (-H,--remote-host [user@]host)."
fi
fi
# Setup ssh command
#check for keyfile
if [ -n "$KEYFILE" ]
then
SSH_COM="ssh $APP_VERBOSE$SSH_PORT -oPreferredAuthentications=publickey -i $KEYFILE"
else
SSH_COM="ssh $APP_VERBOSE$SSH_PORT"
fi
# Setup rsync command
RSYNC_COM="rsync -azl $APP_VERBOSE -e \"$SSH_COM\" \"$LOCAL_PATH\" $SSH_HOST:\"$REMOTE_PATH\""
# Check for errors in parameters
if [ -n "$APP_PARAM_ERROR" ]
then
printf '\a'
printf "$APP_NAME ($APP_VERSION): ABORTED\n$APP_PARAM_ERROR"
printf "\nFor help, please try, $(basename $0) -u.\n"
exit
fi
if [ -z "$APP_QUIET" ]
then
printf "################################################\n"
printf "$APP_VERSION $APP_NAME\n"
printf "################################################\n"
if [ -n "$KEYFILE" ]
then
printf "Keyfile Setup:\n $KEYFILE\n"
fi
printf "Local path configured:\n $LOCAL_PATH\n"
if [ -n "$LOG_FILE" ]
then
printf "Logfile:\n $LOG_FILE\n"
fi
printf "Remote path configure:\n $REMOTE_PATH\n"
printf "SSH host configured:\n $SSH_HOST\n"
printf "SSH command generated:\n $SSH_COM\n"
printf "rsync command generated:\n [$RSYNC_COM]\n"
printf "Running....\n"
fi
# Run rsync
# printf "$RSYNC_COM $APP_QUIET$LOG_FILE_COM"
eval "$RSYNC_COM $APP_QUIET$LOG_FILE_COM"