-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrsunk
More file actions
executable file
·398 lines (347 loc) · 7.79 KB
/
Copy pathrsunk
File metadata and controls
executable file
·398 lines (347 loc) · 7.79 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/usr/bin/env bash
#
# Synopsis:
# Archive /d1/d2/... to rsync.net and notify the masses via email.
# Description:
# Rsync a precious directory (not fs) to an rsync.net server and send
# status email. Files are deleted on remote, as well as excluded files.
# dir /d1/d2/... sunk to
#
# <rsyc.net>:sunk/host/$(hostname -f)/root/d1/d2/...
#
# Multiple exclude files and emails to notify can be specified.
#
# Email a summary report to addresses specified in --notify options.
# Usage:
# rsunk \
# --ignore-24 \
# --notify jmscott@setspace.com \
# --notify jmscott@rj2tech.com \
# --ssh-auth 8008@usw-s008.rsync.net \
# --source-path /usr/local \
# >>log/rsunk-root.log
# rsunk --notify jmscott --source-path /home/svn --ssh-auth jmscott
# rsunk --notify jmscott --source-path /home/jmscott/backup \
# --ssh-auth jmscott --itemize
# Options:
# --subject-tag # tag desccribint
# --source-path <dir> # Path to root of source directory
# --ssh-auth # Authorization at rsync.net
# --dry-run # Do a dry run (no transfer)
# --exclude-path <path> # path to exclude file. multiple ok
# --ignore-24 # Ignore rsync exit status 24 (file gone)
# --itemize # Detailed itemization of file changes
# --notify <email> # Send summary report to <email>. Multiple ok.
# --one-file-system # Only one file system
# See:
# https://github.com/jmscott/work/rsunk
# https://github.com/jmscott/work/rsync-fs
# https://rsync.net
# Note:
# Add --fault-path or --logger for recording or sending faults to syslog!
#
# Add option --help
#
# Consider a --subject-tag option used in subject line of emailed report.
# "Subject: OK: rsunk: nightly" or "ERROR: rsunk: rj2imac-local"
#
START_EPOCH=$(date +%s)
PROG=$(basename $0)
SOURCE_PATH=
TARGET=
SSH_AUTH=
COMPRESS=
DU_OUT=
DRY_RUN=
EXCLUDE_PATHS=
# ignore exit status 24 (some files changed)
IGNORE_24=no
RSYNC_ARGS="
--archive
--compress
--delete
--delete-excluded
--human-readable
--itemize-changes
--numeric-ids
--prune-empty-dirs
--relative
--verbose
"
HOST=$(hostname -f)
TARGET_PATH=sunk/host/$HOST/root/
RSYNC_OUT=
RSYNC_TEE=cat
ITEMIZE=no
log() {
echo "$(date +'%Y/%m/%d %H:%M:%S'): $PROG: $@"
}
leave() {
rm -f $RSYNC_OUT
log "good bye, cruel world: elapsed $(elapsed-english $START_EPOCH)"
}
catch() {
log "WARN: caught signal: $1"
}
do_mail() {
SUBJECT="$1"
(
cat
cat <<END
Begin Process:
Elapsed: $(elapsed-english $START_EPOCH)
Hostname FQDN: $HOST
User: $USER
Source Path: $SOURCE_PATH
Target Path: $TARGET_PATH
Target: $TARGET
Disk Usage: $DU_OUT
Disk Usage Path: $DU_PATH
Du Error Path: $DU_ERR
Itemize: $ITEMIZE
Script Path: $0
Exclude Paths: $EXCLUDE_PATHS
Start Time: $RSYNC_START_TIME
End Time: $(date --rfc-3339=seconds)
SSH Auth: $SSH_AUTH
Rsync Args: $RSYNC_ARGS
Temp Rsync Out: $RSYNC_OUT
Temp Rsync Tee: $RSYNC_TEE
Ignore Exit 24: $IGNORE_24
Which rsunk: $(which rsunk)
PATH: $PATH
END
#
# Concatenate rsync output.
#
if [ -s "$RSYNC_OUT" ]; then
log "WTF: '$RSYNC_OUT'"
cat <<END
Begin rsync stdout/err: $RSYNC_OUT
END
cat $RSYNC_OUT
cat <<END
End rsync stdout/err: $RSYNC_OUT
END
fi
#
# Process environment
#
cat <<END
Begin Environment:
END
env | sort
cat <<END
End Environment:
END Process:
END
) | tr -d '\r' | mail -s "$SUBJECT" $NOTIFY
}
# die without mailing to users.
croak() {
echo "ERROR: $@" >&2
exit 1
}
# die parsing command line options
eopt() {
OPT=$1
shift
croak "option --$OPT: $@"
}
eexclude() {
eopt exclude-path $@
}
etwice() {
eopt $1 'given more than one'
}
essh() {
eopt ssh-auth $@
}
esrc() {
eopt source-path $@
}
die() {
log "ERROR: $@" >&2
#
#
#
MSG="$@"
if [ -n "$SOURCE_PATH" ]; then
MSG=$(basename $SOURCE_PATH)
fi
do_mail "ERROR: $PROG: $MSG" <<END
In process $PROG#$$ the following error occured:
$@
For more details, see the output of standard error of this script log file,
probably defined in a crontab for the user $USER.
END
exit 1
}
# parse command line args
while [ "$1" ]; do
ARG="$1"
shift
case "$ARG" in
--exclude-path)
EP="$1"
test -z "$EP" && eexclude 'empty path to exclude list'
test -r $EP || eexclude "can not read path: $EP"
RSYNC_ARGS="$RSYNC_ARGS --exclude-from=$EP"
EXCLUDE_PATHS="$EXCLUDE_PATHS $EP"
shift
;;
--notify)
N="$1"
test -n "$N" || die 'option --notify: missing user@host'
NOTIFY="$NOTIFY $N"
shift
;;
--ssh-auth) # user@host
test -z "$SSH_AUTH" || essh 'given more than once'
test -n "$1" || essh 'missing auth'
# Note: how to test if ssh auth is valid in config?
SSH_AUTH="$1"
shift
;;
--source-path) # file path
test -z "$SOURCE_PATH" || etwice source-path
SP="$1"
# frisk the source path
case "$SP" in
'')
esrc 'missing path to source: /d1/d2/d3...'
;;
/)
;;
/*)
test -e || esrc "directory does not exist: $1"
test -d || esrc "not a directory: $1"
;;
*)
esrc "not full path: $SP"
;;
esac
SOURCE_PATH="$SP"
shift
;;
--one-file-system)
test -z "$ONE_FS" || etwice one-file-system
RSYNC_ARGS="$RSYNC_ARGS --one-file-system"
ONE_FS=seen
;;
--dry-run)
RSYNC_ARGS="$RSYNC_ARGS --dry-run"
DRY_RUN=yes
;;
--ignore-24)
test $IGNORE_24 = no || etwice ignore-24
IGNORE_24=yes
;;
--itemize)
# itemize defaults to "no"
test $ITEMIZE = yes && etwice itemize
ITEMIZE=yes
RSYNC_OUT=${TMPDIR:=/tmp}/$PROG.$$
RSYNC_TEE="tee $RSYNC_OUT"
;;
--*)
croak "unknown option: $ARG"
;;
*)
croak "unknown cli argument: $ARG"
;;
esac
done
test -n "$SSH_AUTH" || croak 'missing option --ssh-auth'
test -n "$NOTIFY" || croak 'missing option --notify'
test -n "$SOURCE_PATH" || croak 'missing option: --source-path'
trap leave EXIT
trap 'catch TERM' SIGTERM
trap 'catch INT' SIGINT
trap 'catch QUIT' SIGQUIT
log 'hello, world'
log "host: $HOST"
log "PATH: $PATH"
log "source path: $SOURCE_PATH"
EXCLUDE_PATHS=$(echo "$EXCLUDE_PATHS" | sed 's/^ *//')
log "exclude paths: $EXCLUDE_PATHS"
NOTIFY=$(echo "$NOTIFY" | sed 's/^ *//')
log "notify: $NOTIFY"
log "start epoch: $START_EPOCH"
log "ssh auth: $SSH_AUTH"
log "ignore exit status 24: $IGNORE_24"
log "itemize: $ITEMIZE"
log "target path: $TARGET_PATH"
TARGET="$SSH_AUTH:$TARGET_PATH"
log "rsync target: $TARGET"
log 'starting rsync ...'
RSYNC_START_TIME=$(date --rfc-3339=seconds)
log "mkdir target path @rsync.net: $TARGET_PATH"
ssh -n $SSH_AUTH "mkdir -p $TARGET_PATH" ||
die "mkdir target path failed: exit status=$?"
#
# Call rsync.
#
log 'calling rsync ...'
time rsync \
$RSYNC_ARGS \
$SOURCE_PATH $TARGET 2>&1 |
$RSYNC_TEE
STATUS="${PIPESTATUS[*]}"
case $STATUS in
'0 0')
log 'done, no errors in rsync'
;;
'24 0')
if [ $IGNORE_24 = yes ]; then
log "WARN: (OK): some files changed during transfer"
else
die "rsync: some files changed during transfer"
fi
;;
*)
die "rsync failed: exit status=$STATUS"
;;
esac
# only probe rsync.net for disk size if not a dry run
if [ "$DRY_RUN" != yes ]; then
# strip /+ pattern from path do to clumsy path construction
DU_PATH=$(echo "$TARGET_PATH/$SOURCE_PATH" | tr --squeeze-repeats /)
log "remote du path: $DU_PATH"
DU_OUT=$(
ssh -n $SSH_AUTH du -sh $DU_PATH 2>&1 </dev/null |
awk '{print $1, $2}' |
sed "s@/sunk/host/$HOST/@@"
)
case "$DU_OUT" in
*[0-9]*[KMGT]*)
log "disk usage: $DU_OUT"
;;
'')
log 'empty du output'
DU_ERR=$(
realpath \
--canonicalize-missing \
--relative-base sunk/host/$HOST/root \
$DU_PATH
)
die "du failed path: $DU_ERR"
;;
*)
die "wierd du output: $DU_OUT"
;;
esac
fi
# Trim up the Subject: line
SUBJECT=$(
echo OK: $DU_OUT: $(elapsed-english $START_EPOCH) |
sed \
--expression '
s/^[[:space:]]*//;
s/[[:space:]]*$//;
s/[[:space:]]+/ /g
' \
--regexp-extended
)
echo 'Done. No Errors.' | do_mail "$SUBJECT"
exit 0