脚本作用
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 "\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
感谢大家的浏览!!!
原文地址:https://blog.51cto.com/scorpions/2414948
时间: 2024-10-09 18:50:04