Skip to content

Instantly share code, notes, and snippets.

@onebytegone
Created November 14, 2018 00:59
Show Gist options
  • Save onebytegone/2252aa986e24577e9dc2cf824ee68b9e to your computer and use it in GitHub Desktop.
Save onebytegone/2252aa986e24577e9dc2cf824ee68b9e to your computer and use it in GitHub Desktop.
Backup and restore a Docker volume on Mac OS X

Backup and restore a Docker volume on Mac OS X

This is a rough draft for backing up and restoring data contained in a Docker volume. I can't say that this is a "best practice". For my limited testing at the moment, it seems sufficient.

Example docker-compose.yml

version: '3'
services:
   db:
      image: mysql:5.7
      environment:
         MYSQL_ROOT_PASSWORD: secret
         MYSQL_DATABASE: tacos
         MYSQL_USER: tacos
         MYSQL_PASSWORD: secret
      volumes:
         - databasedata:/var/lib/mysql
volumes:
   databasedata:
      driver: 'local'

To start:

docker-compose up

Login and interact with MySQL

From the host machine, where DEADBEEF0123 is the ID of the running container.

docker exec -it DEADBEEF0123 bash

In the container:

mysql -u tacos -p

Backup MySQL

From the host machine:

CONTAINER='DEADBEEF0123'
DIR_TO_BACKUP='/var/lib/mysql'
BACKUP_NAME='backup.tar'
docker run --rm --volumes-from "${CONTAINER}" -v $(pwd):/backup alpine:3.8 tar cvf "/backup/${BACKUP_NAME}" "${DIR_TO_BACKUP}"

Restore MySQL

From the host machine:

CONTAINER='DEADBEEF0123'
BACKUP_NAME='backup.tar'
docker run --rm --volumes-from "${CONTAINER}" -v $(pwd):/backup alpine:3.8 tar xvf "/backup/${BACKUP_NAME}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment