Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I backup & restore docker named volumes

I'm a little bit confused with the functionnality of named volumes in a docker compose file specifically when it comes to backup/restore my app.

I'm actually testing this dockercompose file :

      version: '2'       services:           django:               build:                    context: "{{ build_dir }}/docker/django"               depends_on:                   - db               environment:                   [...]               volumes:                   - code:/data/code                   - www:/var/www                   - conf:/data/conf               networks:                   - front                   - db               expose:                   - "8080"               entrypoint: "/init"           db:               build:                   context: "{{ build_dir }}/docker/postgres"                environment:                   [...]               volumes:                   - data:/var/lib/postgresql/data               networks:                   - db        volumes:           data:           www:           code:           conf:        networks:           front:               external:                   name: "proxy_nw" 

As the documentation said, I tried to use named volume instead of data only container. But how am I suppose to backup my data ?

With a data only container I would have done a docker run --rm --volume-from DOC backup_container save which is really easy.

Now I read in this topic that I should use something like docker run --rm --volume data --volume www --volume code --volume conf backup_container save. This is not so simple because I have many applications with different types and names of volumes so it means that my command to save my data would have to be different for each application. It complicate automation process.

Edit: Actually this syntaxe docker run --volume data --volume www container_image my_command is not correct. It needs the mountpoint inside the container, so it would be docker run --volume data:/somewhere --volume www:/somewhereelse container_image my_command. So it's even more complicated to use with a backup container.

So, what are the best practices in this case ? Should I use just one named volume for all my containers ?

like image 462
Plup Avatar asked Jul 11 '16 03:07

Plup


2 Answers

Actually it should be done same way as written in official documentation. Data volume container stores it's data in "virtual root", so you should backup with next command:

docker run --rm \    --volume [DOCKER_COMPOSE_PREFIX]_[VOLUME_NAME]:/[TEMPORARY_DIRECTORY_TO_STORE_VOLUME_DATA] \   --volume $(pwd):/[TEMPORARY_DIRECTORY_TO_STORE_BACKUP_FILE] \   ubuntu \   tar cvf /[TEMPORARY_DIRECTORY_TO_STORE_BACKUP_FILE]/[BACKUP_FILENAME].tar /[TEMPORARY_DIRECTORY_TO_STORE_VOLUME_DATA] 

where:

  • --rm means that the image created for this run command will be cleaned up
  • DOCKER_COMPOSE_PREFIX in default is your project directory name
  • VOLUME_NAME is the data-volume container name from compose file
  • TEMPORARY_DIRECTORY_TO_STORE_VOLUME_DATA is a directory to mount your volume data
  • TEMPORARY_DIRECTORY_TO_STORE_BACKUP_FILE is a directory virtually mapped to your current directory, where the backup will be placed
  • BACKUP_FILENAME - a name of backup file (you find it in current directory)
  • ubuntu - you may change image type to another container with tar :)

Get data back into the volume(restore):

docker run --rm \    --volume [DOCKER_COMPOSE_PREFIX]_[VOLUME_NAME]:/[TEMPORARY_DIRECTORY_STORING_EXTRACTED_BACKUP] \   --volume $(pwd):/[TEMPORARY_DIRECTORY_TO_STORE_BACKUP_FILE] \   ubuntu \   tar xvf /[TEMPORARY_DIRECTORY_TO_STORE_BACKUP_FILE]/[BACKUP_FILENAME].tar -C /[TEMPORARY_DIRECTORY_STORING_EXTRACTED_BACKUP] --strip 1 

where:

  • TEMPORARY_DIRECTORY_STORING_EXTRACTED_BACKUP is a directory where the extracted files will be copied to (this is linked with the volume and will therefore write to it)
  • -C - tell tar where to extract the contents
  • --strip 1 - remove leading path elements (e.g. the parent directory if the backup contents are located in a /temp folder or similar)
like image 158
Aleksey Rembish Avatar answered Sep 21 '22 04:09

Aleksey Rembish


Base on this answer. I made an easy tool here: docker_named_volume_backup

first run command docker volume ls to list the named volume you want to backup.

then for backup

#sudo backup_docker_volume.sh <volumn_name> <tar_file> sudo bash ./backup_docker_volume.sh codimd_database-data backup1.tar 

for restore

#sudo restore_docker_volume.sh <volumn_name> <tar_file> sudo bash ./restore_docker_volume.sh codimd_database-data backup1.tar 
like image 44
傅继晗 Avatar answered Sep 22 '22 04:09

傅继晗