TEL:
Docker 镜像批量备份(导出)
| 发布时间: 2019-07-08 12:00:30 | 738 次浏览
Docker 镜像批量备份(导出)

脚本作用

1.批量导出Docker Images;

2.部分导出,通过指定Docker Images ID 到脚本“LIST”变量运行实例

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

镜像还原(任选一种)

docke load --input xxx.tar

docker load < xx.tar

docker load -i xx.tar

脚本内容

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
 RES_TAG=`docker images |grep $list|awk ‘{a=$1":"$2;print a }‘`
 NUM=`docker images |grep $list |awk ‘{a=$1":"$2;print a }‘|awk -F"/" ‘{print NF-1}‘`
 if [ $NUM -eq 1 ]
 then
 BAK_NAME=`docker images |grep $list|awk ‘{a=$1":"$2;print a }‘|awk -F"/" ‘{print $2}‘`
 /usr/bin/docker save $RES_TAG -o $BAKDIR/$BAK_NAME.tar >> $LOGFILE 2>&1
 else
 SNUM=$[$NUM + 1 ]
 BAK_NAME=`docker images |grep $list|awk ‘{a=$1":"$2;print a }‘|cut -d ‘/‘ -f $SNUM-`
 /usr/bin/docker save $RES_TAG -o $BAKDIR/$BAK_NAME.tar >> $LOGFILE 2>&1
 fi
 done
else
 REC=`docker images |awk ‘{print $1,$2,$3}‘|sed 1d >> $TXT`
 IDLIST=`cat $TXT|awk ‘{print $3}‘`
 for i in $IDLIST
 do
 RES_TAG=`docker images |grep $i|awk ‘{a=$1":"$2;print a }‘`
 NUM=`docker images |grep $i |awk ‘{a=$1":"$2;print a }‘|awk -F"/" ‘{print NF-1}‘`
 if [ $NUM -eq 1 ]
 then
 BAK_NAME=`docker images |grep $i|awk ‘{a=$1":"$2;print a }‘|awk -F"/" ‘{print $2}‘`
 /usr/bin/docker save $RES_TAG -o $BAKDIR/$BAK_NAME.tar >> $LOGFILE 2>&1
 else
 SNUM=$[$NUM + 1 ]
 BAK_NAME=`docker images |grep $i|awk ‘{a=$1":"$2;print a }‘|cut -d ‘/‘ -f $SNUM-`
 /usr/bin/docker save $RES_TAG -o $BAKDIR/$BAK_NAME.tar >> $LOGFILE 2>&1
 fi
 done
 /usr/bin/rm -f $TXT
fi
if [ -s $LOGFILE ]
then
 echo -e "ERROR:Images Backup Failed!"
 echo -e "Please View The Log Lile : $LOGFILE"
else
 /usr/bin/rm -f $LOGFILE
fi