Created
April 15, 2024 21:04
-
-
Save ergosteur/8c2c7ceb5d2bba4e9ae612cf5a4b5dcf to your computer and use it in GitHub Desktop.
Quick and dirty Docker Volume backup/export and restore/import scripts
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
#!/bin/bash | |
# Check if backup directory is provided | |
if [ "$#" -ne 1 ]; then | |
echo "Usage: $0 <backup_directory>" | |
exit 1 | |
fi | |
backup_dir=$1 | |
# Get a list of all local docker volumes | |
volumes=$(docker volume ls -q) | |
# Loop through each volume and export it to a .tar.gz file | |
for volume in $volumes; do | |
# Create a temporary container to mount the volume and export the data | |
docker run --rm -v $volume:/data -v $backup_dir:/backup alpine tar czf /backup/$volume.tar.gz /data | |
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
#!/bin/bash | |
# Check if backup directory is provided | |
if [ "$#" -ne 1 ]; then | |
echo "Usage: $0 <backup_directory>" | |
exit 1 | |
fi | |
backup_dir=$1 | |
# Get a list of all .tar.gz files in the backup directory | |
files=$(ls $backup_dir/*.tar.gz) | |
# Loop through each file and create or update a docker volume | |
for file in $files; do | |
# Get the volume name from the file name | |
volume=$(basename $file .tar.gz) | |
# Check if the volume already exists | |
if docker volume ls -q | grep -q "^$volume\$"; then | |
# Volume exists, update it with the data from the .tar.gz file | |
docker run --rm -v $volume:/data -v $backup_dir:/backup alpine sh -c "tar xzf /backup/$volume.tar.gz --strip-components=1 -C /data" | |
else | |
# Volume does not exist, create it and populate it with the data from the .tar.gz file | |
docker volume create $volume | |
docker run --rm -v $volume:/data -v $backup_dir:/backup alpine sh -c "tar xzf /backup/$volume.tar.gz --strip-components=1 -C /data" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment