工作中遇到一个问题,要把某个项目的harbor镜像库镜像全部导出,记录下我的操作方法。harbor版本为2.6.1,请确认harbor版本,每个版本api会有差异。
批量导出harbor某个项目的镜像库脚本
#!/bin/bash
# 基于harbor2.6.1
# 设置Harbor仓库的地址、项目名、用户名和密码
HARBOR_URL="https://reg-hub.guoliangjun.com"
HARBOR_NAME="reg-hub.guoliangjun.com"
PROJECT="guoliangjun"
USERNAME="string"
PASSWORD="string"
# 获取项目中所有镜像的列表
images=$(curl -s -k -u ${USERNAME}:${PASSWORD} "${HARBOR_URL}/api/v2.0/projects/${PROJECT}/repositories?page=1&page_size=100" | jq -r '.[].name')
# 循环拉取每个镜像
for image in ${images}; do
# 除去PROJECT内容
image_sub=${image//${PROJECT}\//}
# url中用%252F替换'/'
image_reg=${image_sub//\//%252F}
artifacts=$(curl -s -k -u ${USERNAME}:${PASSWORD} -X 'GET' "${HARBOR_URL}/api/v2.0/projects/${PROJECT}/repositories/${image_reg}/artifacts?with_tag=true" | jq -r '.[].digest')
for artifact in ${artifacts};do
tags=$(curl -s -k -u ${USERNAME}:${PASSWORD} -X 'GET' "${HARBOR_URL}/api/v2.0/projects/${PROJECT}/repositories/${image_reg}/artifacts/${artifact}/tags?page=1&page_size=10&with_signature=false&with_immutable_status=false" | jq -r '.[].name')
for tag in ${tags};do
echo "docker pull ${HARBOR_NAME}/${image}:${tag}"
docker pull ${HARBOR_NAME}/${image}:${tag}
echo ${HARBOR_NAME}/${image}:${tag} >> /tmp/docker/images-list.txt
echo "docker save ${HARBOR_NAME}/${image}:${tag} > /tmp/docker/${HARBOR_NAME}-${image//\//-}-${tag}.tar.gz"
docker save ${HARBOR_NAME}/${image}:${tag} > /tmp/docker/${HARBOR_NAME}-${image//\//-}-${tag}.tar.gz
#echo "docker rmi -f ${HARBOR_NAME}/${image}:${tag}"
#docker rmi ${HARBOR_NAME}/${image}:${tag}
done
done
done
批量导入到images
for i in `ll /tmp/docker|awk '{print $9}'`;do docker load < $i;done