sh 用于将所有docker镜像备份到文件的bash脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 用于将所有docker镜像备份到文件的bash脚本相关的知识,希望对你有一定的参考价值。

#!/usr/bin/sh
if [[ $# -eq 0 ]]; then
	echo "Usage: $0 <backup_dir>"
	exit
fi

destination=$1
if ! type "pv" > /dev/null; then
	echo 'pv' command not found on your system, install it to get a nice progress indicator...
else
       PV=" pv "	
fi

docker images | awk -v destination="$destination" -v PV="$PV" '{
if ($1 != "REPOSITORY") 
	{ 
		original = $1;
		size = $5;
		gsub("/","_",$1);
		normalised_container_name = $1
		print "Container: "original" => "destination"/"normalised_container_name".tgz";
		system("docker save "original" | "PV" | gzip -c > "destination"/"normalised_container_name".tgz")
	};
}'



containers=$(ls $destination/*.tgz)

for container in $containers
do
	normalised_container_name=${container//\//_}
	echo "Container: $container => $1/$normalised_container_name.tgz"
	if [[ -s $1/$normalised_container_name.tgz ]]; then
		echo "File $1/$normalised_container_name already exists with filesize > 0, skipping backup..."
		continue
	fi
	
done

#!/usr/bin/sh
if [[ $# -eq 0 ]]; then
	echo "Usage: $0 <backup_dir>"
	exit
fi

containers=$(ls $1/*.tgz)

if ! type "pv" > /dev/null; then
	echo 'pv' command not found on your system, install it to get a nice progress indicator...
else
       PV=" pv "	
fi

for container in $containers
do
	stripped_container=$(basename $container .tgz)
	echo "Container: $container => ${stripped_container//_/\/}"
	gzip -d -c $container | $PV | docker load 
done

以上是关于sh 用于将所有docker镜像备份到文件的bash脚本的主要内容,如果未能解决你的问题,请参考以下文章

Docker 镜像批量备份(导出)

sh 用于清理docker镜像,容器和停止运行容器的文件

Docker镜像备份与迁移

Docker的备份和迁移

使用Docker迁移与备份

Docker教程系列八:Docker备份与迁移(完)