Backup and restore of data volumes in docker
1. Now use the image to create an nginx service container and mount a data volume
[root@docker ~]# docker run -v /data -d -p80:80 --name web1 centos_nginx:v4 55a45a822c29a1e673d7ca2fb65a4d08fe6c896f5b4fa08fac4d45950e9e7f67
2. Enter the container and add some data to the empty data volume directory /data
[root@docker ~]# docker exec -it web1 /bin/bash [root@55a45a822c29 nginx-1.12.2]# cd /data/ [root@55a45a822c29 data]# ll total 0 [root@55a45a822c29 data]# mkdir wadeson [root@55a45a822c29 data]# vim wadeson/web1.text bash: vim: command not found [root@55a45a822c29 data]# vi wadeson/web1.text [root@55a45a822c29 data]# cat wadeson/web1.text this is in web1
3. Create another container with the web1 container above as the shared data volume, and tar-compress the shared data volume for backup and map it to the local host
[root@docker ~]# docker run -d --volumes-from web1 -v /root/backup:/backup centos tar cvf /backup/data.tar /data 081dd7df6371b86e33106664390a8a2780a1f8fc46d69b88adec2970f6d394cf
Detailed explanation of the above command:
--volumes-from web1: Indicates that the created container is based on the data volume in the web1 container (that is, the two containers share the data volume), and the data volume of web1 is /data
tar vcf /backup/data.tar /data: The created container tar-compresses /data
-v /root/backup:/backup : Map the directory /backup in the container with the /root/backup of the local host
General command explanation: Create a new container data volume based on the data volume of the shared web1 container, tar the data volume to a directory in the container, and map it to the directory of the local host
[root@docker ~]# ll backup/data.tar -rw-r--r--. 1 root root 10240 Nov 2 22:24 backup/data.tar [root@docker ~]# cd backup/ [root@docker backup]# ll total 12 -rw-r--r--. 1 root root 10240 Nov 2 22:24 data.tar [root@docker backup]# tar xf data.tar [root@docker backup]# cd data [root@docker data]# ll total 0 drwxr-xr-x. 2 root root 23 Nov 2 22:13 wadeson [root@docker data]# cat wadeson/web1.text this is in web1
而此次创建的容器的状态为exited也不会影响数据卷的备份:
于是基于数据卷的备份就ok了
现在将备份好的数据进行恢复还原:
1、创建一个空数据卷的容器web2:(创建的数据卷目录名称必须和备份的数据卷名称一致)
[root@docker ~]# docker run -it -v /data --name web2 centos /bin/bash
[root@090eb911d368 /]# ll /data0/
total 0
2、创建一个容器与web2共享数据卷,将备份的数据解压到数据卷/data中:
[root@docker ~]# docker run --volumes-from web2 -v /root/backup:/backup centos tar xvf /backup/data.tar data/ data/wadeson/ data/wadeson/web1.text
The started new container shares the data volume directory /data with the web2 container, and the local host maps the data compressed file under /root/backup to the /backup directory of the new container, and executes the command to decompress the data compressed file to the data volume /data
Without --volumes-from web2 then there will be no data volume directory /data
3. Create a new container shared data volume and view the data content:
[root@docker ~]# docker run -it --volumes-from web2 --name web3 centos sh -c "ls -l /data/wadeson" total 4 -rw-r--r--. 1 root root 16 Nov 3 02:13 web1.text