腳本快速批次備份docker檔

透過執令列出所有容器的Name

docker ps -a –format “table {{.Names}}” | grep -Ev ‘^NAMES’ | awk ‘{print $1}’

 

取得清單後利用for迴圈將docker export執令匯出需要的檔

#!/bin/bash
GREEN_COLOR='\e[032m'   #綠
RED_COLOR='\e[031m'     #紅
YELLOW_COLOR='\e[033m'  #黃
BLACK_COLOR='\e[0m'     #黑
date_time=`date`
date_time=$(date '+%Y%m%d')
declare image_name=(`docker ps -a --format "table {{.Names}}" | grep -Ev '^NAMES' | awk '{print $1}'`)


for i in ${!image_name[@]}
do
        docker export ${image_name[i]} > ${image_name[i]}_${date_time}.tar.gz
        if [ $? -eq 0 ];then
                echo -e "${YELLOW_COLOR}${image_name[i]} ${BLACK_COLOR} =====> ${GREEN_COLOR}back finish!!!${BLACK_COLOR} =====> back file is ${RED_COLOR} ${image_name[i]}_${date_time}.tar.gz ${BLACK_COLOR}"
                echo ""
        fi
don

Leave a Comment

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *