Last active
December 11, 2020 04:15
-
-
Save kwonghung-YIP/48ce6a27cfe4e3ac0ea94865945ba2ef to your computer and use it in GitHub Desktop.
[docker -CLI] Backup all images on the docker daemon to tag file, and those images had been pull from your private registry and using by the running containers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
bkupdir="bkup_`date +%Y%m%d_%H%M%S`" | |
restorefile="$bkupdir/restore.sh" | |
log="$bkupdir/backup.log" | |
mkdir $bkupdir | |
echo "#!/bin/bash" > $restorefile | |
chmod u+x $restorefile | |
echo "backup folder: $bkupdir" | |
echo "backup folder: $bkupdir" >> $log | |
i=1 | |
docker ps --format '{{.ID}} {{.Image}} {{.Names}} {{.Label "com.docker.swarm.service.name"}}' | \ | |
grep << namespace of your private registry or other filter here ... >> | \ | |
while read line | |
do | |
ctrid=`echo $line | awk '{print $1}'` | |
imgtag=`echo $line | awk '{print $2}'` | |
ctrname=`echo $line | awk '{print $3}'` | |
svcname=`echo $line | awk '{print $4}'` | |
echo "$i. backup image for container $ctrname" | |
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" >> $log | |
echo -e "$i.\tbackup the image for container..." >> $log | |
imgdigest=`docker container inspect --format "{{.Config.Image}}" $ctrid` | |
imgid=`docker image inspect --format "{{.Id}}" $imgdigest` | |
echo -e "\tcontainer id : $ctrid" >> $log | |
echo -e "\tcontainer name : $ctrname" >> $log | |
echo -e "\tservice name : $svcname" >> $log | |
echo -e "\timage tag : $imgtag" >> $log | |
echo -e "\timage digest : $imgdigest" >> $log | |
echo -e "\timage id : $imgid" >> $log | |
echo -e "\t..." >> $log | |
echo -e "\tdocker tag $imgid $imgtag" >> $log | |
docker tag $imgid $imgtag | |
if [ ! -f "$imgid.tar" ]; then | |
echo -e "\tdocker image save --output $bkupdir/$imgid.tar $imgid" >> $log | |
docker image save --output $bkupdir/$imgid.tar $imgid | |
#touch $bkupdir/$imgid.tar | |
echo "docker load --input $imgid.tar" >> $restorefile | |
fi | |
echo "docker tag $imgid $imgtag" >> $restorefile | |
((i=i+1)) | |
done | |
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" >> $log | |
echo "End of backup log" >> $log |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment