Docker image batch import

Effect

It is mainly used for batch image import.

1. Support parameter import

Example:

  sh import.sh "pause_3.1.tar etcd_3.3.10.tar"
  Note: multiple tar files are separated by spaces;

2. Support script definition to import image name

Example:

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

3. Support to import image from specified directory

Example:

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

4. Support the backup of current image before image import

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

Be careful:

When a backup is needed, enter the letter Y, which is not case sensitive and does not need to be specified strictly. As long as it is not the letter Y, it can be used as a negative backup.
If the import image is specified in the script and the import image name is specified in the script, the import image name is defined by script by default.

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 website

Docker image backup (export)

Keywords: Linux Docker

Added by Kaizard on Thu, 31 Oct 2019 16:55:54 +0200