-
Notifications
You must be signed in to change notification settings - Fork 1
/
rsunk
executable file
·310 lines (276 loc) · 6.44 KB
/
rsunk
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
#!/usr/bin/env bash
#
# Synopsis:
# Push /d1/d2/... to user@rsync.net:sunk/host/$(hostname -f)/root/d1/d2
# Description:
# Rsync a precious, stable directory to an rsync.net server and send
# status email. Files that change during the sync will generate
# an error, unless given the option --ignore-24.
#
# Files are sunk in archive mode to user@rsync.net:sunk/host/$(hostname).
# Directories are created as needed. Stale files on the remote server
# are deleted. An itemized summary of all sunk files is mailed to those
# who want to know (see option --notify).
#
# Directory level filter rules in .rsunk-filter are observed.
# Remember, paths in .rsunk-filter are relative to directory
# containing the .rsunk-filter file . In other words, absolute paths
# are meaningless.
#
# Obviously, the script assumes the remote server supports an rsync.net
# like environment; i.e, certain programs can be run on the remote
# server, like du, mkdir, find.
#
# For more pedestrian file-system level rsync backups, see
# https://github.com/jmscott/work/rsync-fs
# 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:
# --source-path <dir> Path to root of source directory
# --ignore-24 Ignore rsnc exit status 24 (file disappeared)
# --notify <email> Send summary report to <email>. Multiple ok.
# --ssh-auth Authorization at rsync.net
# See:
# https://github.com/jmscott/work/rsunk
# https://rsync.net
# Note:
# Add option --template to generate reasonable /.rsunk-filter file.
#
# Add option --rsync-filter-path. Not sure if global /.rsunk-filter
# makes too many assumptions about access to root. In principle
# extra merge rules can be added to /.rsunk-filter, but not total sure
# how all that works.
#
START_EPOCH=$(date +%s)
PROG=$(basename $0)
SOURCE_PATH=
TARGET=
SSH_AUTH=
DU=
# ignore exit status 24 (some files changed)
IGNORE_24=no
RSYNC_ARGS="
--numeric-ids
--verbose
--archive
--delete
--delete-excluded
--itemize-changes
--human-readable
--compress
"
TARGET_PATH=sunk/host/$(hostname -f)/root/
RSYNC_OUT=
RSYNC_TEE=cat
ITEMIZE=no
log()
{
echo "$(date +'%Y/%m/%d %H:%M:%S'): $@"
}
leave()
{
rm -f $RSYNC_OUT
log "good bye, cruel world: elapsed $(elapsed-english $START_EPOCH)"
}
do_mail()
{
SUBJECT="$1"
if [ -z "$NOTIFY" ]; then
log 'ERROR: do_mail: --option notify required' >&2
exit 1
fi
(
cat
cat <<END
Begin Process:
Elapsed: $(elapsed-english $START_EPOCH)
Hostname FQDN: $(hostname --fqdn)
User: $USER
Script Path: $0
Start Time: $RSYNC_START_TIME
End Time: $(date --rfc-3339=seconds)
SSH Auth: $SSH_AUTH
Source Path: $SOURCE_PATH
Target: $TARGET
Rsync Args: $RSYNC_ARGS
END
#
# Tack on disk usage, start end times
#
test -n "$DU" && echo " Disk Usage: $DU"
#
# Concatenate rsync output.
#
if [ -s $RSYNC_OUT ]; then
cat <<END
Begin rsync stdout/err:
END
cat $RSYNC_OUT
cat <<END
End rsync stdout/err:
END
fi
#
# Process environment
#
cat <<END
Begin Environment:
END
env
cat <<END
End Environment:
END Process:
END
) | tr -d '\r' | mail -s "$SUBJECT" $NOTIFY
}
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.
-$USER
END
exit 1
}
trap leave EXIT
while [ "$1" ]; do
ARG="$1"
shift
case "$ARG" in
--notify)
N="$1"
test -n "$N" || die 'option --notify: missing user@host'
NOTIFY="$NOTIFY $N"
shift
;;
--ssh-auth) # user@host
test -z "$SSH_AUTH" ||
die 'option --ssh-auth: given more than once'
test -n "$1" || die 'option --ssh-auth: missing auth'
SSH_AUTH="$1"
shift
;;
--source-path) # file path
test -z "$SOURCE_PATH" ||
die 'option --source-path: given more than once'
case "$1" in
'')
die 'option --source-path: missing /d1/d2/...'
;;
/)
;;
/*/)
die 'option --source-path: path can not end in /'
;;
/*)
;;
*)
die "option --source-path: not full path: $SOURCE_PATH"
;;
esac
SOURCE_PATH="$1"
shift
;;
--one-file-system)
RSYNC_ARGS="$RSYNC_ARGS --one-file-system"
;;
--dry-run)
RSYNC_ARGS="$RSYNC_ARGS --dry-run"
;;
--ignore-24)
IGNORE_24=yes
;;
--itemize)
ITEMIZE=yes
RSYNC_OUT=${TMPDIR:=/tmp}/$PROG.$$
RSYNC_TEE="tee $RSYNC_OUT"
;;
--exclude)
test -n "$1" || die 'option --exclude: missing pattern'
RSYNC_ARGS="$RSYNC_ARGS --exclude $1"
shift
;;
--*)
die "unknown option: $ARG"
;;
*)
die "unknown argument: $ARG"
;;
esac
done
log 'hello, world'
test -n "$NOTIFY" || die 'missing option --notify'
log "notify: $NOTIFY"
test -n "$SSH_AUTH" || die 'missing option --ssh-auth'
log "ssh auth: $SSH_AUTH"
log "rsync args: $RSYNC_ARGS"
log "ignore exit status 24: $IGNORE_24"
log "start epoch: $START_EPOCH"
test -n "$SOURCE_PATH" || die 'missing option: --source-path'
log "source path: $SOURCE_PATH"
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 "make sure target path $TARGET_PATH exists on rsync.net"
ssh $SSH_AUTH "mkdir -p $TARGET_PATH" ||
die "rsync.net mkdir -p failed: exit status=$?"
#
# Call rsync.
#
# Note:
# Notice the explicit adding of the command line argument
#
# --filter='dir-merge /.rsunk-filter'
#
# See gripe at top of file.
#
set -x
time rsync --filter='dir-merge /.rsunk-filter' \
$RSYNC_ARGS $SOURCE_PATH $TARGET 2>&1 | $RSYNC_TEE
STATUS="${PIPESTATUS[*]}"
set +x
case $STATUS in
'0 0')
log 'done, no errors in rsync'
;;
'24 0')
if [ $IGNORE_24 = yes ]; then
log "WARN: some files changed during transfer (ok)"
else
die "rsync: some files changed during transfer"
fi
;;
*)
die "rsync failed: exit status=$STATUS"
;;
esac
log 'calculating disk usage'
DU=$(ssh $SSH_AUTH du -sh $TARGET_PATH/$(basename $SOURCE_PATH))
log "disk usage: $DU"
echo 'Done. No Errors.' |
do_mail "OK: $PROG: $DU in $(elapsed-english $START_EPOCH)"