-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup
executable file
·417 lines (366 loc) · 10.1 KB
/
backup
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/bin/bash
driver=
opt_n=false
main() {
case "$1" in
backup)
shift 1
extract_opts "$@"
run backup "${opts[0]}" "${args[@]}"
res=$?
banner_results $res
exit $res
;;
list)
shift 1
extract_opts "$@"
run list "${opts[0]}" "${args[@]}"
res=$?
banner_results $res
exit $res
;;
restore)
shift 1
extract_opts "$@"
run restore "${opts[0]}" "${args[@]}"
res=$?
banner_results $res
exit $res
;;
*)
cat <<EOF
USAGE
${0##*/} backup DESCIPTOR [OPTIONS...] [-- [...]]
${0##*/} restore DESCIPTOR [OPTIONS...] [-- [...]]
${0##*/} list DESCIPTOR [OPTIONS...] [-- [...]]
${0##*/} help
NOTES ABOUT OPTIONS
Key-values options are specified by one of these three syntax:
--option=value
--option value
option=value
Boolean options can be specified by one of these syntax:
-option enable option
+option disable option
--no-option disable option
--disable-option disable option
--enable-option enable option
Setting an option that isn't documented can result in undefined behaviour.
GANERAL OPTIONS
-n only print the commant that will be executed, do not run them
EOF
return 1
;;
esac
}
# READ $@ : Extract --VAR=VALUE --VAR VALUE, -BOOLOPT, +BOOLOPT
# WRITE $opt_<OPTNAME> : the option extracted
# WRITE $opts : option output array containing unparsed arguments
# WRITE $args : anything after --
extract_opts(){
opts=()
args=()
local _optname
local _optval
while [[ $# -gt 0 ]]; do
case "$1" in
--)
shift
args=("$@")
break
;;
--*=*)
_optname="${1%%=*}"
_optname="${_optname#--}"
_optval="${1#*=}"
_optval="${_optval//-/_}"
eval "opt_$_optname='${_optval//'/'\"'\"'}'"
shift
;;
--*)
_optname="${1#--}"
_optval="$2"
eval "opt_$_optname='${_optval//'/'\"'\"'}'"
shift 2
;;
--no-*)
_optname="${1#--no-}"
eval "opt_$_optname=false"
shift
;;
--disable-*)
_optname="${1#--disable-}"
eval "opt_$_optname=false"
shift
;;
--enable-*)
_optname="${1#--enable-}"
eval "opt_$_optname=true"
shift
;;
-*)
_optname="${1#-}"
eval "opt_$_optname=true"
shift
;;
+*)
_optname="${1#+}"
eval "opt_$_optname=false"
shift
;;
*)
opts+=("$1")
shift
;;
esac
done
}
# READ $driver
# READ $driver_dir
driver(){
export BACKUP_DRY_RUN=$opt_n
export BACKUP_DIR="$driver_dir/$subdir"
export BACKUP_DRIVER_DIR="$driver_dir"
export BACKUP_DRIVER_SOURCE_DIR="$(readlink -f $driver)"
export BACKUP_DRIVER_SOURCE_DIR="${BACKUP_DRIVER_DIR%/*}"
if "$driver" "$@"; then :; else
local res=$?
banner_error "Driver failed with status $res"
(
echo " driver: $driver"
echo " arguments: $@"
echo " BACKUP_DRY_RUN=$BACKUP_DRY_RUN"
echo " BACKUP_DIR=$BACKUP_DIR"
echo " BACKUP_DRIVER_DIR=$BACKUP_DRIVER_DIR"
echo " BACKUP_DRIVER_SOURCE_DIR=$BACKUP_DRIVER_SOURCE_DIR"
) >&2
return $res
fi
}
# READ $driver_dir : the location where to find the driver
# READ $driver : if different than ":", will recurse parent directories
# to find driver
# WRITE $driver : the driver that was found
# WRITE $driver_dir : where the driver was found
# APPEND $driver_lookup : where the driver was looked up (array)
# WRITE $? : failure if driver was not found
find_driver(){
if [[ -e $driver_dir/driver.sh ]]; then
. "$driver_dir/driver.sh"
return 0
elif [[ -x $driver_dir/driver ]]; then
driver="$driver_dir/driver"
return 0
fi
driver_lookup+=("$driver_dir/driver.sh" "$driver_dir/driver")
if [[ $driver != : ]]; then
local old_driver_dir="$driver_dir"
driver_dir="${driver_dir%/*}"
if [[ $old_driver_dir != $driver_dir ]]; then
if ! find_driver; then
driver_dir="$old_driver_dir"
return 1
else
return 0
fi
fi
fi
return 1
}
# READ $descriptor : the descriptor
# WRITE $type : the descriptor type
get_type(){
# Get the extension
local basename=${descriptor##*/}
type=${basename##*.}
if [[ $type = ${basename} ]] && [[ -d $descriptor ]]; then
# If no extension and is a directory, consider it a sub
type=sub
elif [[ $type == dir ]] && [[ -L $descriptor ]]; then
# Alias *.dir links as link
type=link
elif [[ $type == d ]]; then
# Alias *.d are sub
type=sub
elif [[ $type == md ]]; then
# Alias *.md are txt
type=txt
fi
}
# READ $1 : the action (backup, list)
# READ $2 : the backup descriptor
# READ $3... : the backup options
# READ $driver : the driver to use. If empty, the descriptor must be a directory
run() {
local type
local res=0
local driver=$driver
local driver_change=false
local action="$1"
local descriptor="$2"
local up_driver_dir="$driver_dir"
local driver_dir="${descriptor%/}"
local Action="$(tr a-z A-Z <<<${action:0:1})${action:1}"
shift 2
get_type
# look in the same directory of the driver for anything else but sub
if [[ $type != sub ]]; then
driver_dir="${driver_dir%/?*}"
fi
if [[ $type = sub ]] || [[ $driver = : ]]; then
local old_driver_dir="$driver_dir"
local driver_lookup=()
if find_driver; then
driver init $action || return 1
banner_info "Found driver: $driver"
driver_change=true
elif [[ $driver = : ]]; then
banner_error "$old_driver_dir: Could not find driver in $driver_dir"
for location in "${driver_lookup[@]}"; do
echo " Not found: $location"
done
return 1
fi
fi
if ! $driver_change; then
driver_dir="$up_driver_dir"
fi
if type "run_$type" >/dev/null 2>&1; then
"run_$type" "$@"
((res+=$?))
else
banner_warning "$descriptor: Unknown type $type"
fi
if [[ $res = 0 ]] && $driver_change; then
if ! driver commit $action; then
((res++))
fi
fi
return $res
}
banner_error(){
echo -e "$Bold--> ${BIRed}Error: $1$Color_Off" >&2
}
banner_warning(){
echo -e "$Bold--> ${BIYellow}Warning: $1$Color_Off" >&2
}
banner_action(){
echo -e "$Bold--> $BIGreen$Action $1$Color_Off"
}
banner_info(){
echo -e "$Bold--> $BIGreen$1$Color_Off"
}
banner_results(){
if [[ $1 -eq 0 ]]; then
echo -e "$Bold--> $BIGreen${2:-Success}$Color_Off"
else
echo -e "$Bold--> $BIRed${2:-Failure} ($1)$Color_Off"
fi
}
run_sub(){
local res=0
local up_subdir="$subdir"
local subdir="$up_subdir"
banner_action "$descriptor/..."
for f in "$descriptor"/*; do
[[ ${f##*/} = driver ]] && continue
[[ ${f##*/} = driver.sh ]] && continue
subdir="$up_subdir/${f##*/}"
subdir="${subdir#/}"
run $action "$f" "$@"
((res+=$?))
done
return $res
}
run_link(){
local link="$(readlink "$descriptor")"
if [[ -d $link ]]; then
link="${link%/}/"
fi
banner_action "$descriptor -> $link"
driver $action "$link" "$@"
}
run_exec(){
banner_action "$descriptor (execute)"
"$descriptor" "$action" "$@"
}
run_sh(){
banner_action "$descriptor (plug-in)"
local args=("$@")
. "$descriptor"
}
run_conf(){ : ; }
run_txt(){
true
}
run_md(){
true
}
# Reset
Color_Off='\e[0m' # Text Reset
# Regular Colors
Black='\e[0;30m' # Black
Red='\e[0;31m' # Red
Green='\e[0;32m' # Green
Yellow='\e[0;33m' # Yellow
Blue='\e[0;34m' # Blue
Purple='\e[0;35m' # Purple
Cyan='\e[0;36m' # Cyan
White='\e[0;37m' # White
# Bold
Bold='\e[1m' # No Color Change
BBlack='\e[1;30m' # Black
BRed='\e[1;31m' # Red
BGreen='\e[1;32m' # Green
BYellow='\e[1;33m' # Yellow
BBlue='\e[1;34m' # Blue
BPurple='\e[1;35m' # Purple
BCyan='\e[1;36m' # Cyan
BWhite='\e[1;37m' # White
# Underline
UBlack='\e[4;30m' # Black
URed='\e[4;31m' # Red
UGreen='\e[4;32m' # Green
UYellow='\e[4;33m' # Yellow
UBlue='\e[4;34m' # Blue
UPurple='\e[4;35m' # Purple
UCyan='\e[4;36m' # Cyan
UWhite='\e[4;37m' # White
# Background
On_Black='\e[40m' # Black
On_Red='\e[41m' # Red
On_Green='\e[42m' # Green
On_Yellow='\e[43m' # Yellow
On_Blue='\e[44m' # Blue
On_Purple='\e[45m' # Purple
On_Cyan='\e[46m' # Cyan
On_White='\e[47m' # White
# High Intensity
IBlack='\e[0;90m' # Black
IRed='\e[0;91m' # Red
IGreen='\e[0;92m' # Green
IYellow='\e[0;93m' # Yellow
IBlue='\e[0;94m' # Blue
IPurple='\e[0;95m' # Purple
ICyan='\e[0;96m' # Cyan
IWhite='\e[0;97m' # White
# Bold High Intensity
BIBlack='\e[1;90m' # Black
BIRed='\e[1;91m' # Red
BIGreen='\e[1;92m' # Green
BIYellow='\e[1;93m' # Yellow
BIBlue='\e[1;94m' # Blue
BIPurple='\e[1;95m' # Purple
BICyan='\e[1;96m' # Cyan
BIWhite='\e[1;97m' # White
# High Intensity backgrounds
On_IBlack='\e[0;100m' # Black
On_IRed='\e[0;101m' # Red
On_IGreen='\e[0;102m' # Green
On_IYellow='\e[0;103m' # Yellow
On_IBlue='\e[0;104m' # Blue
On_IPurple='\e[0;105m' # Purple
On_ICyan='\e[0;106m' # Cyan
On_IWhite='\e[0;107m' # White
main "$@"
exit $?