命令行获取Harbor镜像清单

在Harbor服务主机上,可通过如下命令在 registry 存储目录下获取镜像列表:

 

$ grep data_volume  /app/harbor/harbor.yml    #根据配置文件查找数据存储目录
data_volume: /data

$ cd /data/registry    #进入到Harbor的数据目录下
$ find  docker  -type  d  -name "current"  | sed  's|docker/registry/v2/repositories/||g;s|/_manifests/tags/|:|g;s|/current||g'  >  images.list
$ cat images.list
lidabai/busybox:1.28
library/prepare:v2.5.1

 

 Harbor API v2版本的Shell脚本实现

$ cat  Harbor-image-listk-v2.sh
#!/bin/bash
Harbor_Address=192.168.2.250:443       #Harbor主机地址
Harbor_User=admin                      #登录Harbor的用户
Harbor_Passwd=Harbor12345              #登录Harbor的用户密码
Images_File=harbor-images-`date '+%Y-%m-%d'`.txt   # 镜像清单文件
Tar_File=/backup/Harbor-backup/                 #镜像tar包存放路径
set -x
# 获取Harbor中所有的项目(Projects)
Project_List=$(curl -u admin:Harbor12345  -H "Content-Type: application/json" -X GET  https://192.168.2.250:443/api/v2.0/projects  -k  | python -m json.tool | grep name | awk '/"name": /' | awk -F '"' '{print $4}')

for Project in $Project_List;do
   # 循环获取项目下所有的镜像
    Image_Names=$(curl -u admin:Harbor12345 -H "Content-Type: application/json" -X GET https://192.168.2.250:443/api/v2.0/projects/$Project/repositories -k | python -m json.tool | grep name | awk '/"name": /' | awk -F '"' '{print $4}')
    for Image in $Image_Names;do
        # 循环获取镜像的版本(tag)
        Image_Tags=$(curl -u admin:Harbor12345  -H "Content-Type: application/json"   -X GET  https://192.168.2.250:443/v2/$Image/tags/list  -k |  awk -F '"'  '{print $8,$10,$12}')
        for Tag in $Image_Tags;do
            # 格式化输出镜像信息
            echo "$Harbor_Address/$Image:$Tag"   >> harbor-images-`date '+%Y-%m-%d'`.txt
        done
    done
done
$ chmod +x Harbor-image-listk-v2.sh
$ sh Harbor-image-listk-v2.sh

 

 

基于镜像清单将Harbor中的镜像制作成tar包

$ cat image-pull.sh
#!/bin/bash
###使用docker从镜像文件中下载镜像——将下载的镜像进行打包保存——删除下载到本地的镜像——将封装好的镜像包移动到备份目录
Image_tags=$(uniq $Images_File)
for image_tag in $Image_tags;do
    image_Name=$(echo $image_tag | awk -F/ '{print $3}' |  awk -F: '{print $1}')
    image_Lable=$(echo $image_tag | awk -F/ '{print $3}' |  awk -F: '{print $2}')
    docker pull $image_tag
    docker save $image_tag  -o $image_Name-$image_Lable.tar
    docker rmi  $image_tag
    mv $image_Name-$image_Lable.tar  $Tar_File
done
$ chmod +x  image-pull.sh

 

摘自:

https://blog.51cto.com/u_11529070/9176507

posted @ 2024-05-22 09:29  fengjian1585  阅读(777)  评论(0编辑  收藏  举报