Skip to content

Instantly share code, notes, and snippets.

@rgov
Created April 8, 2020 18:24
Show Gist options
  • Select an option

  • Save rgov/edc5028d5649be68d7648cff793e5163 to your computer and use it in GitHub Desktop.

Select an option

Save rgov/edc5028d5649be68d7648cff793e5163 to your computer and use it in GitHub Desktop.
Backing up Docker volumes

This is documented.

I'm confused by how docker-compose decides to name volumes, prefixing a garbled directory path to the volume name. Consequently if you rename the directory, a new volume is created.

So I wanted to export the volume contents as a .tar.

MOUNT=/usr/share/elasticsearch/data; \
for VOLUME in {mariner,data_parsers_mariner,optdata_parsers_mariner}_elasticsearch_data; do \
    sudo docker run --rm -v $VOLUME:$MOUNT -v /data/backup:/backup ubuntu \
        tar cvf /backup/$VOLUME.tar $MOUNT; \
done

The $MOUNT path is where the volume actually gets mounted. I had 3 different potential volumes listed by docker volume list and I didn't know which one was actually being used, so I just backed them all up.

The new volumes can be created without starting the services with: docker-compose up --no-start

Then the tarball can be restored with:

MOUNT=/usr/share/elasticsearch/data; \
NEW_VOLUME=mariner_elasticsearch_data; \
OLD_VOLUME=data_parsers_mariner_elasticsearch_data; \
sudo docker run --rm -v $NEW_VOLUME:$MOUNT -v /data/backup:/backup ubuntu \
    tar xvf /backup/$OLD_VOLUME.tar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment