导出runner镜像

导出镜像脚本:

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
#!/bin/bash
# export-images.sh - 导出所有Docker镜像为tar文件

set -e

# 配置变量
BACKUP_DIR="/opt/docker-images-backup"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_PATH="$BACKUP_DIR/$TIMESTAMP"

# 创建备份目录
mkdir -p "$BACKUP_PATH"
cd "$BACKUP_PATH"

echo "=== 开始导出Docker镜像 ==="
echo "备份目录: $BACKUP_PATH"

# 定义要导出的镜像列表
IMAGES=(
# GitLab Auto DevOps 相关镜像
"registry.gitlab.com/gitlab-org/cluster-integration/auto-build-image:v4.12.1"

# 安全扫描镜像
"registry.gitlab.com/security-products/secrets:7"
"registry.gitlab.com/security-products/semgrep:6"
"registry.gitlab.com/security-products/container-scanning:8"
"registry.gitlab.com/gitlab-org/ci-cd/codequality:0.96.0-gitlab.1"

# 基础运行环境
"node:18-alpine"
"alpine/git:latest"
"docker:20.10.12"
"docker:20.10.12:dind"
"bitnami/git:latest"
"gliderlabs/herokuish:latest"

# GitLab Runner 辅助镜像
"registry.gitlab.com/gitlab-org/gitlab-runner/gitlab-runner-helper:x86_64-v18.5.0"
)

# 导出镜像函数
export_image() {
local image=$1
if docker image inspect "$image" > /dev/null 2>&1; then
echo "导出镜像: $image"
# 将镜像名转换为文件名(替换特殊字符)
local filename=$(echo "$image" | sed 's/[\/:]/-/g').tar
docker save -o "$filename" "$image"

# 计算文件大小
local size=$(du -h "$filename" | cut -f1)
echo "✅ 已导出: $filename ($size)"
else
echo "⚠️ 镜像不存在: $image"
fi
}

# 导出所有镜像
for image in "${IMAGES[@]}"; do
export_image "$image"
done

# 创建镜像列表文件
echo "创建镜像列表..."
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.ID}}\t{{.Size}}" > images-list.txt

# 创建恢复脚本
cat > restore-images.sh << 'EOF'
#!/bin/bash
# restore-images.sh - 自动恢复Docker镜像

set -e

echo "=== 开始恢复Docker镜像 ==="

for file in *.tar; do
if [[ -f "$file" ]]; then
echo "导入镜像: $file"
docker load -i "$file"
fi
done

echo "✅ 所有镜像导入完成"
echo "镜像列表:"
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}\t{{.CreatedSince}}"
EOF

chmod +x restore-images.sh

# 创建验证脚本
cat > verify-images.sh << 'EOF'
#!/bin/bash
# verify-images.sh - 验证镜像是否成功导入

set -e

echo "=== 验证Docker镜像 ==="

IMAGES=(
"node:18-alpine"
"alpine:latest"
"docker:latest"
)

for image in "${IMAGES[@]}"; do
if docker image inspect "$image" > /dev/null 2>&1; then
echo "✅ $image - 存在"
else
echo "❌ $image - 缺失"
fi
done

echo "=== 验证完成 ==="
EOF

chmod +x verify-images.sh

# 生成摘要信息
echo "生成备份摘要..."
total_size=$(du -sh . | cut -f1)
image_count=$(find . -name "*.tar" | wc -l)

cat > README.txt << EOF
Docker镜像备份
===============

备份时间: $(date)
备份目录: $(pwd)
镜像数量: $image_count
总大小: $total_size

包含镜像:
$(docker images --format "- {{.Repository}}:{{.Tag}} ({{.Size}})" | grep -E "$(printf "%s|" "${IMAGES[@]}" | sed 's/|$//')")

恢复步骤:
1. 将整个目录复制到目标机器
2. 运行: ./restore-images.sh
3. 验证: ./verify-images.sh

EOF

echo "=== 导出完成 ==="
echo "备份位置: $BACKUP_PATH"
echo "镜像数量: $image_count"
echo "总大小: $total_size"
echo ""
echo "恢复命令: cd $BACKUP_PATH && ./restore-images.sh"