MongoDB日常運維-08備份與恢復
MongoDB日常運維-08備份與恢復
一 mongoexport/mongoimport
二 mongodump/mongorestore
三 自動備份
一 mongoexport/mongoimport
[root@svnhost bin]# pwd
/usr/local/mongodb/bin
[root@svnhost bin]# ./mongoexport --help
匯出chendb下test01表
[root@svnhost bin]# ./mongoexport -d chendb -c test01 -o /mongobak/chendb_test01.bak
[root@svnhost bin]# ./mongoexport -d chendb -c test02 -o /mongobak/chendb_test02.bak
檢視備份內容
[root@svnhost bin]# cat /mongobak/chendb_test01.bak
{"_id":{"$oid":"5b8f418f1c1560b2bb03cb7b"},"url":"www.sina.com"}
{"_id":{"$oid":"5b8f41c51c1560b2bb03cb7c"},"url":"ccc - 10"}
.....
{"_id":{"$oid":"5b8f41d81c1560b2bb03cb90"},"url":19.0}
{"_id":{"$oid":"5b8f41d81c1560b2bb03cb91"},"url":20.0}
{"_id":{"$oid":"5b8f74e51c1560b2bb03cb92"},"url":"www.baidu.com"}
恢復單表
將test01表恢復並重新命名為test01aaa
[root@svnhost ~]# cd /usr/local/mongodb/bin/
[root@svnhost bin]# ./mongoimport -d chendb -c test01aaa /mongobak/chendb_test01.bak
2018-09-05T17:39:09.592+0800 connected to: localhost
2018-09-05T17:39:09.807+0800 imported 24 documents
MongoDB Enterprise > show collections
test01
test01aaa
test02
檢視資料
MongoDB Enterprise > db.test01aaa.find()
{ "_id" : ObjectId("5b8f41c51c1560b2bb03cb7c"), "url" : "ccc - 10" }
{ "_id" : ObjectId("5b8f41c51c1560b2bb03cb7d"), "url" : "ccc - 11" }
{ "_id" : ObjectId("5b8f41c51c1560b2bb03cb7e"), "url" : "ccc - 12" }
.....
{ "_id" : ObjectId("5b8f41d81c1560b2bb03cb90"), "url" : 19 }
{ "_id" : ObjectId("5b8f41d81c1560b2bb03cb91"), "url" : 20 }
{ "_id" : ObjectId("5b8f74e51c1560b2bb03cb92"), "url" : "www.baidu.com" }
刪除表,模擬故障
MongoDB Enterprise > show collections
test01
test01aaa
test02
MongoDB Enterprise > db.test02.drop()
true
MongoDB Enterprise > db.test02.find()
恢復表test02
[root@svnhost bin]# ./mongoimport -d chendb -c test02 /mongobak/chendb_test02.bak
2018-09-05T17:49:47.912+0800 connected to: localhost
2018-09-05T17:49:48.089+0800 imported 1 document
檢視資料
MongoDB Enterprise > db.test02.find()
{ "_id" : ObjectId("5b8f752f943b3cbaa33a5518"), "url" : "www.baidu.com" }
二 mongodump/mongorestore
[root@svnhost bin]# ./mongodump --help
備份chendb全庫
root@svnhost bin]# ./mongodump -d chendb -o /mongobak/db_backup/
2018-09-06T10:24:19.545+0800 writing chendb.test01 to
2018-09-06T10:24:19.545+0800 writing chendb.test02 to
2018-09-06T10:24:19.546+0800 writing chendb.test01aaa to
2018-09-06T10:24:19.547+0800 done dumping chendb.test01 (24 documents)
2018-09-06T10:24:19.563+0800 done dumping chendb.test02 (1 document)
2018-09-06T10:24:19.579+0800 done dumping chendb.test01aaa (24 documents)
檢視備份檔案
[root@svnhost bin]# unset LANG
[root@svnhost bin]# ll -rth /mongobak/db_backup/chendb/
total 24K
-rw-r--r-- 1 root root 127 Sep 6 10:24 test01.metadata.json
-rw-r--r-- 1 root root 127 Sep 6 10:24 test02.metadata.json
-rw-r--r-- 1 root root 130 Sep 6 10:24 test01aaa.metadata.json
-rw-r--r-- 1 root root 914 Sep 6 10:24 test01.bson
-rw-r--r-- 1 root root 45 Sep 6 10:24 test02.bson
-rw-r--r-- 1 root root 914 Sep 6 10:24 test01aaa.bson
備份MongoDB下全部資料庫
[root@svnhost bin]# ./mongodump -o /mongobak/db_fullbackup/
2018-09-06T10:28:35.465+0800 writing admin.system.version to
2018-09-06T10:28:35.465+0800 done dumping admin.system.version (1 document)
2018-09-06T10:28:35.465+0800 writing chendb.test01 to
2018-09-06T10:28:35.465+0800 writing chendb.test01aaa to
2018-09-06T10:28:35.465+0800 writing chendb.test02 to
2018-09-06T10:28:35.466+0800 done dumping chendb.test01 (24 documents)
2018-09-06T10:28:35.466+0800 done dumping chendb.test02 (1 document)
2018-09-06T10:28:35.482+0800 done dumping chendb.test01aaa (24 documents)
指定使用者名稱密碼和埠進行備份
[root@svnhost bin]# ./mongodump -d chendb -o /mongobak/newdbbackup -u cjc -p cjc --port 27017
2018-09-06T16:37:45.209+0800 writing chendb.test01aaa to
2018-09-06T16:37:45.209+0800 writing chendb.test01bbb to
2018-09-06T16:37:45.209+0800 writing chendb.test01 to
2018-09-06T16:37:45.209+0800 writing chendb.test02 to
2018-09-06T16:37:45.250+0800 done dumping chendb.test02 (1 document)
2018-09-06T16:37:45.252+0800 done dumping chendb.test01bbb (24 documents)
2018-09-06T16:37:45.252+0800 done dumping chendb.test01aaa (24 documents)
2018-09-06T16:37:45.277+0800 done dumping chendb.test01 (24 documents)
備份chendb庫中test01表
[root@svnhost bin]# ./mongodump -d chendb -c test01 -o /mongobak/chendb_test01/
2018-09-06T10:42:39.354+0800 writing chendb.test01 to
2018-09-06T10:42:39.354+0800 done dumping chendb.test01 (24 documents)
檢視備份
[root@svnhost bin]# ll -rth /mongobak/chendb_test01/chendb/
total 8.0K
-rw-r--r-- 1 root root 127 Sep 6 10:42 test01.metadata.json
-rw-r--r-- 1 root root 914 Sep 6 10:42 test01.bson
恢復單表
[root@svnhost bin]# pwd
/usr/local/mongodb/bin
[root@svnhost bin]# mongorestore -d chendb -c test01bbb /mongobak/chendb_test01/chendb/test01.bson
2018-09-06T15:43:22.572+0800 checking for collection data in /mongobak/chendb_test01/chendb/test01.bson
2018-09-06T15:43:22.572+0800 reading metadata for chendb.test01bbb from /mongobak/chendb_test01/chendb/test01.metadata.json
2018-09-06T15:43:22.755+0800 restoring chendb.test01bbb from /mongobak/chendb_test01/chendb/test01.bson
2018-09-06T15:43:22.816+0800 no indexes to restore
2018-09-06T15:43:22.816+0800 finished restoring chendb.test01bbb (24 documents)
2018-09-06T15:43:22.816+0800 done
檢視資料
MongoDB Enterprise > db.test01bbb.find()
{ "_id" : ObjectId("5b8f41d81c1560b2bb03cb87"), "url" : 10 }
{ "_id" : ObjectId("5b8f41d81c1560b2bb03cb88"), "url" : 11 }
......
{ "_id" : ObjectId("5b8f41d81c1560b2bb03cb90"), "url" : 19 }
{ "_id" : ObjectId("5b8f74e51c1560b2bb03cb92"), "url" : "www.baidu.com" }
恢復單個資料庫並重新命名為chendbaaa
[root@svnhost bin]# mongorestore -d chendbaaa /mongobak/db_backup/chendb
2018-09-06T15:52:04.586+0800 the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2018-09-06T15:52:04.586+0800 building a list of collections to restore from /mongobak/db_backup/chendb dir
2018-09-06T15:52:04.587+0800 reading metadata for chendbaaa.test01 from /mongobak/db_backup/chendb/test01.metadata.json
2018-09-06T15:52:04.587+0800 reading metadata for chendbaaa.test01aaa from /mongobak/db_backup/chendb/test01aaa.metadata.json
2018-09-06T15:52:04.756+0800 restoring chendbaaa.test01aaa from /mongobak/db_backup/chendb/test01aaa.bson
2018-09-06T15:52:04.940+0800 restoring chendbaaa.test01 from /mongobak/db_backup/chendb/test01.bson
2018-09-06T15:52:04.944+0800 no indexes to restore
2018-09-06T15:52:04.944+0800 finished restoring chendbaaa.test01aaa (24 documents)
2018-09-06T15:52:04.944+0800 no indexes to restore
2018-09-06T15:52:04.944+0800 finished restoring chendbaaa.test01 (24 documents)
2018-09-06T15:52:04.944+0800 reading metadata for chendbaaa.test02 from /mongobak/db_backup/chendb/test02.metadata.json
2018-09-06T15:52:05.098+0800 restoring chendbaaa.test02 from /mongobak/db_backup/chendb/test02.bson
2018-09-06T15:52:05.102+0800 no indexes to restore
2018-09-06T15:52:05.102+0800 finished restoring chendbaaa.test02 (1 document)
2018-09-06T15:52:05.102+0800 done
檢視資料庫
MongoDB Enterprise > show dbs
admin 0.000GB
chendb 0.000GB
chendbaaa 0.000GB
config 0.000GB
local 0.000GB
MongoDB Enterprise > use chendbaaa
switched to db chendbaaa
MongoDB Enterprise > show collections
test01
test01aaa
test02
MongoDB Enterprise > db.test02.find()
{ "_id" : ObjectId("5b8f752f943b3cbaa33a5518"), "url" : "www.baidu.com" }
三 自動備份
備份計劃任務
[root@cjcos ~]# crontab -l
01 03 * * * /dbbackup/mongodb/mongo_bak.sh
備份指令碼
備份例項27017、27018、27019下所有資料庫
並自動刪除2020開頭,3天前的所有備份檔案
[root@cloud3 ~]# cat /dbbackup/mongodb/mongo_bak.sh
#!/bin/bash
DATE=`date +%Y_%m_%d`
mkdir -p /dbbackup/mongodb/$DATE/{27017,27018,27019}
/kingdee/mongodb/bin/mongodump -o /dbbackup/mongodb/$DATE/27017 --port 27017
/kingdee/mongodb/bin/mongodump -o /dbbackup/mongodb/$DATE/27018 --port 27018
/kingdee/mongodb/bin/mongodump -o /dbbackup/mongodb/$DATE/27019 --port 27019
find /dbbackup/mongodb -mtime +3 -name "2020*" -exec rm -rf {} \;
檢視備份檔案
[root@cjcos ~]# cd /dbbackup/mongodb/
[root@cjcos mongodb]# du -sh *
32G 2020_03_21
32G 2020_03_22
32G 2020_03_23
4.0K mongo_bak.sh
通過備份檔案進行恢復
恢復chendb
[root@svnhost bin]# pwd
/usr/local/mongodb/bin
[root@cjcos bin]# mongorestore -d chendbccc /dbbackup/mongodb/2020_03_23/27017/chendb
歡迎關注我的微信公眾號"IT小Chen",共同學習,共同成長!!!
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29785807/viewspace-2681989/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Mongodb的備份恢復與匯出匯入MongoDB
- RAC備份恢復之Voting備份與恢復
- 備份與恢復:polardb資料庫備份與恢復資料庫
- 02 . MongoDB複製集,分片集,備份與恢復MongoDB
- mongodb 備份恢復常用命令MongoDB
- MySQL備份與恢復——基於Xtrabackup物理備份恢復MySql
- MySQL運維實戰之備份和恢復(8.1)xtrabackup全量備份MySql運維
- Jenkins備份與恢復Jenkins
- Postgresql 備份與恢復SQL
- MySQL 備份與恢復MySql
- Mysql備份與恢復(1)---物理備份MySql
- MySQL備份與恢復——基於MyDumper/MyLoader 邏輯備份恢復MySql
- Oracle 備份 與 恢復 概述Oracle
- DB的備份與恢復
- GitLab的備份與恢復Gitlab
- MySQL 非常規恢復與物理備份恢復MySql
- Mysql備份與恢復(2)---邏輯備份MySql
- MongoDB日常運維-02安裝MongoDB運維
- MySQL備份與恢復——基於OUTFILE /LOAD DATA 邏輯備份恢復MySql
- 分散式文件儲存資料庫之MongoDB備份與恢復分散式資料庫MongoDB
- GitLab的自動備份、清理備份與恢復Gitlab
- 備份與恢復oracle_homeOracle
- 《入門MySQL—備份與恢復》MySql
- DB2備份與恢復DB2
- MySQL備份與恢復——實操MySql
- 入門MySQL——備份與恢復MySql
- RMAN備份與恢復測試
- MySQL備份與恢復操作解析MySql
- Mysql資料備份與恢復MySql
- mongodb資料庫備份與恢復(資料庫資料遷移)MongoDB資料庫
- mydumper備份恢復
- Mysql備份恢復MySql
- 備份和恢復
- Oracle 12c 備份與恢復Oracle
- Mysql的幾種備份與恢復MySql
- Elasticsearch叢集的備份與恢復Elasticsearch
- MySQL入門--備份與恢復(三)MySql
- MySQL入門--備份與恢復(一)MySql