Skip to content

Instantly share code, notes, and snippets.

@dieterrosch
Last active August 11, 2023 19:48
Show Gist options
  • Save dieterrosch/0c8d37201480394fca7567a1cdd5e758 to your computer and use it in GitHub Desktop.
Save dieterrosch/0c8d37201480394fca7567a1cdd5e758 to your computer and use it in GitHub Desktop.

Revisions

  1. dieterrosch revised this gist Nov 11, 2016. 2 changed files with 47 additions and 4 deletions.
    30 changes: 26 additions & 4 deletions backup_all_docker_images.sh
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,37 @@
    containers=$(docker images |awk '{ if ($1 != "REPOSITORY") { print $1};}' )
    #!/usr/bin/sh
    if [[ $# -eq 0 ]]; then
    echo "Usage: $0 <backup_dir>"
    exit
    fi

    #Check if we have pv installed...
    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.tgz";
    system("docker save $original | $PV | gzip -c > $destination/$normalised_container_name.tgz")
    };
    }'

    for container in $containers
    do
    echo "Container: $container => ${container//\//_}.tgz"
    docker save $container | $PV | gzip -c > ${container//\//_}.tgz
    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

    21 changes: 21 additions & 0 deletions restore_all_docker_images.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    #!/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

  2. dieterrosch created this gist Nov 9, 2016.
    15 changes: 15 additions & 0 deletions backup_all_docker_images.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    containers=$(docker images |awk '{ if ($1 != "REPOSITORY") { print $1};}' )

    #Check if we have pv installed...
    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
    echo "Container: $container => ${container//\//_}.tgz"
    docker save $container | $PV | gzip -c > ${container//\//_}.tgz
    done