如何从已删除的Docker容器中恢复数据?如何重新连接到数据? [英] How to recover data from a deleted Docker container? How to reconnect it to the data?
问题描述
让我们说,我删除了一个PostgreSQL docker容器,它只有这里的数据:
$ docker inspect postgres1
...
Source:/var/lib/docker/volumes/4948af..../_data
Destination:/ var / lib / postgresql / data
$ docker rm postgres1
如果没有其他容器引用卷,无法使用 - volumes-from
重新连接到此卷,即使文件仍位于磁盘上的某个位置: / var / lib / docker / volumes /...
这给我两个问题:
<
- 执行
ls / var / lib / docker / volumes /
f77c92 ...
。 - 使用
查找。 -name* postgres *
我仍然得到这样的几十个结果... 10e0dc / _data / postgresql.conf,没有明确的方法来识别正确的一个。
/ var / lib / docker / volumes / 4948af ... / _ data
每日全系统rsync备份。
这正是为什么,当创建一个数据容器,我总是在一个文件中注册它的路径。 (请参阅我的脚本 updateDataContainerPath
)
用法(仅在创建数据容器之后使用):
docker inspect $ {gitolite_repos_cont}> / dev / null 2>& 1 || docker create --name =$ {gitolite_repos_cont}gitolite.repos / bin / true
#源脚本,使updatePath()函数可用
。 ../updateDataContainerPath
#保存文件中的路径
updatePath $ {gitolite_repos_cont}$ HOME / b2d / gitolite$ {grepos}
(这里 $ {grepos}
是您注册或保存路径的文件
该脚本将会为该数据容器保存的 路径删除空数据容器文件夹,将旧文件移动到新文件夹(并更新新路径)
sudo rm -Rf$ {grpath}
sudo mv$ {fgrpath} $ {grpath}
这将有助于回答你的问题2,并完全避免你的问题1。
这样,我可以rm任何容器(包括数据容器,当然没有 -v
,我知道下一次我重新创建相同的数据容器,我会找回我的数据。
Let's say, I delete a PostgreSQL docker container which had its data only here:
$ docker inspect postgres1
...
"Source": "/var/lib/docker/volumes/4948af..../_data"
"Destination": "/var/lib/postgresql/data"
$ docker rm postgres1
If there was no other container referencing the volume, I cannot reconnect to this volume with --volumes-from
any more, even though the files are still on disk somewhere in: /var/lib/docker/volumes/...
This presents me with two problems:
- What is the best way to locate the data, if I do not know the volume UUID of the directory?
- Doing
ls /var/lib/docker/volumes/
presents me with a hundred dirs like thisf77c92...
. - Using
find . -name "*postgres*"
I still get dozens of results like this ...10e0dc/_data/postgresql.conf, with no clear way to identify the right one.
- Doing
- Once I find the correct data in the directory, how do I reconnect a newly created postgres container to this data in
/var/lib/docker/volumes/4948af.../_data
I have a daily full system rsync backup. How could this help in recovery (short of restoring the whole system)?
That is exactly why, when creating a data container, I always register its path in a file. (see my script updateDataContainerPath
)
Usage (to be used just after creating a data container):
docker inspect ${gitolite_repos_cont} > /dev/null 2>&1 || docker create --name="${gitolite_repos_cont}" gitolite.repos /bin/true
# source the script, to make the updatePath() function available
. ../updateDataContainerPath
# save the path in a file
updatePath ${gitolite_repos_cont} "$HOME/b2d/gitolite" ${grepos}
(here ${grepos}
is the file where you register or save the path of the volume of the data container)
That script will, if there was already a path saved for that data container, remove the empty data container folder, and move the old one to the new one (and update the new path)
sudo rm -Rf "${grpath}"
sudo mv "${fgrpath}" "${grpath}"
That would help answering your question 2, and avoid entirely your question 1.
That way, I can rm any container (including a data container, without the -v
option of course), and I know the next time I recreate that same data container, I will find back my data.
这篇关于如何从已删除的Docker容器中恢复数据?如何重新连接到数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!