如何恢復docker容器數(shù)據(jù)
項目測試環(huán)境數(shù)據(jù)庫數(shù)據(jù)丟失,特此記錄一下。當時是用 docker 安裝的,以為臨時使用一段時間,也沒有持久化。突然前天 docker 日志滿了,同事想著去清理日志,使用了如下命令:
docker system prune
結果當時 MySQL 容器當時正常處于停止狀態(tài),結果容器一下子就被干掉了,我們備份的數(shù)據(jù)還是三月份的,這下糟糕了。然后各種研究開始恢復。
然后我就去官方文檔去研究這個命令是干什么的,上面用到的 docker system prune 意思是:
Remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes.
刪除所有未使用的容器、網(wǎng)絡、圖像(懸空和未引用的圖像)以及卷(可選)。
By default, volumes are not removed to prevent important data from being deleted if there is currently no container using the volume. Use the --volumes flag when running the command to prune volumes as well:
默認情況下,如果當前沒有使用卷的容器,則不會刪除卷以防止刪除重要數(shù)據(jù)。運行命令時也可以使用 --volumes 標志來修剪卷:
這下心放下了一半,還好數(shù)據(jù)卷沒有被刪除,我們可以利用數(shù)據(jù)卷可以進行恢復數(shù)據(jù)。接下來記錄下我的恢復方案吧。
1.查找數(shù)據(jù)卷位置數(shù)據(jù)卷目錄在 /var/lib/docker/volumes 下,每個容器都會在該目錄下有一個文件夾,如果容器還存在的話,我們可以使用 docker inspect 容器ID 去查看 數(shù)據(jù)卷位置,這下容器被刪除了,可怎么辦,只能挨個去找了,一般 MySQL 容器數(shù)據(jù)卷目錄下會有一個 _data 目錄,該目錄下會顯示你每個數(shù)據(jù)庫的文件夾,最終找到了。
這個 cxhello 就是我們的測試庫,現(xiàn)在我們就可以恢復數(shù)據(jù)了。
2.恢復使用 docker volume create 數(shù)據(jù)卷名字 命令新建一個數(shù)據(jù)卷,docker volume ls 查看數(shù)據(jù)卷列表
注意:使用數(shù)據(jù)卷進行掛載的時候,數(shù)據(jù)卷必須是一個空的目錄,也就是說不能有任何數(shù)據(jù)。
然后創(chuàng)建容器
docker run -d -p 3309:3306 -v mysqldata:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name huifu mysql:5.7
在恢復數(shù)據(jù)之前需要把剛剛建立的數(shù)據(jù)卷里面關聯(lián)的內(nèi)容刪除掉,然后把之前的數(shù)據(jù)卷內(nèi)容復制到現(xiàn)在的數(shù)據(jù)卷進行數(shù)據(jù)恢復。
cd /var/lib/docker/volumes/mysqldata/_data/rm -f *rm -f -R *
復制內(nèi)容到數(shù)據(jù)卷
cd /var/lib/docker/volumes/1db16a9dfdf3442b117ebc2ec11df5df4db717cfd567c77fa0a49905a9652fa0/_data/cp -R * /var/lib/docker/volumes/mysqldata/_data/
至此數(shù)據(jù)庫數(shù)據(jù)恢復完成,進入恢復的容器查看
https://docs.docker.com/engine/reference/commandline/system_prune/
https://www.cnblogs.com/cheyunhua/p/13433400.html
到此這篇關于如何恢復docker容器數(shù)據(jù) 的文章就介紹到這了,更多相關docker容器數(shù)據(jù)恢復內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!
相關文章:
