我应该如何备份 &恢复 docker 命名卷 [英] How should I backup & restore docker named volumes

查看:25
本文介绍了我应该如何备份 &恢复 docker 命名卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 docker compose 文件中命名卷的功能有点困惑,特别是在备份/恢复我的应用程序时.

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.

我实际上是在测试这个 dockercompose 文件:

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 ?

使用纯数据容器我会做一个 docker run --rm --volume-from DOC backup_container save 这真的很容易.

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

现在我阅读了本主题,我应该使用类似 docker run --rm --volume data --volume www --volume code --volume conf backup_container save 之类的东西.这不是那么简单,因为我有许多具有不同类型和卷名称的应用程序,所以这意味着我保存数据的命令对于每个应用程序都必须不同.它使自动化过程复杂化.

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.

实际上这个语法docker run --volume data --volume www container_image my_command 不正确.它需要容器内的挂载点,所以它是docker run --volume data:/somewhere --volume www:/somewhereelse container_image my_command.因此,与备份容器一起使用更加复杂.

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 ?

推荐答案

其实应该按照官方文档里写的方式来做.数据卷容器将其数据存储在虚拟根"中,因此您应该使用下一个命令进行备份:

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]

哪里:

  • --rm 表示将清理为此运行命令创建的映像
  • DOCKER_COMPOSE_PREFIX默认是你的项目目录名
  • VOLUME_NAME 是来自撰写文件的数据卷容器名称
  • TEMPORARY_DIRECTORY_TO_STORE_VOLUME_DATA 是挂载卷数据的目录
  • TEMPORARY_DIRECTORY_TO_STORE_BACKUP_FILE 是一个虚拟映射到您当前目录的目录,用于放置备份
  • BACKUP_FILENAME - 备份文件的名称(您可以在当前目录中找到它)
  • ubuntu - 您可以使用 tar 将图像类型更改为另一个容器 :)
  • --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

哪里:

  • TEMPORARY_DIRECTORY_STORING_EXTRACTED_BACKUP 是将提取的文件复制到的目录(该目录与卷链接,因此会写入)
  • -C - 告诉 tar 在哪里提取内容
  • --strip 1 - 删除前导路径元素(例如,如果备份内容位于/temp 文件夹或类似文件夹中,则为父目录)
  • 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)

这篇关于我应该如何备份 &恢复 docker 命名卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆