Skip to content

1.备份

bash
#!/bin/bash
harborUsername='admin'
harborPassword='Harbor12345'
harborRegistry='registry.test.com'
harborBasicAuthToken=$(echo -n "${harborUsername}:${harborPassword}" | base64)
docker login --username ${harborUsername} --password ${harborPassword} ${harborRegistry}
rm -f dist/images.list
rm -f dist/charts.list
# list projects
projs=`curl -s -k -H "Authorization: Basic ${harborBasicAuthToken}" "https://${harborRegistry}"'/api/projects?page=1&page_size=1000' | jq -r '.[] | "\(.project_id)=\(.name)"'`
for proj in ${projs[*]}; do
  projId=`echo $proj|cut -d '=' -f 1`
  projName=`echo $proj|cut -d '=' -f 2`
  # list repos in one project
  repos=`curl -s -k -H "Authorization: Basic ${harborBasicAuthToken}" "https://${harborRegistry}"'/api/repositories?page=1&page_size=1000&project_id='"${projId}" | jq -r '.[] | "\(.id)=\(.name)"'`
  for repo in ${repos[*]}; do
    repoId=`echo $repo|cut -d '=' -f 1`
    repoName=`echo $repo|cut -d '=' -f 2`
    # list tags in one repo
    tags=`curl -s -k -H "Authorization: Basic ${harborBasicAuthToken}" "https://${harborRegistry}"'/api/repositories/'"${repoName}"'/tags?detail=1' | jq -r '.[].name'`
    for tag in ${tags[*]}; do
      #echo ${tag};
      # pull image
      docker pull ${harborRegistry}/${repoName}:${tag}
      # tag image
      docker tag ${harborRegistry}/${repoName}:${tag} ${repoName}:${tag}
      # save image
      mkdir -p $(dirname dist/${repoName})
      docker save -o dist/${repoName}:${tag}.tar  ${repoName}:${tag}
      # record image to list file
      echo "${repoName}:${tag}" >> dist/images.list
    done
  done
  # list charts in one project
  charts=`curl -s -k -H "Authorization: Basic ${harborBasicAuthToken}" "https://${harborRegistry}"'/api/chartrepo/'"${projName}"'/charts' | jq -r '.[].name'`
  for chart in ${charts[*]}; do
    #echo ${chart}
    # list download urls in one chart
    durls=`curl -s -k -H "Authorization: Basic ${harborBasicAuthToken}" "https://${harborRegistry}"'/api/chartrepo/'"${projName}"'/charts/'"${chart}" | jq -r '.[].urls[0]'`
    #echo ${durl[*]}
    for durl in ${durls[*]}; do
        #echo ${durl};
        # download chart
        mkdir -p $(dirname dist/${projName}/${durl})
        curl -s -k -H "Authorization: Basic ${harborBasicAuthToken}" -o dist/${projName}/${durl} "https://${harborRegistry}/chartrepo/${projName}/${durl}"
        # record chart to list file
        echo "${projName}/${durl}" >> dist/charts.list
        
    done
  done
done
#!/bin/bash
harborUsername='admin'
harborPassword='Harbor12345'
harborRegistry='registry.test.com'
harborBasicAuthToken=$(echo -n "${harborUsername}:${harborPassword}" | base64)
docker login --username ${harborUsername} --password ${harborPassword} ${harborRegistry}
rm -f dist/images.list
rm -f dist/charts.list
# list projects
projs=`curl -s -k -H "Authorization: Basic ${harborBasicAuthToken}" "https://${harborRegistry}"'/api/projects?page=1&page_size=1000' | jq -r '.[] | "\(.project_id)=\(.name)"'`
for proj in ${projs[*]}; do
  projId=`echo $proj|cut -d '=' -f 1`
  projName=`echo $proj|cut -d '=' -f 2`
  # list repos in one project
  repos=`curl -s -k -H "Authorization: Basic ${harborBasicAuthToken}" "https://${harborRegistry}"'/api/repositories?page=1&page_size=1000&project_id='"${projId}" | jq -r '.[] | "\(.id)=\(.name)"'`
  for repo in ${repos[*]}; do
    repoId=`echo $repo|cut -d '=' -f 1`
    repoName=`echo $repo|cut -d '=' -f 2`
    # list tags in one repo
    tags=`curl -s -k -H "Authorization: Basic ${harborBasicAuthToken}" "https://${harborRegistry}"'/api/repositories/'"${repoName}"'/tags?detail=1' | jq -r '.[].name'`
    for tag in ${tags[*]}; do
      #echo ${tag};
      # pull image
      docker pull ${harborRegistry}/${repoName}:${tag}
      # tag image
      docker tag ${harborRegistry}/${repoName}:${tag} ${repoName}:${tag}
      # save image
      mkdir -p $(dirname dist/${repoName})
      docker save -o dist/${repoName}:${tag}.tar  ${repoName}:${tag}
      # record image to list file
      echo "${repoName}:${tag}" >> dist/images.list
    done
  done
  # list charts in one project
  charts=`curl -s -k -H "Authorization: Basic ${harborBasicAuthToken}" "https://${harborRegistry}"'/api/chartrepo/'"${projName}"'/charts' | jq -r '.[].name'`
  for chart in ${charts[*]}; do
    #echo ${chart}
    # list download urls in one chart
    durls=`curl -s -k -H "Authorization: Basic ${harborBasicAuthToken}" "https://${harborRegistry}"'/api/chartrepo/'"${projName}"'/charts/'"${chart}" | jq -r '.[].urls[0]'`
    #echo ${durl[*]}
    for durl in ${durls[*]}; do
        #echo ${durl};
        # download chart
        mkdir -p $(dirname dist/${projName}/${durl})
        curl -s -k -H "Authorization: Basic ${harborBasicAuthToken}" -o dist/${projName}/${durl} "https://${harborRegistry}/chartrepo/${projName}/${durl}"
        # record chart to list file
        echo "${projName}/${durl}" >> dist/charts.list
        
    done
  done
done

2.还原

bash
#!/bin/bash
harborUsername='admin'
harborPassword='Harbor12345'
harborRegistry='registry.test.com'
harborBasicAuthToken=$(echo -n "${harborUsername}:${harborPassword}" | base64)
docker login --username ${harborUsername} --password ${harborPassword} ${harborRegistry}
while IFS="" read -r image || [ -n "$image" ]
do
  projName=${image%%/*}
  # echo ${projName}
  # create harbor project
  curl -k -X POST -H "Authorization: Basic ${harborBasicAuthToken}" "https://${harborRegistry}/api/projects" -H "accept: application/json" -H "Content-Type: application/json" -d '{ "project_name": "'"$projName"'", "metadata": { "public": "true" }}'
  # load image
  docker load -i dist/${image}.tar
  # tag image
  docker tag ${image} ${harborRegistry}/${image}
  # push image
  docker push ${harborRegistry}/${image}
done < dist/images.list
while IFS="" read -r chart || [ -n "$chart" ]
do
  projName=${chart%%/*}
  # echo ${projName}
  # create harbor project
  curl -k -X POST -H "Authorization: Basic ${harborBasicAuthToken}" "https://${harborRegistry}/api/projects" -H "accept: application/json" -H "Content-Type: application/json" -d '{ "project_name": "'"$projName"'", "metadata": { "public": "true" }}'
  # upload chart
  curl -s -k -H "Authorization: Basic ${harborBasicAuthToken}" -X POST "https://${harborRegistry}/api/chartrepo/${projName}/charts" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "chart=@dist/${chart};type=application/gzip"
done < dist/charts.list
#!/bin/bash
harborUsername='admin'
harborPassword='Harbor12345'
harborRegistry='registry.test.com'
harborBasicAuthToken=$(echo -n "${harborUsername}:${harborPassword}" | base64)
docker login --username ${harborUsername} --password ${harborPassword} ${harborRegistry}
while IFS="" read -r image || [ -n "$image" ]
do
  projName=${image%%/*}
  # echo ${projName}
  # create harbor project
  curl -k -X POST -H "Authorization: Basic ${harborBasicAuthToken}" "https://${harborRegistry}/api/projects" -H "accept: application/json" -H "Content-Type: application/json" -d '{ "project_name": "'"$projName"'", "metadata": { "public": "true" }}'
  # load image
  docker load -i dist/${image}.tar
  # tag image
  docker tag ${image} ${harborRegistry}/${image}
  # push image
  docker push ${harborRegistry}/${image}
done < dist/images.list
while IFS="" read -r chart || [ -n "$chart" ]
do
  projName=${chart%%/*}
  # echo ${projName}
  # create harbor project
  curl -k -X POST -H "Authorization: Basic ${harborBasicAuthToken}" "https://${harborRegistry}/api/projects" -H "accept: application/json" -H "Content-Type: application/json" -d '{ "project_name": "'"$projName"'", "metadata": { "public": "true" }}'
  # upload chart
  curl -s -k -H "Authorization: Basic ${harborBasicAuthToken}" -X POST "https://${harborRegistry}/api/chartrepo/${projName}/charts" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "chart=@dist/${chart};type=application/gzip"
done < dist/charts.list