Skip to content

Commit d3d5408

Browse files
committed
feat: Add exclusion image backups
1 parent a74d15d commit d3d5408

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

backup.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ LOG_LEVEL=3
2929
# 是否默认启用完整备份模式(包含镜像)
3030
DEFAULT_FULL_BACKUP=false
3131

32+
# 是否默认排除镜像备份
33+
DEFAULT_EXCLUDE_IMAGES=false
34+
3235
# 是否默认排除数据卷备份
3336
DEFAULT_EXCLUDE_VOLUMES=false
3437

docker-backup.sh

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ show_usage() {
3737
-v, --verbose 详细输出模式
3838
--exclude-volumes 排除数据卷备份
3939
--exclude-mounts 排除挂载点备份
40+
--exclude-images 排除镜像备份
4041
4142
示例:
4243
$0 nginx mysql # 备份指定容器
4344
$0 -a # 备份所有运行中的容器
4445
$0 -f nginx # 完整备份nginx容器(包含镜像)
46+
$0 --exclude-images nginx # 备份nginx容器但排除镜像
4547
$0 -o /backup nginx # 指定备份目录
4648
4749
EOF
@@ -57,6 +59,7 @@ parse_arguments() {
5759
VERBOSE=false
5860
EXCLUDE_VOLUMES=false
5961
EXCLUDE_MOUNTS=false
62+
EXCLUDE_IMAGES=false
6063

6164
while [[ $# -gt 0 ]]; do
6265
case $1 in
@@ -92,6 +95,10 @@ parse_arguments() {
9295
EXCLUDE_MOUNTS=true
9396
shift
9497
;;
98+
--exclude-images)
99+
EXCLUDE_IMAGES=true
100+
shift
101+
;;
95102
-*)
96103
log_error "未知选项: $1"
97104
show_usage
@@ -278,6 +285,11 @@ backup_image() {
278285
local container_name="$1"
279286
local backup_dir="$2"
280287

288+
if [[ "${EXCLUDE_IMAGES}" == true ]]; then
289+
log_info "跳过镜像备份(--exclude-images)"
290+
return
291+
fi
292+
281293
if [[ "${FULL_BACKUP}" != true ]]; then
282294
log_info "跳过镜像备份(使用 -f 选项启用完整备份)"
283295
return
@@ -288,10 +300,16 @@ backup_image() {
288300
local image=$(docker inspect --format='{{.Config.Image}}' "${container_name}")
289301
local image_file="${backup_dir}/${container_name}_image.tar"
290302

291-
docker save "${image}" -o "${image_file}"
292-
gzip "${image_file}"
293-
294-
log_success "镜像备份完成: ${image_file}.gz"
303+
if docker save "${image}" -o "${image_file}"; then
304+
if gzip "${image_file}"; then
305+
log_success "镜像备份完成: ${image_file}.gz"
306+
else
307+
log_warning "镜像压缩失败,保留未压缩文件: ${image_file}"
308+
fi
309+
else
310+
log_error "镜像备份失败: ${image}"
311+
return 1
312+
fi
295313
}
296314

297315
# 收集容器日志
@@ -588,7 +606,7 @@ Docker容器备份摘要
588606
- 容器日志 ✓
589607
$([ "${EXCLUDE_MOUNTS}" != true ] && echo "- 挂载点数据 ✓" || echo "- 挂载点数据 ✗ (已排除)")
590608
$([ "${EXCLUDE_VOLUMES}" != true ] && echo "- 数据卷 ✓" || echo "- 数据卷 ✗ (已排除)")
591-
$([ "${FULL_BACKUP}" == true ] && echo "- 容器镜像 ✓" || echo "- 容器镜像 ✗ (未启用完整备份)")
609+
$([ "${EXCLUDE_IMAGES}" != true ] && [ "${FULL_BACKUP}" == true ] && echo "- 容器镜像 ✓" || echo "- 容器镜像 ✗ (已排除或未启用完整备份)")
592610
593611
恢复说明:
594612
1. 将整个备份目录复制到目标服务器
@@ -672,6 +690,27 @@ main() {
672690
if [[ -f "${CONFIG_FILE}" ]]; then
673691
log_info "加载配置文件: ${CONFIG_FILE}"
674692
source "${CONFIG_FILE}"
693+
694+
# 应用配置文件中的默认设置
695+
if [[ "${FULL_BACKUP}" == false && "${DEFAULT_FULL_BACKUP:-false}" == true ]]; then
696+
FULL_BACKUP=true
697+
log_info "根据配置文件启用完整备份模式"
698+
fi
699+
700+
if [[ "${EXCLUDE_VOLUMES}" == false && "${DEFAULT_EXCLUDE_VOLUMES:-false}" == true ]]; then
701+
EXCLUDE_VOLUMES=true
702+
log_info "根据配置文件排除数据卷备份"
703+
fi
704+
705+
if [[ "${EXCLUDE_MOUNTS}" == false && "${DEFAULT_EXCLUDE_MOUNTS:-false}" == true ]]; then
706+
EXCLUDE_MOUNTS=true
707+
log_info "根据配置文件排除挂载点备份"
708+
fi
709+
710+
if [[ "${EXCLUDE_IMAGES}" == false && "${DEFAULT_EXCLUDE_IMAGES:-false}" == true ]]; then
711+
EXCLUDE_IMAGES=true
712+
log_info "根据配置文件排除镜像备份"
713+
fi
675714
fi
676715

677716
# 创建备份根目录

0 commit comments

Comments
 (0)