Docker 镜像批量导入导出脚本
docker 批量导出镜像的sh文件,export-images.sh
sh export-img.sh
LIST=“ d235b 201c7a 201c7a”
sh export-img.sh
注意:LIST赋值Docker Images ID ,多个镜像ID通过空格隔离;执行脚本是只会导出定义ID的镜像
export-image.sh内容
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
docker 批量导入镜像的sh文件
- 支持传参数导入
sh import.sh "nacos.tar redis.tar"
- 支持脚本定义导入镜像名称
vi import.sh
STATE="ImgName1 ImgName2 ImgName3 ...."
sh import.sh
- 支持指定目录导入镜像
vi import.sh
TARDIR="/usr/local/bak"
sh import.sh
- 支持镜像导入前备份当前镜像
sh import.sh
Whether to backup the current images[y/n]:[y/Y]
import.sh 内容
#!/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
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
|