Last active
October 13, 2020 23:20
-
-
Save smileart/b56588b5219d05fb62d46ce50e5db189 to your computer and use it in GitHub Desktop.
Backing up and Restoring named Docker volumes
This file contains 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
# Based on | |
https://stackoverflow.com/questions/38298645/how-should-i-backup-restore-docker-named-volumes | |
# Dependencies | |
brew install fzf fx | |
# Backup (docker-compose running and we're in the same dir) | |
export CONTAINER_NAME=$(docker ps --format '{{.Names}}' | fzf) && \ | |
export VOLUME_NAME=$(docker inspect $CONTAINER_NAME | fx '.[0].Mounts[0].Name') && \ | |
docker stop $(docker inspect $CONTAINERNAME | fx '.[0].Id') && \ | |
docker run --rm --volume $VOLUME_NAME:/data --volume $(pwd):/volumebackup ubuntu tar cvf /volumebackup/$CONTAINER_NAME.tar /data && \ | |
docker-compose up -d $CONTAINER_NAME | |
# Restore (docker-compose running and we're in the same dir + backup file is here too) | |
export CONTAINER_NAME=$(docker ps --format '{{.Names}}' | fzf) && \ | |
export VOLUME_NAME=$(docker inspect $CONTAINER_NAME | fx '.[0].Mounts[0].Name') && \ | |
export VOLUME_DEST=$(docker inspect $CONTAINER_NAME | fx '.[0].Mounts[0].Destination') && \ | |
docker stop $(docker inspect $CONTAINERNAME | fx '.[0].Id') && \ | |
docker run --rm --volume $VOLUME_NAME:$VOLUME_DEST --volume $(pwd):/volumebackup ubuntu tar xvf /volumebackup/$CONTAINER_NAME.tar -C $VOLUME_DEST --strip 1 && \ | |
docker-compose up -d $CONTAINER_NAME |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment