daocke镜像批量导出脚本

脚本作用

1.批量导出Docker Images;
2.部分导出,通过指定Docker Images ID 到脚本“LIST”变量;
3.支持相同ID,不同REPOSITORY名称备份;

例如:

docker images 
kry1702/coredns                      1.3.1               eb516548c180        5 months ago        40.3MB
k8s.gcr.io/coredns                   1.3.1               eb516548c180        5 months ago        40.3MB
注意:备份文件名称格式为:kry1702_coredns:1.3.1.tar
主要是解决相同ID,不同REPOSITORY名称,如果提取“/”最右边为备份文件名称格式导致备份文件冲突,以上为例备份文件名称格式为:coredns:1.3.1.tar 
View Code

运行实例

#导出全部的镜像;
 sh ExportImg.sh
 
 #导出部分镜像
 LIST=“ d235b 201c7a  201c7a”
 sh ExportImg.sh
注意:LIST赋值Docker Images ID ,多个镜像ID通过空格隔离;执行脚本是只会导出定义ID的镜像;
View Code

镜像还原(任选一种)

docker load --input xxx.tar
docker load < xx.tar
docker load -i xx.tar
View Code

脚本内容

LIST=""
TXT=/root/tmp.txt
BAKDIR=/usr/local/bak
LOGDIR=/usr/local/bak/log
LOGFILE=$LOGDIR/bak.`date +%Y%m%d`.log
 
[ ! -d $BAKDIR ] && mkdir -p $BAKDIR
[ ! -d $LOGDIR ] && mkdir -p $LOGDIR
 
if [ -n "$LIST" ]
then
        for list in $LIST
        do
                RESLIST=`docker images |grep $list | awk '{print $1}'`
                for reslist in $RESLIST
                do
                RESTAG=`docker images |grep "$reslist" |awk '{a=$1":"$2;print a }'`
                BAKNAME=`docker images |grep "$reslist" |awk '{a=$1":"$2;print a }'|sed 's/\//_/g'`
                /usr/bin/docker save $RESTAG -o $BAKDIR/$BAKNAME.tar  >> $LOGFILE 2>&1
                done
        done
else
        REC=`docker images |awk '{print $1,$2,$3}'|sed 1d >> $TXT`
        RESLIST=`cat $TXT|awk '{print $1}'`
        for reslist in $RESLIST
        do
                RESTAG=`docker images |grep "$reslist" |awk '{a=$1":"$2;print a }'`
                BAKNAME=`docker images |grep "$reslist" |awk '{a=$1":"$2;print a }'|sed 's/\//_/g'`
                /usr/bin/docker save $RESTAG -o $BAKDIR/$BAKNAME.tar  >> $LOGFILE 2>&1
        done
        /usr/bin/rm -f $TXT
fi
 
if [ -s $LOGFILE ]
then
        echo -e "\033[31mERROR:Images Backup Failed!\033[0m"
        echo -e "\033[31mPlease View The Log Lile : $LOGFILE\033[0m"
else
        /usr/bin/rm -f $LOGFILE
fi
View Code

参考网址

批量导出Docker镜像

 
posted @ 2020-02-22 11:18  沈叶唐  阅读(222)  评论(0编辑  收藏  举报