Skip to content

Instantly share code, notes, and snippets.

@acalatrava
Created April 11, 2024 08:47
Show Gist options
  • Save acalatrava/f6cf122f4431795d39163a1d09f7e8bf to your computer and use it in GitHub Desktop.
Save acalatrava/f6cf122f4431795d39163a1d09f7e8bf to your computer and use it in GitHub Desktop.
Docker Volume Backup and Restore Script

Docker Volume Backup and Restore Script

Overview

This Bash script automates the process of exporting all Docker volumes to a compressed archive, creating a portable script for restoring these volumes on another system. It includes the generation of a recover.sh script for restoring the volumes from the archive and wraps everything into a self-executable import.sh script for easy transportation and execution.

Prerequisites

  • Docker installed on the host machine.
  • Bash shell environment.
  • Sufficient disk space for the backup files.

Features

  • Export Volumes: Iterates over all Docker volumes, exporting each to its own tar.gz file.
  • Create Unified Archive: Combines all individual volume archives into a single volumes.tar.gz file for easy management.
  • Automated Restoration Script Generation: Generates a recover.sh script capable of restoring volumes from the unified archive.
  • Self-Executable Restoration Script: Produces an import.sh script that when executed, will restore all volumes contained within the archive to the destination Docker environment.
  • Cleanup: Removes all intermediate files to leave only the necessary scripts for backup and restoration.

Usage

Backup Process

  1. Ensure the script is executable: chmod +x docker-volume-export.sh.sh
  2. Run the script in your terminal: ./docker-volume-export.sh.sh
  3. After execution, the script generates import.sh. This is the only file needed for restoration.

Restoration Process

  1. Copy import.sh to the destination Docker host.
  2. Ensure the script is executable: chmod +x import.sh
  3. Execute the script: ./import.sh
  4. The script will restore all volumes from the archive into the Docker environment.

Note

  • The script assumes all Docker volumes can be safely backed up and restored without data integrity issues. It's advisable to stop containers using these volumes during the backup and restoration process to avoid data corruption.
  • Ensure there is enough disk space on both the source and destination systems to accommodate the volume backups and their uncompressed contents.

Limitations

  • The script does not handle volumes used by running containers. Ensure containers are stopped to avoid data corruption.
  • The script currently does not support selective volume backup or restoration.

Conclusion

This script streamlines the process of Docker volume backup and restoration, making it easier to migrate volumes across environments or keep a backup for disaster recovery purposes. It's a handy tool for developers and system administrators managing Docker volumes.

#!/bin/bash
# Docker volume exporter
#
# Usage:
# ./docker-volume-export.sh
#
# It will generate a file called "import.sh". This file is a self-contained script with all the volumes.
# Copy "import.sh" to the destination host and execute
#
# Export all the volumes
VOLUMES=$(docker volume ls|tail -n +2|awk '{ print $2 }')
while IFS= read -r VOLUME; do
echo " - Exporting $VOLUME"
VOL_PATH=$(docker volume inspect $VOLUME|grep -i mountpoint|awk '{ print $2 }'|cut -d '"' -f2)
tar czf "$VOLUME.tgz" -C "$VOL_PATH" .
done <<< $VOLUMES
# Now create a tar.gz with all the volumes together
while IFS= read -r VOLUME; do
tar rf volumes.tar "$VOLUME.tgz"
rm "$VOLUME.tgz"
done <<< $VOLUMES
gzip volumes.tar
# Generate recover.sh file
echo "IyEvYmluL2Jhc2gKZGllICgpIHsKICAgIGVjaG8gPiYyICIkQCIKICAgIGV4aXQgMQp9CgpbICIkIyIgLWVxIDEgXSB8fCBkaWUgInRhci5neiBmaWxlIHRvIHByb2Nlc3MgcmVxdWlyZWQiCgpGSUxFPSQxCgppZiB0ZXN0IC1mICIkRklMRSI7IHRoZW4KCVRNUERJUj0kKG1rdGVtcCAtZCkKCXRhciB4emYgJEZJTEUgLUMgJFRNUERJUgoJZm9yIGZpbGVuYW1lIGluICIkVE1QRElSIi8qOyBkbwoJCWZpbGU9JChiYXNlbmFtZSAtLSAiJGZpbGVuYW1lIikKCQl2b2xfbmFtZT0iJHtmaWxlJS4qfSIKCQllY2hvICIgW3hdIENyZWF0aW5nICR2b2xfbmFtZSIKCQkjIENvbXByb2JhbW9zIHNpIGV4aXN0ZQoJCWlmIGRvY2tlciB2b2x1bWUgaW5zcGVjdCAiJHZvbF9uYW1lIiAyPi9kZXYvbnVsbCA+L2Rldi9udWxsIDsgdGhlbgoJCQllY2hvICIgWyFdIEVycm9yLCB2b2x1bWUgYWxyZWFkeSBleGlzdHMhIHNraXBwaW5nIgoJCWVsc2UKCQkJaWYgZG9ja2VyIHZvbHVtZSBjcmVhdGUgIiR2b2xfbmFtZSIgMj4vZGV2L251bGwgPi9kZXYvbnVsbCA7IHRoZW4KCQkJCVZPTF9QQVRIPSQoZG9ja2VyIHZvbHVtZSBpbnNwZWN0ICR2b2xfbmFtZXxncmVwIC1pIG1vdW50cG9pbnR8YXdrICd7IHByaW50ICQyIH0nfGN1dCAtZCAnIicgLWYyKQoJCQkJdGFyIHh6ZiAiJFRNUERJUi8kZmlsZSIgLUMgJFZPTF9QQVRICgkJCWVsc2UKCQkJCWVjaG8gIiBbIV0gRXJyb3IsIHVuYWJsZSB0byBjcmVhdGUgdm9sdW1lIgoJCQlmaQoJCWZpCglkb25lCglybSAtUmYgIiRUTVBESVIiCgllY2hvICIgW3hdIENvbXBsZXRlZCEiCmVsc2UKCWRpZSAiRmlsZSBkb2VzIG5vdCBleGlzdCIKZmkK" | base64 --decode > recover.sh
chmod +x recover.sh
# Create a tar.gz with recover.sh and volumes.tar.gz
tar czf full.tar.gz -C . recover.sh volumes.tar.gz
rm recover.sh
rm volumes.tar.gz
# Create self executable script to process
echo "IyEvYmluL2Jhc2gKTUFSS0VSPSJfX19fX0FSQ0hJVkVfQkVMT1dfX19fXyIKZnVuY3Rpb24gZXh0cmFjdF9hcmNoaXZlKCkgewogICAgQVJDSElWRT0kKGF3ayAiLyRNQVJLRVIve3ByaW50IE5SICsgMTsgfSIgJDB8dGFpbCAtbiAxKQogICAgdGFpbCAtbiskQVJDSElWRSAkMCB8IHRhciB4egogICAgLi9yZWNvdmVyLnNoIHZvbHVtZXMudGFyLmd6CiAgICBybSByZWNvdmVyLnNoCiAgICBybSB2b2x1bWVzLnRhci5negp9CmV4dHJhY3RfYXJjaGl2ZQpleGl0IDAKX19fX19BUkNISVZFX0JFTE9XX19fX18K" | base64 --decode > import.sh
cat full.tar.gz >> import.sh
chmod +x import.sh
rm full.tar.gz
echo Completed! Now copy "import.sh" to destination docker and execute it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment