Skip to content

Instantly share code, notes, and snippets.

@ziadoz
Last active October 10, 2022 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ziadoz/dc8f442a412c25bfde96e565c993fac1 to your computer and use it in GitHub Desktop.
Save ziadoz/dc8f442a412c25bfde96e565c993fac1 to your computer and use it in GitHub Desktop.
Backup and Restore MySQL Docker Volume
#!/usr/bin/env bash
# @see: https://docs.docker.com/storage/volumes/#backup-restore-or-migrate-data-volumes
# @see: https://jareklipski.medium.com/backup-restore-docker-named-volumes-350397b8e362
# @see: https://www.spherex.dev/backing-up-docker-volumes/
# The name of the volume to backup/restore:
VOLUME="my-db"
# Backup the Docker Volume to an archive:
docker run --rm --volume $VOLUME:/var/lib/mysql -v $(pwd):/backup alpine:latest ash -c "tar cvf /backup/$VOLUME.tar /var/lib/mysql"
# Restore the archive back to a Docker Volume:
docker run --rm -v $VOLUME:/var/lib/mysql -v $(pwd):/backup alpine:latest ash -c "cd /var/lib/mysql && tar xvf /backup/$VOLUME.tar --strip 3"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment