脚本:Auto_docker_pull_tag_save.sh
#!/bin/bash
## Author: dongge 2021-12-10 15:05
## 根据镜像列表文件,自动到harbor拉取镜像,并根据给出的前缀自动打tag,最后自动保存镜像为tgz文件
## 参数说明:$1表示镜像列表文件名,其组织的格式如下所示,$2表示目标tag的前缀,即repositry名,默认:registry.cluster.local:30443
## 如想将registry.dongge.cn/amd64/zookeeper:3.5.8拉下来并打上tag为registry.cluster.local:30443/zookeeper/zookeeper:3.5.8,最后保存为zookeeper_3.5.8.tgz步骤如下:
## 1. 新建一个文本文件,如repo.txt
## 2. 向新建的repo.txt文件中写入内容为:registry.dongge.cn/amd64/zookeeper:3.5.8->registry.cluster.local:30443/zookeeper/zookeeper:3.5.8
## 3. 注意:1)如果有多组镜像,每组镜像需另起一行; 2)每一组的"->"符号前后无空格;3)如果仅需要从harbor拉镜像,并使用registry.cluster.local:30443为前缀打tag,如:
## 将registry.dongge.cn/amd64/jdk:1.8.0拉下来,并打上:registry.cluster.local:30443/amd64/jdk:1.8.0,则只需在repo.txt中写registry.dongge.cn/amd64/jdk:1.8.0
## 对于第 "3)" 注意:多个镜像,每个镜像需要另起一行。4)示例文件参考repo_sample.txt
splitor='->'
repo_prefix=$2
if [ -z "$repo_prefix" ];then
repo_prefix='registry.cluster.local:30443'
fi
while read line
do
if [[ $line =~ $splitor && $line != \#* ]];then
a=${line%->*}
b=${line##*->}
docker pull $a
docker tag $a $b
c=${b##*/}
d=${c/:/_}
docker save -o $d.tgz $b
elif [[ $line && $line != \#* ]];then
suffix=${line#*/}
docker pull $line
docker tag $line $repo_prefix/$suffix
c=${line##*/}
d=${c/:/_}
docker save -o $d.tgz $line
fi
done < $1
镜像列表文件:repo_sample.txt
##目标tag后缀与源部分一致,部分不一致的情况
## 说明:直接使用此文件经过测试可以直接在docker_pull_tag_save.sh执行
registry.dongge.cn/amd64/busybox:1.31.1->registry.cluster.local:30443/cetc10/busybox:1.31.1
registry.dongge.cn/elasticsearch/elasticsearch-oss:7.9.3
registry.dongge.cn/justwatch/elasticsearch_exporter:1.0.2
registry.dongge.cn/jaegertracing/jaeger-collector-amd64:1.24.0->registry.cluster.local:30443/jaegertracing/jaeger-collector:1.24.0
registry.dongge.cn/jaegertracing/jaeger-query-amd64:1.24.0->registry.cluster.local:30443/jaegertracing/jaeger-query:1.24.0
registry.dongge.cn/jaegertracing/jaeger-agent-amd64:1.24.0->registry.cluster.local:30443/jaegertracing/jaeger-agent:1.24.0
registry.dongge.cn/kafka/kafka-amd64:2.8.0->registry.cluster.local:30443/kafka/kafka:2.8.0
registry.dongge.cn/spdigital/prometheus-jmx-exporter-kubernetes:0.3.1
registry.dongge.cn/minio/minio:RELEASE.2020-12-29T23-29-29Z
registry.dongge.cn/cetc10/init-nacos-amd64:1.0.1->registry.cluster.local:30443/cetc10/init-nacos_maridb:1.0.1
registry.dongge.cn/nacos/nacos-peer-finder-plugin:1.1
registry.dongge.cn/nacos/nacos-server:2.0.3
registry.dongge.cn/amd64/redis:6.0.5->registry.cluster.local:30443/cetc10/redis:6.0.5
registry.dongge.cn/oliver006/redis_exporter:v1.12.0
registry.dongge.cn/leominov/redis_sentinel_exporter:1.7.1
registry.dongge.cn/cetc10/init-xxl-job-amd64:2.3.2->registry.cluster.local:30443/cetc10/init-xxl-job_maridb:2.3.2
registry.dongge.cn/amd64/xxl-job-admin:2.3.0->registry.cluster.local:30443/cetc10/xxl-job-admin:2.3.1
registry.dongge.cn/amd64/zookeeper:3.5.8->registry.cluster.local:30443/zookeeper/zookeeper:3.5.8
执行:
$ chmod +x Auto_docker_pull_tag_save.sh
$ ./Auto_docker_pull_tag_save.sh repo_sample.txt