forked from shuguangnet/docker_backup_script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup-utils.sh
More file actions
executable file
·498 lines (429 loc) · 11.4 KB
/
backup-utils.sh
File metadata and controls
executable file
·498 lines (429 loc) · 11.4 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
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
#!/bin/bash
# Docker备份工具函数库
# 作者: Docker Backup Tool
# 版本: 1.0
# 描述: 提供备份脚本使用的通用函数
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# 日志级别
LOG_LEVEL_ERROR=1
LOG_LEVEL_WARNING=2
LOG_LEVEL_INFO=3
LOG_LEVEL_DEBUG=4
# 默认日志级别
CURRENT_LOG_LEVEL=${CURRENT_LOG_LEVEL:-$LOG_LEVEL_INFO}
# 获取时间戳
get_timestamp() {
date '+%Y-%m-%d %H:%M:%S'
}
# 日志记录函数
log() {
local level="$1"
local color="$2"
local message="$3"
local timestamp=$(get_timestamp)
echo -e "${color}[${timestamp}] [${level}] ${message}${NC}" >&2
}
# 错误日志
log_error() {
if [[ $CURRENT_LOG_LEVEL -ge $LOG_LEVEL_ERROR ]]; then
log "ERROR" "$RED" "$1"
fi
}
# 警告日志
log_warning() {
if [[ $CURRENT_LOG_LEVEL -ge $LOG_LEVEL_WARNING ]]; then
log "WARN" "$YELLOW" "$1"
fi
}
# 信息日志
log_info() {
if [[ $CURRENT_LOG_LEVEL -ge $LOG_LEVEL_INFO ]]; then
log "INFO" "$BLUE" "$1"
fi
}
# 成功日志
log_success() {
if [[ $CURRENT_LOG_LEVEL -ge $LOG_LEVEL_INFO ]]; then
log "SUCCESS" "$GREEN" "$1"
fi
}
# 调试日志
log_debug() {
if [[ $CURRENT_LOG_LEVEL -ge $LOG_LEVEL_DEBUG ]]; then
log "DEBUG" "$CYAN" "$1"
fi
}
# 检查命令是否存在
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# 检查文件是否存在且可读
file_readable() {
[[ -f "$1" && -r "$1" ]]
}
# 检查目录是否存在且可写
dir_writable() {
[[ -d "$1" && -w "$1" ]]
}
# 创建目录(如果不存在)
ensure_dir() {
local dir="$1"
if [[ ! -d "$dir" ]]; then
mkdir -p "$dir" || {
log_error "无法创建目录: $dir"
return 1
}
log_debug "创建目录: $dir"
fi
}
# 获取文件大小(人类可读格式)
get_file_size() {
if [[ -f "$1" ]]; then
if command_exists du; then
du -sh "$1" 2>/dev/null | cut -f1
else
ls -lh "$1" 2>/dev/null | awk '{print $5}'
fi
else
echo "0B"
fi
}
# 获取目录大小(人类可读格式)
get_dir_size() {
if [[ -d "$1" ]]; then
if command_exists du; then
du -sh "$1" 2>/dev/null | cut -f1
else
echo "unknown"
fi
else
echo "0B"
fi
}
# 检查磁盘空间是否足够
check_disk_space() {
local path="$1"
local required_mb="$2" # 以MB为单位
if command_exists df; then
local available_kb=$(df "$path" | tail -1 | awk '{print $4}')
local available_mb=$((available_kb / 1024))
if [[ $available_mb -lt $required_mb ]]; then
log_warning "磁盘空间不足: 需要 ${required_mb}MB,可用 ${available_mb}MB"
return 1
fi
log_debug "磁盘空间检查通过: 需要 ${required_mb}MB,可用 ${available_mb}MB"
return 0
else
log_warning "无法检查磁盘空间:df命令不可用"
return 0 # 假设有足够空间
fi
}
# 验证Docker容器名称
validate_container_name() {
local name="$1"
# Docker容器名称规则:只能包含字母、数字、下划线、点和连字符
if [[ ! "$name" =~ ^[a-zA-Z0-9][a-zA-Z0-9_.-]*$ ]]; then
log_error "无效的容器名称: $name"
return 1
fi
return 0
}
# 检查容器是否存在
container_exists() {
local container="$1"
docker ps -a --format "{{.Names}}" | grep -q "^${container}$"
}
# 检查容器是否正在运行
container_running() {
local container="$1"
docker ps --format "{{.Names}}" | grep -q "^${container}$"
}
# 获取容器状态
get_container_status() {
local container="$1"
docker inspect --format='{{.State.Status}}' "$container" 2>/dev/null || echo "not found"
}
# 安全删除文件
safe_remove() {
local file="$1"
if [[ -f "$file" ]]; then
rm -f "$file" && log_debug "删除文件: $file"
fi
}
# 安全删除目录
safe_remove_dir() {
local dir="$1"
if [[ -d "$dir" ]]; then
rm -rf "$dir" && log_debug "删除目录: $dir"
fi
}
# 创建临时目录
create_temp_dir() {
local prefix="${1:-docker-backup}"
local temp_dir
if command_exists mktemp; then
temp_dir=$(mktemp -d -t "${prefix}.XXXXXX")
else
temp_dir="/tmp/${prefix}.$$"
mkdir -p "$temp_dir"
fi
echo "$temp_dir"
}
# 清理临时文件和目录
cleanup_temp() {
local temp_path="$1"
if [[ -n "$temp_path" && "$temp_path" == /tmp/* ]]; then
safe_remove_dir "$temp_path"
log_debug "清理临时目录: $temp_path"
fi
}
# 压缩文件或目录
compress_path() {
local source="$1"
local target="$2"
local compression="${3:-gzip}" # gzip, bzip2, xz
if [[ ! -e "$source" ]]; then
log_error "压缩源不存在: $source"
return 1
fi
local tar_opts=""
case "$compression" in
gzip|gz)
tar_opts="-czf"
[[ "$target" != *.tar.gz ]] && target="${target}.tar.gz"
;;
bzip2|bz2)
tar_opts="-cjf"
[[ "$target" != *.tar.bz2 ]] && target="${target}.tar.bz2"
;;
xz)
tar_opts="-cJf"
[[ "$target" != *.tar.xz ]] && target="${target}.tar.xz"
;;
*)
log_error "不支持的压缩格式: $compression"
return 1
;;
esac
if [[ -d "$source" ]]; then
tar $tar_opts "$target" -C "$(dirname "$source")" "$(basename "$source")" 2>/dev/null
else
tar $tar_opts "$target" -C "$(dirname "$source")" "$(basename "$source")" 2>/dev/null
fi
if [[ $? -eq 0 ]]; then
log_debug "压缩完成: $source -> $target"
return 0
else
log_error "压缩失败: $source"
return 1
fi
}
# 解压文件
decompress_file() {
local archive="$1"
local target_dir="$2"
if [[ ! -f "$archive" ]]; then
log_error "压缩文件不存在: $archive"
return 1
fi
ensure_dir "$target_dir" || return 1
local tar_opts=""
case "$archive" in
*.tar.gz|*.tgz)
tar_opts="-xzf"
;;
*.tar.bz2|*.tbz2)
tar_opts="-xjf"
;;
*.tar.xz|*.txz)
tar_opts="-xJf"
;;
*.tar)
tar_opts="-xf"
;;
*)
log_error "不支持的压缩格式: $archive"
return 1
;;
esac
tar $tar_opts "$archive" -C "$target_dir" 2>/dev/null
if [[ $? -eq 0 ]]; then
log_debug "解压完成: $archive -> $target_dir"
return 0
else
log_error "解压失败: $archive"
return 1
fi
}
# 计算文件哈希值
calculate_hash() {
local file="$1"
local algorithm="${2:-sha256}" # md5, sha1, sha256, sha512
if [[ ! -f "$file" ]]; then
log_error "文件不存在: $file"
return 1
fi
case "$algorithm" in
md5)
if command_exists md5sum; then
md5sum "$file" | cut -d' ' -f1
elif command_exists md5; then
md5 -q "$file"
else
log_error "MD5工具不可用"
return 1
fi
;;
sha1)
if command_exists sha1sum; then
sha1sum "$file" | cut -d' ' -f1
elif command_exists shasum; then
shasum -a 1 "$file" | cut -d' ' -f1
else
log_error "SHA1工具不可用"
return 1
fi
;;
sha256)
if command_exists sha256sum; then
sha256sum "$file" | cut -d' ' -f1
elif command_exists shasum; then
shasum -a 256 "$file" | cut -d' ' -f1
else
log_error "SHA256工具不可用"
return 1
fi
;;
sha512)
if command_exists sha512sum; then
sha512sum "$file" | cut -d' ' -f1
elif command_exists shasum; then
shasum -a 512 "$file" | cut -d' ' -f1
else
log_error "SHA512工具不可用"
return 1
fi
;;
*)
log_error "不支持的哈希算法: $algorithm"
return 1
;;
esac
}
# 验证JSON格式
validate_json() {
local file="$1"
if [[ ! -f "$file" ]]; then
log_error "JSON文件不存在: $file"
return 1
fi
if command_exists jq; then
jq empty "$file" >/dev/null 2>&1
return $?
elif command_exists python3; then
python3 -m json.tool "$file" >/dev/null 2>&1
return $?
elif command_exists python; then
python -m json.tool "$file" >/dev/null 2>&1
return $?
else
log_warning "无法验证JSON格式:缺少jq或python工具"
return 0 # 假设格式正确
fi
}
# 格式化字节大小
format_bytes() {
local bytes="$1"
local units=("B" "KB" "MB" "GB" "TB" "PB")
local unit_index=0
while [[ $bytes -ge 1024 && $unit_index -lt $((${#units[@]} - 1)) ]]; do
bytes=$((bytes / 1024))
((unit_index++))
done
echo "${bytes}${units[$unit_index]}"
}
# 显示进度条
show_progress() {
local current="$1"
local total="$2"
local width="${3:-50}"
local prefix="${4:-Progress}"
local percentage=$((current * 100 / total))
local filled=$((current * width / total))
local empty=$((width - filled))
printf "\r%s: [" "$prefix"
printf "%*s" "$filled" | tr ' ' '='
printf "%*s" "$empty" | tr ' ' '-'
printf "] %d%%" "$percentage"
if [[ $current -eq $total ]]; then
echo
fi
}
# 等待用户确认
ask_confirmation() {
local message="$1"
local default="${2:-n}" # y或n
while true; do
if [[ "$default" == "y" ]]; then
echo -n "$message [Y/n]: "
else
echo -n "$message [y/N]: "
fi
read -r response
response=${response,,} # 转换为小写
case "$response" in
y|yes)
return 0
;;
n|no)
return 1
;;
"")
if [[ "$default" == "y" ]]; then
return 0
else
return 1
fi
;;
*)
echo "请回答 y 或 n"
;;
esac
done
}
# 设置陷阱函数进行清理
setup_cleanup_trap() {
local cleanup_function="$1"
# 在脚本退出时执行清理
trap "$cleanup_function" EXIT
trap "$cleanup_function" INT
trap "$cleanup_function" TERM
}
# 检查必需的工具
check_required_tools() {
local tools=("$@")
local missing_tools=()
for tool in "${tools[@]}"; do
if ! command_exists "$tool"; then
missing_tools+=("$tool")
fi
done
if [[ ${#missing_tools[@]} -gt 0 ]]; then
log_error "缺少必需的工具: ${missing_tools[*]}"
log_info "请安装缺失的工具后重试"
return 1
fi
return 0
}
# 打印分割线
print_separator() {
local char="${1:--}"
local width="${2:-60}"
printf "%*s\n" "$width" | tr ' ' "$char"
}