Forked from dieterrosch/backup_all_docker_images.sh
Last active
January 11, 2023 04:58
-
-
Save pythoninthegrass/1e0c83ed916722457a2ad4a319260170 to your computer and use it in GitHub Desktop.
A bash script to backup all docker images to files
This file contains hidden or 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
#!/usr/bin/env bash | |
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 '{ | |
if ($1 != "REPOSITORY") | |
{ | |
original = $1; | |
size = $5; | |
gsub("/","_",$1); | |
normalised_container_name = $1 | |
print "Container: $original => $destination/normalised_container_name.tar.gz"; | |
system("docker save $original | $PV | gzip -c > $destination/$normalised_container_name.tar.gz") | |
}; | |
}' | |
for container in "$containers" | |
do | |
normalised_container_name=${container//\//_} | |
echo "Container: $container => $1/$normalised_container_name.tar.gz" | |
if [[ -s $1/$normalised_container_name.tar.gz ]]; then | |
echo "File $1/$normalised_container_name already exists with filesize > 0, skipping backup..." | |
continue | |
fi | |
done |
This file contains hidden or 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
#!/usr/bin/env bash | |
if [[ $# -eq 0 ]]; then | |
echo "Usage: $0 <backup_dir>" | |
exit | |
fi | |
containers=$(find "$1" -name "*.tar.gz") | |
# echo "$containers" | |
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 .tar.gz) | |
echo "Container: $container => ${stripped_container//_/\/}" | |
gzip -d -c $container | $PV | docker load | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment