> For the complete documentation index, see [llms.txt](https://bohans.gitbook.io/devops/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bohans.gitbook.io/devops/ji-chu-zhi-shi/6.-docker-shu-ju-guan-li/6.3-li-yong-shu-ju-juan-rong-qi-lai-qian-yi-shu-ju.md).

# 6.3 利用数据卷容器来迁移数据

可以利用**数据卷容器**对其中的数据卷进行**备份**、**恢复**，以实现数据的迁移。

## 1.备份

```bash
$ docker run --name worker --volumes-from dbdata \
    -v $(pwd):/backup ubuntu:18.04 tar -cvf /backup/backup.tar /dbdata
```

上述命令：

* 利用ubuntu镜像创建了一个容器worker；
* 使用--volumes-from dbdata参数来让worker容器挂载dbdata容器的数据卷（即dbdata数据卷）；
* 使用-v $(pwd):/backup参数来挂载本地的当前目录到worker容器的/backup目录；
* worker容器启动后，使用tar cvf /backup/backup.tar /dbdata命令将/dbdata下内容备份为容器内的/backup/backup.tar，即宿主主机当前目录下的backup.tar。

## 2.恢复

```bash
$ docker run -itd --name dbdata2 -v /dbdata ubuntu:18.04
c2f2736dc8a9abba28e08fbef47ae612669b5dd1f16e5a1b371e63ba45f02809

$ docker run --name recovery --volumes-from dbdata2 \
    -v $(pwd):/backup ubuntu:18.04 tar -xvf /backup/test.tar
dbdata/
dbdata/test.log
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bohans.gitbook.io/devops/ji-chu-zhi-shi/6.-docker-shu-ju-guan-li/6.3-li-yong-shu-ju-juan-rong-qi-lai-qian-yi-shu-ju.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
