作用
主要目的用于批量镜像导入;
1.支持传参数导入
实例:
sh import.sh "pause_3.1.tar etcd_3.3.10.tar"
注意:多个tar文件使用空格分隔;
2.支持脚本定义导入镜像名称
实例:
vi import.sh
STATE="ImgName1 ImgName2 ImgName3 ...."
sh import.s
3.支持指定目录导入镜像
实例:
vi import.sh
TARDIR="/usr/local/bak"
sh import.sh
4.支持镜像导入前备份当前镜像
sh import.sh
Whether to backup the current images[y/n]:[y/Y]
注意:
需要备份时输入字母Y,不区分大小写,不需要备份没有严格的指定,只要不是字母Y都可以作为否定备份;
如果同时在脚本中指定导入镜像,又在指定脚本时指定导入镜像名称,默认使用以脚本定义导入镜像名称;
脚本内容:
#!/bin/sh
STATE=""
RECEIVE=$1
TARDIR="/root"
SUFFIX="*.tar"
LIST=`ls $TARDIR/$SUFFIX`
LOGFILE=$TARDIR/import.error.`date +%Y%m%d`.log
BAKLOGFILE=$TARDIR/bak.`date +%Y%m%d`.log
STATEIMPORT() {
for i in $STATE
do
/usr/bin/docker load -i $i >/dev/null 2>>$LOGFILE
done
}
RECEIVEIMPORT() {
for i in $RECEIVE
do
/usr/bin/docker load -i $i >/dev/null 2>>$LOGFILE
done
}
LISTIMPORT() {
for i in $LIST
do
/usr/bin/docker load -i $i >/dev/null 2>>$LOGFILE
done
}
IMAGESBAK(){
IMGINFO=`docker images |awk ‘{print $1,$2,$3}‘|sed 1d >> $TARDIR/tmp.txt`
RESLIST=`/usr/bin/cat $TARDIR/tmp.txt |awk ‘{print $1}‘ `
for i in $RESLIST
do
RESTAG=`docker images |grep "$i" |awk ‘{a=$1":"$2;print a }‘`
BAKNAME=`docker images |grep "$i" |awk ‘{a=$1":"$2;print a }‘|sed ‘s/\//_/g‘`
/usr/bin/docker save $RESTAG -o $TARDIR/$BAKNAME_`date +%Y%m%d`.tar >/dev/null 2>>$BAKLOGFILE
done
if [ -s $BAKLOGFILE ]
then
echo -e "\033[31mERROR:Images Backup Failed!\033[0m"
echo -e "\033[31mPlease View The Log Lile : $BAKLOGFILE\033[0m"
else
/usr/bin/rm -f $BAKLOGFILE
fi
/usr/bin/rm -f $TARDIR/tmp.txt
}
/usr/bin/rm -f $TARDIR/*.log
read -p "Whether to backup the current images[y/n]:" INPUT
if [[ $INPUT = "y" ]] || [[ $INPUT = "Y" ]]
then
IMAGESBAK
else
if [[ -n "$RECEIVE" ]] || [[ -n "$STATE" ]]
then
if [ -n "$RECEIVE" ]
then
RECEIVEIMPORT
else
STATEIMPORT
fi
else
LISTIMPORT
fi
fi
##ERROR Output
if [ -s $LOGFILE ]
then
echo -e "\033[31mERROR:Images Import 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/2415715
时间: 2024-10-09 18:50:05