-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlvm-snapshot-backup-wrapper.lib.sh
300 lines (251 loc) · 8.06 KB
/
lvm-snapshot-backup-wrapper.lib.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
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
## vim:ts=4:sw=4:tw=200:nu:ai:nowrap:
##
## application library for lvm-snapshot-backup-wrapper
##
## Created by Wolfram Schlich <wschlich@gentoo.org>
## Licensed under the GNU GPLv3
## Web: http://www.bashinator.org/projects/lvm-snapshot-backup-wrapper/
## Code: https://github.com/wschlich/lvm-snapshot-backup-wrapper/
##
##
## REQUIRED PROGRAMS IN PATH
## =========================
## - mktemp
## - touch
## - rm
##
##
## application control functions
##
function __init() {
return 0 # success
} # __init()
function __main() {
## initialize variables
local -i Error=0
local -i Fatal=0
local -i SystemMirrorCreatedSuccessfully=0
local -i SystemMirrorCreatePreExecCommandRanSuccessfully=0
local -i SystemMirrorCreatePostExecCommandRanSuccessfully=0
local -i SystemBackupFinishedSuccessfully=0
local -i SystemMirrorDeletedSuccessfully=0
local -i SystemMirrorDeletePreExecCommandRanSuccessfully=0
local -i SystemMirrorDeletePostExecCommandRanSuccessfully=0
## run system mirror create pre-exec command (if set)
if [[ -n ${SystemMirrorCreatePreExecCommand} ]]; then
__msg info "running system mirror create pre-exec command"
if ! eval ${SystemMirrorCreatePreExecCommand} &>"${_L}"; then
__msg err "failed running system mirror create pre-exec command"
let Fatal=1
else
__msg info "successfully ran the system mirror create pre-exec command"
let SystemMirrorCreatePreExecCommandRanSuccessfully=1
fi
fi
## create system mirror when system mirror create pre-exec command ran successfully (if set)
if [[ -z ${SystemMirrorCreatePreExecCommand} || ${SystemMirrorCreatePreExecCommandRanSuccessfully} -eq 1 ]]; then
__msg info "creating system mirror"
if ! createSystemMirror; then
__msg err "failed to create system mirror"
let Fatal=1
else
__msg info "successfully created the system mirror"
let SystemMirrorCreatedSuccessfully=1
fi
fi
## run system mirror create post-exec command (if set) when system mirror was created successfully
if [[ ${SystemMirrorCreatedSuccessfully} -eq 1 && -n ${SystemMirrorCreatePostExecCommand} ]]; then
__msg info "running system mirror create post-exec command"
if ! eval ${SystemMirrorCreatePostExecCommand} &>"${_L}"; then
__msg err "failed running system mirror create post-exec command"
let Fatal=1
else
__msg info "successfully ran the system mirror create post-exec command"
let SystemMirrorCreatePostExecCommandRanSuccessfully=1
fi
fi
## run requested backup tool in server mode when system mirror was created successfully
## and system mirror create post-exec command ran successfully (if set)
if [[ ${SystemMirrorCreatedSuccessfully} -eq 1 && \
( -z ${SystemMirrorCreatePostExecCommand} || ${SystemMirrorCreatePostExecCommandRanSuccessfully} -eq 1 ) ]]; then
## check original command to determine requested backup tool
BackupCommand="${SSH_ORIGINAL_COMMAND}"
case "${BackupCommand}" in
## rsnapshot/rsync
rsync[[:space:]]--server[[:space:]]--sender[[:space:]]*)
__msg info "running rsync"
if ! runRsync; then
__msg err "failed running rsync"
let Error=1
else
__msg info "successfully ran rsync"
let SystemBackupFinishedSuccessfully=1
fi
;;
## rdiff-backup
rdiff-backup[[:space:]]*)
__msg info "running rdiff-backup"
if ! runRdiffBackup; then
__msg err "failed running rdiff-backup"
let Error=1
else
__msg info "successfully ran diff-backup"
let SystemBackupFinishedSuccessfully=1
fi
;;
## invalid backup command
*)
__msg err "invalid backup command: '${BackupCommand}'"
let Error=1
;;
esac
fi
## run system mirror delete pre-exec command (if set)
if [[ -n ${SystemMirrorDeletePreExecCommand} ]]; then
__msg info "running system mirror delete pre-exec command"
if ! eval ${SystemMirrorDeletePreExecCommand} &>"${_L}"; then
__msg err "failed running system mirror delete pre-exec command"
let Fatal=1
else
__msg info "successfully ran the system mirror delete pre-exec command"
let SystemMirrorDeletePreExecCommandRanSuccessfully=1
fi
fi
## delete system mirror when system mirror delete pre-exec command ran successfully (if set)
if [[ -z ${SystemMirrorDeletePreExecCommand} || ${SystemMirrorDeletePreExecCommandRanSuccessfully} -eq 1 ]]; then
__msg info "deleting system mirror"
if ! deleteSystemMirror; then
__msg err "failed to delete the system mirror"
let Fatal=1
else
__msg info "successfully deleted the system mirror"
let SystemMirrorDeletedSuccessfully=1
fi
fi
## run system mirror delete post-exec command (if set) when system mirror was deleted successfully
if [[ ${SystemMirrorDeletedSuccessfully} -eq 1 && -n ${SystemMirrorDeletePostExecCommand} ]]; then
__msg info "running system mirror delete post-exec command"
if ! eval ${SystemMirrorDeletePostExecCommand} &>"${_L}"; then
__msg err "failed running system mirror delete post-exec command"
let Fatal=1
else
__msg info "successfully ran the system mirror delete post-exec command"
let SystemMirrorDeletePostExecCommandRanSuccessfully=1
fi
fi
## check for fatal errors
if [[ ${Fatal} -gt 0 ]]; then
__die 2 "a fatal error occured, please check preceding log entries for details"
fi
## check for non-fatal errors (remove the lockfile before exiting)
if [[ ${Error} -gt 0 ]]; then
rm -f "${__ScriptLockFile}" >&/dev/null # TODO FIXME
__die 2 "a non-fatal error occured, please check preceding log entries for details"
fi
__msg info "finished successfully"
} # __main()
##
## application worker functions
##
function createSystemMirror() {
## ----- head -----
##
## DESCRIPTION:
## creates a mirror of all mounted volumes using lvm-snaptool.sh
##
## ARGUMENTS:
## /
##
## GLOBAL VARIABLES USED:
## _L
## LvmSnapTool
## LvmSnapToolSystemMirrorCreateArgs
## SystemMirrorMountDirectory
##
## ----- main -----
"${LvmSnapTool}" -q -M -d "${SystemMirrorMountDirectory}" ${LvmSnapToolSystemMirrorCreateArgs} -C >>"${_L}" 2>&1
local -i lvmSnapToolExitCode=${?}
if [[ ${lvmSnapToolExitCode} -ne 0 ]]; then
__msg err "failed running lvm-snaptool (exit code: ${lvmSnapToolExitCode})"
return 2 # error
fi
return 0 # success
} # createSystemMirror()
function deleteSystemMirror() {
## ----- head -----
##
## DESCRIPTION:
## deletes a mirror of all mounted volumes using lvm-snaptool.sh
##
## ARGUMENTS:
## /
##
## GLOBAL VARIABLES USED:
## _L
## LvmSnapTool
## LvmSnapToolSystemMirrorDeleteArgs
## SystemMirrorMountDirectory
##
## ----- main -----
"${LvmSnapTool}" -q -M -d "${SystemMirrorMountDirectory}" -D ${LvmSnapToolSystemMirrorDeleteArgs} >>"${_L}" 2>&1
local -i lvmSnapToolExitCode=${?}
if [[ ${lvmSnapToolExitCode} -ne 0 ]]; then
__msg err "failed running lvm-snaptool (exit code: ${lvmSnapToolExitCode})"
return 2 # error
fi
return 0 # success
} # deleteSystemMirror()
function runRdiffBackup() {
## ----- head -----
##
## DESCRIPTION:
## runs rdiff-backup in server mode
##
## ARGUMENTS:
## /
##
## GLOBAL VARIABLES USED:
## SystemMirrorMountDirectory
## RdiffBackup
##
## ----- main -----
## run rdiff-backup chrooted inside system mirror
/usr/bin/chroot "${SystemMirrorMountDirectory}" "${RdiffBackup}" --server --restrict-read-only /
local -i rdiffBackupExitCode=${?}
if [[ ${rdiffBackupExitCode} > 0 ]]; then
__msg err "failed running rdiff-backup (exit code: ${rdiffBackupExitCode})"
return 2 # error
fi
return 0 # success
} # runRdiffBackup()
function runRsync() {
## ----- head -----
##
## DESCRIPTION:
## runs rsync in server mode
##
## ARGUMENTS:
## /
##
## GLOBAL VARIABLES USED:
## SystemMirrorMountDirectory
## Rsync
## RsyncArgs
##
## ----- main -----
## run rsync chrooted inside system mirror
/usr/bin/chroot "${SystemMirrorMountDirectory}" "${Rsync}" --server --sender ${RsyncArgs} . /
local -i rsyncExitCode=${?}
case ${rsyncExitCode} in
0)
;;
24)
__msg notice "rsync reported vanished source files during transfer, continuing"
;;
*)
__msg err "failed running rsync (exit code: ${rsyncExitCode}"
return 2 # error
;;
esac
return 0 # success
} # runRsync()