-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathraidless
765 lines (700 loc) · 23.4 KB
/
raidless
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
#!/usr/bin/env bash
readonly VERSION="v0.0.1"
readonly APP='RaidLess'
readonly SNAPRAIDSHOTS_CONTAINER='.snapraidshots'
readonly SNAPRAID_OPTIONS_WITH_VAL=''
readonly SNAPRAID_TEMP_CONF='/tmp/_raidless.conf'
SNAPRAID_CONF='/etc/snapraid.conf'
MAX_SNAPRAIDSHOT_SN=0
NEW_SNAPRAIDSHOT_SN=1
ALL_NAME_ARRAY=
DATA_NAME_ARRAY=
PARITY_NAME_ARRAY=
DATA_PATH_ARRAY=
PARITY_PATH_ARRAY=
CONTENT_PATH_ARRAY=
ALL_PATH_ARRAY=
# 去掉 -n 和 --snapraid 后的参数,用于传递给 snapraid
RAW_ARGS=
# command 参数
CMD_ARGS=
# options 参数
OPT_ARGS=
OPT_SN=
OPT_SNS=
OPT_FILTER_PATH=
OPT_DISK_NAME=
set -o nounset
set -o pipefail
set -o errexit
set -o errtrace
get_config() {
readonly DATA_NAME_ARRAY="$(cat ${SNAPRAID_CONF} | grep -P "^data\s+[^\s]+\s+[^\s]+$" | awk '{print $2}')"
readonly PARITY_NAME_ARRAY="$(cat ${SNAPRAID_CONF} | grep -P "^parity\s+|^[1-6]-parity\s+.+$" | awk '{print $1}')"
readonly ALL_NAME_ARRAY="${DATA_NAME_ARRAY} ${PARITY_NAME_ARRAY}"
readonly DATA_PATH_ARRAY="$(cat ${SNAPRAID_CONF} | grep -P "^data\s+[^\s]+\s+[^\s]+$" | awk '{gsub("/$","");print $NF}')"
readonly PARITY_PATH_ARRAY="$(cat ${SNAPRAID_CONF} | grep -Po "(?<=^parity\s|^[1-6]-parity\s)(.+)$" | awk '{sub(/^\s+/, ""); gsub(/\/[^\/]+$/, ""); print}')"
readonly ALL_PATH_ARRAY="${DATA_PATH_ARRAY} ${PARITY_PATH_ARRAY}"
readonly CONTENT_PATH_ARRAY="$(cat ${SNAPRAID_CONF} | grep -Po "^content\s+\K.+$")"
}
error() {
printf -- "$APP ERROR: $@\n"
return 1
}
info() {
printf -- "$APP INFO: $@\n"
}
is_number() {
case $1 in
''|*[!0-9]*) return 1 ;;
*) return 0 ;;
esac
}
# 找到返回位置,没找到返回0,最大只能 255
get_index_of_array() {
local __member=$1
shift
local __array="$@"
local __i=0
for __item in $__array; do
__i=$((__i+1))
if [ "$__item" = "$__member" ]; then
return $__i
fi
done
return 0
}
is_member_of_array() {
if get_index_of_array $@ ;then
return 1
else
return 0
fi
}
mod_data_content_snapraid_conf() {
info "Modifying data & content snapraid.config:\n"
local _sn=$1
# 修改 data 为 snapraidshots 路径,parity 不变
local _snapshot_dir="${SNAPRAIDSHOTS_CONTAINER}\/$_sn"
# sed '/^data/s/\/$\|$/\/'${_snapshot_dir}'/' ${SNAPRAID_CONF} | sed -E 's|^([0-9]*-)?parity .*/|\0'${_snapshot_dir}'/|' > ${SNAPRAID_TEMP_CONF}
# 只修改 data 和 处于 data 中的 content
sed '/^data/s/\/$\|$/\/'${_snapshot_dir}'/' "${SNAPRAID_CONF}" | awk -v d="$DATA_PATH_ARRAY" '
BEGIN { split(d, paths, " ")} {
if ($1 == "content") {
for (i in paths) {
if (index($2, paths[i]) == 1) {
gsub("^" paths[i], paths[i] "/'${SNAPRAIDSHOTS_CONTAINER}/$_sn'", $2)
print $0
modified = 1
break
}
}
if (!modified) {
print $0
}
modified = 0
}
else {
print $0
}
}' > "${SNAPRAID_TEMP_CONF}"
cat "${SNAPRAID_TEMP_CONF}"
}
mod_data_parity_content_snapraid_conf() {
info "Modifying data & parity & contentfile in snapraid.config:"
local _sn=$1
# 同时修改 data parity content为 snapraidshots 路径
local _snapshot_dir="${SNAPRAIDSHOTS_CONTAINER}\/$_sn"
# sed '/^data/s/\/$\|$/\/'${_snapshot_dir}'/' ${SNAPRAID_CONF} | sed -E 's|^([0-9]*-)?parity\s+.+/\|content\s+.+/|\0'${_snapshot_dir}'/|' > ${SNAPRAID_TEMP_CONF}
sed '/^data/s/\/$\|$/\/'${_snapshot_dir}'/' "${SNAPRAID_CONF}" | sed -E 's|^([0-9]*-)?parity\s*.*/\|content\s*.*/|\0'${_snapshot_dir}'/|' > ${SNAPRAID_TEMP_CONF}
cat "${SNAPRAID_TEMP_CONF}"
}
####### btrfs operations #######
is_btrfs_subvolume() {
__path=$1
btrfs subvolume show "${__path}" &> /dev/null
}
del_btrfs_subvolume() {
local _subvolume=$1
is_btrfs_subvolume "$_subvolume" && {
btrfs subvolume delete "${_subvolume}"
}
}
print_btrfs_snapraidshots_info() {
local _path=$1
local _index=$2
local _name=$(get_array_element "$ALL_NAME_ARRAY" "$_index")
local _data_str=
is_member_of_array "$_name" "$DATA_NAME_ARRAY" && {
_data_str="[data]\t"
} || {
_name="[$_name]"
}
echo -e "\033[1m$_data_str$_name\t${_path}:\033[0m"
btrfs subv list -oucgst ${_path}/${SNAPRAIDSHOTS_CONTAINER} | awk -v OFS="\t" '{if (NR==1) print "SN",$0; else if (NR==2) print "-",$0; else {match($NF, /'${SNAPRAIDSHOTS_CONTAINER}'\/([0-9]+)/,a); print a[1],$0}}'
#| awk 'NR<=2{print} {buffer[NR%5]=$0} END{for(i=NR-4;i<=NR;i++) print buffer[i%5]}'
}
set_btrfs_property() {
local _subvolume=$1
local _property=$2
local _value=$3
btrfs property set "${_subvolume}" "${_property}" "${_value}"
}
create_btrfs_snapshot() {
local _path=$1
local _snapshot=$2
local _readonly=${3-}
is_btrfs_subvolume "${_snapshot}" || {
rm -fr "${_snapshot}" &> /dev/null || true
btrfs subvolume snapshot ${_readonly} "${_path}" "${_snapshot}"
}
}
create_btrfs_subvolume() {
local _path=$1
is_btrfs_subvolume "${_path}" || {
rm -fr "${_path}" &> /dev/null || true
btrfs subvolume create "${_path}"
}
}
get_btrfs_sns() {
local _path=$1
echo $(btrfs subv list -oucgst "${_path}/${SNAPRAIDSHOTS_CONTAINER}" | awk '{if (NR>2) {match($NF, /'${SNAPRAIDSHOTS_CONTAINER}'\/([0-9]+)/,a); print a[1]}}')
}
get_btrfs_max_sn() {
local _path=$1
echo $(btrfs subv list -oucgst ${_path}/${SNAPRAIDSHOTS_CONTAINER} | awk 'BEGIN {max = 0} {match($NF, /\/'${SNAPRAIDSHOTS_CONTAINER}'\/([0-9]+)$/,a); if (a[1]>max) max=a[1]; fi} END {print max}')
}
reflink_cp() {
local _cp_para=$1
local _source=$2
local _dest=$3
if [ -d "$_source" ]; then
info "cp ${_cp_para} --reflink=always ${_source}/* ${_dest}"
cp ${_cp_para} --reflink=always ${_source}/* ${_dest} && return 0 || return 1
elif [ -f "$_source" ]; then
info "cp ${_cp_para} --reflink=always ${_source} ${_dest}"
cp ${_cp_para} --reflink=always ${_source} ${_dest} && return 0 || return 1
else
info "NO ${_source} exist, please check"
return 1
fi
}
###### end of btrfs operations ######
get_max_sns() {
local _path_item=
local _max_sn_array=
local _num=
for _path_item in $ALL_PATH_ARRAY; do
_max_sn_array="$_max_sn_array $(get_btrfs_max_sn "$_path_item")"
done
for _num in $_max_sn_array; do
# 比较当前元素与最大值
if [ "$_num" -gt "$MAX_SNAPRAIDSHOT_SN" ]; then
MAX_SNAPRAIDSHOT_SN="$_num"
fi
done
[ -z MAX_SNAPRAIDSHOT_SN ] && {
error "Get MAX SN of snapraidshots error! plese check."
}
NEW_SNAPRAIDSHOT_SN=$((MAX_SNAPRAIDSHOT_SN+1))
}
get_array_element() {
local __array="$1"
local __index="$2"
local __i=1
local __item
for __item in $__array; do
[ "$__i" -eq "$__index" ] && {
echo "$__item"
return
}
__i=$((__i + 1))
done
echo ""
}
print_snapraidshots_infos() {
local _path_item=
local _i=0
for _path_item in $ALL_PATH_ARRAY; do
_i=$((_i+1))
print_btrfs_snapraidshots_info "$_path_item" "$_i"
done
}
create_parity_snapraidshots() {
info "Creating parity snapraidshots.."
local _sn=$1
local _parity_path_item=
for _parity_path_item in $PARITY_PATH_ARRAY; do
create_btrfs_snapshot "$_parity_path_item" "${_parity_path_item}/${SNAPRAIDSHOTS_CONTAINER}/${_sn}" -r
done
}
create_data_snapraidshots() {
info "Creating data snapraidshots.."
local _sn=$1
local _data_path_item=
for _data_path_item in $DATA_PATH_ARRAY; do
create_btrfs_snapshot "$_data_path_item" "${_data_path_item}/${SNAPRAIDSHOTS_CONTAINER}/${_sn}"
done
}
set_data_snapraidshots_readonly() {
local _sn=$1
local _realonly=$2
local _data_path_item=
for _data_path_item in $DATA_PATH_ARRAY; do
set_btrfs_property "${_data_path_item}/${SNAPRAIDSHOTS_CONTAINER}/${_sn}" "ro" "${_realonly}"
done
}
# 检查配置文件中的 parity data 是否都是 subvolume,content 是否都在 data/parity 中
check_snap_config_valid() {
local _path_item _content_path_item _is_part_of_data_parity
# 检查 data 和 parity 是否是 subvolume.
for _path_item in $ALL_PATH_ARRAY; do
if echo "$_path_item" | grep -qP '^/.*$' && is_btrfs_subvolume "${_path_item}"; then
is_btrfs_subvolume "${_path_item}/${SNAPRAIDSHOTS_CONTAINER}" || {
error "Data/Parity path: ${_path_item}, NO snapraidshots container exist, use \`raidless init\` to initialization."
}
else
error "Data/Parity path: ${_path_item} is NOT subvolume, please check!"
fi
done
# 检查 content 是否都在 data 和 parity 中
for _content_path_item in $CONTENT_PATH_ARRAY; do
# 判断不为空,是否是绝对路径
if echo "$_path_item" | grep -qP '^/.*$'; then
_is_part_of_data_parity=false
for _path_item in $ALL_PATH_ARRAY; do
echo "$_content_path_item" | grep -qP "^$_path_item" && _is_part_of_data_parity=true
done
$_is_part_of_data_parity || {
error "Content file MUST be a part of Data or Parity! , content file: ${_content_path_item} if not a part of Data or Parity, please check!"
}
else
error "Content file: ${_content_path_item} if not a valid path, please check!"
fi
done
}
# 初始化 data 和 parity 的 snapraidshots 容器,存放备份快照
handle_init() {
info "Initializing snapraidshos.."
local _path_item=
for _path_item in $ALL_PATH_ARRAY; do
create_btrfs_subvolume "${_path_item}/${SNAPRAIDSHOTS_CONTAINER}"
done
}
del_btrfs_subvolumes() {
local _path_item=$1
local _sns=$2
local _sn
for _sn in $_sns; do
del_btrfs_subvolume "${_path_item}/${SNAPRAIDSHOTS_CONTAINER}/${_sn}" || true
done
}
handle_del() {
[ -z "$OPT_SNS" ] && {
error "No valid SN(s), please specify SN with -n, exit."
}
local _data_name=$OPT_DISK_NAME
if [ -z $_data_name ]; then
# 删除指定id所有数据和 parity 的snapraidshot
local _path_item=
for _path_item in $ALL_PATH_ARRAY; do
del_btrfs_subvolumes "${_path_item}" "${OPT_SNS}"
done
else
# 删除指定id特定数据或 parity 的 snapraidshot
local _data_path=
if _data_path=$(get_all_path_by_name "${_data_name}"); then
del_btrfs_subvolumes "${_data_path}" "${OPT_SNS}"
else
error "Not Found the path by data name: $_data_name, please check."
fi
fi
}
cp_snapraid_content_file() {
# 1. 遍历contern array,查询路径是否是属于某个 data 或 parity
# 3. 如果是,截取fix path
# 4. 从最新的snapraidshot中复制到 data 或 parity /fix_path 下
info "Copy content files.."
local _sn=$1
local _content_path_item=
local _data_path_item=
local _fix_path=
for _content_path_item in $CONTENT_PATH_ARRAY; do
for _data_path_item in $DATA_PATH_ARRAY; do
_fix_path=$(echo "$_content_path_item" | grep -oP "^$_data_path_item\K.*") && {
reflink_cp "-p" "${_data_path_item}/${SNAPRAIDSHOTS_CONTAINER}/${_sn}${_fix_path}" "${_content_path_item}"
}
done
done
}
handle_sync() {
# 执行步骤:
# 0. 执行 snapraid diff 确定是否需要 sync,如果存在 -f 选项,强制 sync
# 1. 对 data 创建 可写 snapraidshots
# 2. 修改 snapraid.conf中的 data 和 `content` 为 snapraidshots 路径
# 3. 执行 snapraid sync 操作
# 4. 完成之后,对 parity 创建 只读 snapraidshots
# 5. 对之前可些的 data snapraidshots 设置成 read-only
# 6. 复制 snapraidshots 中的 content 到 data
info "Handle Sync.."
(is_member_of_array '-F' "${OPT_ARGS}" || is_member_of_array '--force-full' "${OPT_ARGS}" ) || {
snapraid -c ${SNAPRAID_CONF} diff && {
info 'No sync needed, exit!'
return 0
}
}
local _sn=$NEW_SNAPRAIDSHOT_SN
# 传入特定 sn, 只针对特定 sn 进行 sync,用于上此次 sync 退出的情况,如果之前正常退出,加上-n 可能会出现一些问题
# [ ! -z $OPT_SN ] && _sn=$OPT_SN
create_data_snapraidshots ${_sn}
mod_data_content_snapraid_conf ${_sn}
snapraid -c "${SNAPRAID_TEMP_CONF}" ${OPT_ARGS} sync
create_parity_snapraidshots ${_sn}
set_data_snapraidshots_readonly ${_sn} "true"
cp_snapraid_content_file ${_sn}
}
get_path_by_name() {
local _name=$1
local _name_array=$2
local _path_array=$3
[ -z "$_name" ] && return 1
get_index_of_array "${_name}" "${_name_array}" && return 1
local _i=$?
local _j=0
local _path_item=
for _path_item in $_path_array; do
_j=$((_j+1))
if [ "$_i" = "$_j" ]; then
echo $_path_item
return 0
fi
done
return 1
}
get_data_path_by_name() {
local _data_name=$1
local _path=
_path=$(get_path_by_name "${_data_name}" "${DATA_NAME_ARRAY}" "${DATA_PATH_ARRAY}") && {
echo $_path
return 0
} || return 1
}
get_all_path_by_name() {
local _data_name=$1
local _path=
_path=$(get_path_by_name "${_data_name}" "${ALL_NAME_ARRAY}" "${ALL_PATH_ARRAY}") && {
echo $_path
return 0
} || return 1
}
fix_use_snapshot() {
# 支持 -d <data 数据卷>; -m <path>:跳过已有文件,恢复已经删除的文件与snaprraid一致,不加参数直接加路径,即恢复文件
local _dest_path=$1
local _data_path=$2
local _fix_path=$3 # 以数据目录为/,相对子目录
# 复制前进行 sync,保证数据安全
# handle_sync
# get_max_sns
info "Use SN.$OPT_SN snapraidshots to fix $_dest_path"
if is_member_of_array '-m' "${OPT_ARGS}" ; then
# -m 选项,只回复已经被删除的文件,即:不覆盖从 snapraidshot 中复制
reflink_cp "-rpiun" "${_data_path}/${SNAPRAIDSHOTS_CONTAINER}/${OPT_SN}${_fix_path}" "${_dest_path}"
else
reflink_cp "-rp" "${_data_path}/${SNAPRAIDSHOTS_CONTAINER}/${OPT_SN}${_fix_path}" "${_dest_path}"
# 有可能覆盖掉 content file ,使用 max sn 的 snapraidshot 中的 content 恢复
cp_snapraid_content_file ${MAX_SNAPRAIDSHOT_SN}
fi
}
# 检测 sn 是否有效: 通过判断遍历非所选 data path 的snapraidshots ,以此检测 sn 是否存在
check_snapraid_sn_valid() {
info "Checking sn valid.."
local _data_path=$1
local _sn=$2
local _sn_array=
for _path_item in $ALL_PATH_ARRAY; do
[ $_path_item != $_data_path ] && {
# 这里是其他的 data 和 parity
_sn_array=$(get_btrfs_sns "$_path_item")
is_member_of_array "${_sn}" "${_sn_array}" || {
# 没有找到相应 SN,显示相应的data/parity路径
error "SN.$_sn snapraidshot NOT find in $_path_item, please check,exit!"
}
}
done
}
fix_use_snapraid() {
# --. 判断 data path 是否有效
# 0. 根据 data path/name 判断给出的 sn 是否有效(判断其他 sn 对应的 data 及 parity 是否存在)
# 1. 根据 data path/name 及 sn 判断对应的 snapraidshot 是否存在,如果存在,解除对应 snapraidshot 中的 ro 属性
# 2. 如果不存在,直接对 data path 创建一个 sn 为 <sn> 的 snapraidshot, 同时需要准备一个content
# 3. 修改 snapraid.conf ,全部指向 snapraidshots
# 4. 执行 fix 操作
# 5. 执行 fix use snapshot 操作
local _data_name=$1
local _data_path=$2
local _fix_path=$3 # 以数据目录为/,相对子目录
local _path_item=
# 检查对于的 sn 中的 snapriadshots 是否可用
check_snapraid_sn_valid "${_data_path}" "${OPT_SN}"
info "Use SN.$_sn SNAPRAID to fix /$_fix_path"
# 对目标 snapraidshot 进行检查,如果是 subvolume 开放写属性,
local {_dest_subvolume}="${_data_path}/${SNAPRAIDSHOTS_CONTAINER}/${OPT_SN}"
if is_btrfs_subvolume "${_dest_subvolume}"; then
set_btrfs_property "${_dest_subvolume}" "ro" false
else
# 不是 subvolume ,删除并则创建 snapshot
create_btrfs_snapshot "${_data_path}" "${_dest_subvolume}"
fi
# 为所有 <sn> 编号的 snapraidshots 开放写权限,主要是为了:snapraid.content.lock
for _path_item in $ALL_PATH_ARRAY; do
set_btrfs_property "${_path_item}/${SNAPRAIDSHOTS_CONTAINER}/${OPT_SN}" "ro" false
done
# 修改 snapraid.conf
mod_data_parity_content_snapraid_conf ${OPT_SN} && \
if is_member_of_array '-d' "${OPT_ARGS}"; then
# -d 跟着 data name
snapraid -c ${SNAPRAID_TEMP_CONF} fix ${OPT_ARGS} "${_data_name}"
else
snapraid -c ${SNAPRAID_TEMP_CONF} fix ${OPT_ARGS} "${_fix_path}"
fi
# 为所有 <sn> 编号的 snapraidshots 关闭写权限
for _path_item in $ALL_PATH_ARRAY; do
set_btrfs_property "${_path_item}/${SNAPRAIDSHOTS_CONTAINER}/${OPT_SN}" "ro" true
done
}
handle_fix() {
# 执行步骤:
# 0. 获得 -<sn> 传入的 sn,否则使用最大的sn,并判断 sn 是否有效
# 1. 通过给出的目标路径,获得目标 data,以及 fix path(用于目标路径减去subvolume部分,及data目录中 数据部分的路径)
# 2. 通过与snapraid中的data进行比较,判断给出的路径是否有效
# 3. 在恢复之前,执行一次 sync,确保数据安全性
# 4. 如果没有存在 --snapraid 参数,使用 snapraid 阵列恢复
# 5. 使用 snapshot 恢复,-m 跳过已经目标目录中已经存在的文件
# 6. 使用 reflink cp 相应 snapraidshots 中的文件到目标目录
# 7. 恢复 content file,防止 cp 时被修改
local _dest_path=$OPT_FILTER_PATH
local _data_name=$OPT_DISK_NAME
[ -z $_dest_path ] && [ -z $_data_name ] && {
error "Please specify a file or directory, exit!"
}
[ -z $OPT_SN ] && OPT_SN=$MAX_SNAPRAIDSHOT_SN
([ $MAX_SNAPRAIDSHOT_SN -le 0 ] || [ $OPT_SN -le 0 ] || [ $OPT_SN -gt $MAX_SNAPRAIDSHOT_SN ]) && {
# 如果没有snapraidshots,或者给出的sn>maxsn,错误
error "Not valid SN or snapraidshots!, exit!"
}
#处理 -d
# is_member_of_array '-d' "$OPT_ARGS" && {
[ ! -z $_data_name ] && {
_dest_path=$(get_data_path_by_name "${_data_name}") || {
error "Not Found the path by data name: $_data_name, please check."
}
}
# 处理相对路径
echo "$_dest_path" | grep -qP '^[^/]' && {
_dest_path="$(realpath $_dest_path)"
}
# 解析 dest 路径
local _data_path=
local _fix_path= # 以数据目录为/,相对子目录
local _data_path_item=
for _data_path_item in $DATA_PATH_ARRAY; do
_fix_path=$(echo "$_dest_path" | grep -oP "^$_data_path_item\K.*") && {
_data_path=$_data_path_item
break
}
done
([ -z $_dest_path ] || [ -z $_data_path ] || [ -z $_fix_path ]) && {
error "Path Invalid! please check."
}
echo name:"$_data_name". dest:"$_dest_path". datapath:"$_data_path". fix:"$_fix_path"
# fix
is_member_of_array '--snapraid' "$OPT_ARGS" && fix_use_snapraid "$_data_name" "$_data_path" "$_fix_path"
fix_use_snapshot "$_dest_path" "$_data_path" "$_fix_path"
}
print_help_info() {
printf -- "$APP $VERSION by lisaac, https://www.github.com/lisaac/raidless
Usage: raidless info|int|sync|fix|del|flush [options]
Commands:
info Print the info of the snapraidshots
init
sync Create snapraidshots for array and synchronize the state of the snapraidshots
fix Fix the array from snapraidshots specifying SN, or using
--snapraid from snapraid array
del Delete the snapraidshot through specified SN and array name
flush Delete the snapraid through specified SNs
Options:
-n SN Specify SN for fix, del or flush, flush command support muti SNs: 1,3,7-10
--snapraid Fix the array from snapraid array insteade of from snapraidshots
"
}
handle_exe_snapraid() {
local _yes=
local _snapraid_conf=$SNAPRAID_CONF
[ ! -z $OPT_SN ] && {
mod_data_content_snapraid_conf ${OPT_SN}
_snapraid_conf=$SNAPRAID_TEMP_CONF
}
echo -en "snapraid -c $_snapraid_conf $RAW_ARGS\nDo you want to execute [y/n]"
read -n 1 _yes
echo
[ $_yes = "y" ] && snapraid -c "$_snapraid_conf" $RAW_ARGS
}
get_para_sn() {
local _paras="$1"
local _para _start _end
_paras=$(echo "$_paras" | tr ',' ' ')
for _para in $_paras; do
# 如果参数包含连字符,则表示为范围
if echo "$_para" | grep -q '[0-9]\+-[0-9]\+'; then
_start=$(echo "$_para" | cut -d'-' -f1)
_end=$(echo "$_para" | cut -d'-' -f2)
# 生成范围内的数字,并添加到结果字符串中
for i in $(seq "$_start" "$_end"); do
OPT_SNS="$OPT_SNS $i"
done
else
# 否则直接添加参数到结果字符串中
OPT_SNS="$OPT_SNS $_para"
OPT_SN="$_para"
fi
done
readonly OPT_SNS
readonly OPT_SN
}
parse_args() {
while [ $# -gt 0 ]; do
case "$1" in
-n)
shift
[[ $1 =~ [0-9,-]+ ]] || error "SN error, please specify right SN(s), etc: 1,3,5-7"
get_para_sn "$1"
;;
-c)
shift
SNAPRAID_CONF="$1";
;;
--snapraid)
OPT_ARGS="$OPT_ARGS $1"
;;
-f|--filter)
RAW_ARGS="$RAW_ARGS $1"
shift
RAW_ARGS="$RAW_ARGS $1"
OPT_FILTER_PATH="$1";
;;
-d|--filter-disk)
RAW_ARGS="$RAW_ARGS $1"
shift
RAW_ARGS="$RAW_ARGS $1"
OPT_DISK_NAME="$1";
;;
-i|--import|-p|--plan|-o|--older-than|-l|--log|-S|--start|-B|--count|-L|--error-limit)
# 原有带值的 参数 都跳过,区别与 command
RAW_ARGS="$RAW_ARGS $1"
shift
RAW_ARGS="$RAW_ARGS $1"
;;
-*)
OPT_ARGS="$OPT_ARGS $1"
RAW_ARGS="$RAW_ARGS $1"
;;
*)
RAW_ARGS="$RAW_ARGS $1"
CMD_ARGS="$CMD_ARGS $1"
;;
esac
shift
done
}
main() {
cmd=${1-}
[ -z $cmd ] && error "No raidless command! please check."
case "$1" in
info)
print_snapraidshots_infos
;;
init)
handle_init
;;
del)
handle_del
;;
sync)
handle_sync
;;
fix)
# 默认使用 subvolume snapshot 进行恢复
# TODO: --snapraid,使用 snapraid 计算并进行恢复
# -<SN> 使用编号为:<SN>的 snapshot 进行恢复, 例:-2,使用编号2的 snapshot,如果有多个,使用最大(时间上最后创建)编号,无此参数或大于最大的编号,将使用最大(时间上最后创建)编号
handle_fix
;;
help)
print_help_info
;;
version|ver)
info "$VERSION"
;;
*)
# info "Unknown option: $1"
# printf -- "ARGS: $RAW_ARGS\n"
# printf -- "COMMAND: $CMD_ARGS\n"
# printf -- "OPTIONS: $OPT_ARGS\n"
# printf -- "SN: $OPT_SN\n"
# printf -- "SNS: $OPT_SNS\n"
handle_exe_snapraid
;;
esac
}
install_snapraid() {
apk add --no-cache btrfs-progs build-base grep && \
wget -O snapraid.tar.gz https://github.com/amadvance/snapraid/releases/download/v12.3/snapraid-12.3.tar.gz && \
tar -xzvf snapraid.tar.gz && \
cd snapraid-*/ && \
./configure --prefix=/usr && \
make && make install && \
cd .. && \
rm -rf snapraid*
}
install_lua() {
apk add --no-cache btrfs-progs build-base grep readline-dev && \
wget -O lua-5.3.0.tar.gz http://www.lua.org/ftp/lua-5.3.0.tar.gz && \
tar -xzf lua-5.3.0.tar.gz && \
cd lua-5.3.0 && \
make linux test && \
make INSTALL_TOP=/usr install
}
install_coreutils() {
# apk del coreutils && \
apk add --no-cache libacl libattr skalibs s6-ipcserver utmps build-base && \
wget -O coreutils-9.5.tar.xz https://ftp.gnu.org/gnu/coreutils/coreutils-9.5.tar.xz && \
tar -xf coreutils-*.tar.xz && \
cd coreutils-*/ && \
./configure --prefix=/usr FORCE_UNSAFE_CONFIGURE=1 && \
make && make install && \
cd .. && \
rm -rf coreutils*
}
check_env() {
command -v snapraid > /dev/null || install_snapraid
command -v btrfs > /dev/null || error 'No btrfs found, please install btrfs-progs.'
command -v realpath > /dev/null || error 'No realpath found, please install realpath.'
}
init() {
[ ! -f "$SNAPRAID_CONF" ] && {
error "NO snapraid config file $SNAPRAID_CONF found, please check."
}
get_config
# handle init
is_member_of_array "init" $CMD_ARGS && {
handle_init
info 'Done.'
exit 0
}
check_snap_config_valid
get_max_sns
}
check_env
parse_args "$@"
init
main $CMD_ARGS
info 'Done.'