Docker Mirror bulk import

effect

The main purpose of introducing a bulk mirror;

1. Support pass parameters introduced

Example:

  sh import.sh "pause_3.1.tar etcd_3.3.10.tar"
  注意:多个tar文件使用空格分隔;

2. Import mirroring support script defines the name

Example:

  vi import.sh
  STATE="ImgName1 ImgName2 ImgName3 ...."
  sh import.s

3. Support the specified directory to import image

Example:

  vi import.sh
  TARDIR="/usr/local/bak"
  sh import.sh

4. Before the current backup support mirrored image import

  sh import.sh
  Whether to backup the current images[y/n]:[y/Y]

note:

Enter the time required to back up the letter Y, case-insensitive, no backup is not strictly specified, if not the letter Y can be backed up as a negative;
If you specify the import mirrored in the script, and specify the name when imported mirror script specified, the default used in the definitions into mirror script name;

Script Content:

#!/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

Reference Site

Docker image backup (export)

Guess you like

Origin blog.51cto.com/scorpions/2415715