262_Mongodb_備份恢復(fù)
評估備份方案兩個指標(biāo)
下面兩個指標(biāo)決定全備份和增備的時間間隔
1 RPO recovery point objective 數(shù)據(jù)庫可以承受多少時間的數(shù)據(jù)丟失 ?(增備)
2 RTO recovery time objective? 數(shù)據(jù)庫可以承受多長時間的停機(jī) (全備)
MongoDB的邏輯/物理備份
邏輯備份:通過命令導(dǎo)出/導(dǎo)入數(shù)據(jù)文件
物理備份: 直接復(fù)制硬盤上的數(shù)據(jù)文件,
邏輯備份 mongodump和mongorestore
a.介紹
mongodump能夠在MongoDB運行時進(jìn)行備份,它的工作原理是對運行的Mongodb做查詢,然后將所有查到的文檔寫入磁盤;無法遷移系統(tǒng)庫admin和local
直接對mongos & mongod進(jìn)行操作,備份的數(shù)據(jù)及數(shù)據(jù)結(jié)構(gòu)以BSON格式存儲
mongodump 命令依賴游標(biāo),每次備份將數(shù)據(jù)加載到內(nèi)存,吃性能(內(nèi)存/IO) 適合小型/單一數(shù)據(jù)庫備份中
mongodump產(chǎn)生的備份不一定是數(shù)據(jù)庫的實時快照,如果我們在備份時對數(shù)據(jù)庫進(jìn)行了寫入操作,則備份出來的文件可能不完全和Mongodb實時數(shù)據(jù)相等
b mongodump & mongorestore參數(shù)
$ mongodump –help
$ mongorestore –help
參數(shù)說明:
公共參數(shù)
-h
--host 指明數(shù)據(jù)庫宿主機(jī)的IP
--port? 數(shù)據(jù)庫端口
-u
--username 指明數(shù)據(jù)庫的用戶名
-p
--password 指明數(shù)據(jù)庫的密碼
-d
--db 指明數(shù)據(jù)庫的名字
-c
--collection 指明collection的名字
--authenticationDatabase 認(rèn)證數(shù)據(jù)庫
-j
--numParallelCollections= number of collections to dump in parallel (4 by default)
mongodump
-q
--query 指明導(dǎo)出數(shù)據(jù)的過濾條件
-o
--out 指明到要導(dǎo)出的文件名
--oplog 備份的同時備份oplog
--numParallelCollections= number of collections to dump in parallel (4 by default)
mongorestore
--dir
恢復(fù)備份文件存放的位置
--drop
先對目標(biāo)數(shù)據(jù)庫中數(shù)據(jù)刪除,在進(jìn)行恢復(fù)
c mongodump和mongorestore基本使用
全庫備份 mkdir /mongodb/backup -p mongodump -uroot -proot123 --port 27017 --authenticationDatabase admin -o /mongodb/backup --備份test庫 $ mongodump -uroot -proot123 --port 27017 --authenticationDatabase admin -d test -o /mongodb/backup/ --備份test庫下的log集合 $ mongodump -uroot -proot123 --port 27017 --authenticationDatabase admin -d test -c log -o /mongodb/backup/ --壓縮備份 mongodump -uroot -proot123 --port 27017 --authenticationDatabase admin -o /mongodb/backup/ --gzip --全備中恢復(fù)單庫 $ mongorestore -uroot -proot123 --port 27017 --authenticationDatabase admin -d world /mongodb/backup/test --gzip --全備中恢復(fù)單表 [mongod@db01 backup]$ mongorestore -uroot -proot123 --port 27017 --authenticationDatabase admin -d a -c t1 /mongodb/backup/test/city.bson.gz --gzip --drop表示恢復(fù)的時候把之前的集合drop掉(危險) $ mongorestore -uroot -proot123 --port 27017 --authenticationDatabase admin -d test --drop /mongodb/backup/test
2? 數(shù)據(jù)導(dǎo)出mongoexport & 導(dǎo)入mongoimport
與備份不同, 導(dǎo)出是以集合為單位, 只導(dǎo)出數(shù)據(jù)本身,并不能保證數(shù)據(jù)導(dǎo)出后的字段類型,且導(dǎo)出的數(shù)據(jù)可以直接閱讀 (json & csv格式可選)
導(dǎo)出與備份類似; 多了欲導(dǎo)出的字段和導(dǎo)出的文件格式
參數(shù)匯總
公共參數(shù)
-p
-h
-d
-c
--port –password
--host ip地址
--db 數(shù)據(jù)庫
--collection 集合
-u
-p
--username --password 用戶名 & 密碼
--authenticationDataabase 認(rèn)證數(shù)據(jù)庫
-f
--fields 導(dǎo)出字段,用逗號分庫,默認(rèn)導(dǎo)出所有字段
Mongoexport
-q
導(dǎo)出數(shù)據(jù)的篩選條件,默認(rèn)全部條件
--type
CSV JSON 默認(rèn)json
-o
--out 導(dǎo)出路徑
Mongoimport
--headerline
如果知道第一行是字段名,則無需導(dǎo)入,導(dǎo)入CSV時候使用,后面不需要加變量,與 –f 互斥
--file
導(dǎo)入文件路徑
--drop
導(dǎo)入前刪除
use test for(i=0;i<10000;i++){ db.log.insert({"uid":i,"name":"mongodb","age":6,"date":new Date()}); } 1.單表備份至json格式 mongoexport -uroot -proot123 --port 27017 --authenticationDatabase admin -d test -c log -o/mongodb/log.json 注:備份文件的名字可以自定義,默認(rèn)導(dǎo)出了JSON格式的數(shù)據(jù) 2. 單表備份至csv格式 如果我們需要導(dǎo)出CSV格式的數(shù)據(jù),則需要使用--type=csv參數(shù): mongoexport -uroot -proot123 --port 27017 --authenticationDatabase admin -d test -c log --type=csv -f uid,name,age,date -o /mongodb/log.csv 數(shù)據(jù)恢復(fù): 1.恢復(fù)json格式表數(shù)據(jù)到log1 mongoimport -uroot -proot123 --port 27017 --authenticationDatabase admin -d test -c log1 /mongodb/log.json 2.恢復(fù)csv格式的文件到log2 上面演示的是導(dǎo)入JSON格式的文件中的內(nèi)容,如果要導(dǎo)入CSV格式文件中的內(nèi)容,則需要通過--type參數(shù)指定導(dǎo)入格式,具體如下所示: 注意: (1)csv格式的文件頭行,有列名字 mongoimport -uroot -proot123 --port 27017 --authenticationDatabase admin -d test -c log2 --type=csv --headerline --file /mongodb/log.csv (2)csv格式的文件頭行,沒有列名字 --headerline:指明第一行是列名,不需要導(dǎo)入 mongoimport -uroot -proot123 --port 27017 --authenticationDatabase admin -d test -c log3 -j 4 --type=csv -f id,name,age,date --file /mongodb/log.csv
異構(gòu)mysql2MongoDB遷移_字段順序可能不準(zhǔn) 例 異構(gòu)平臺遷移案例(離線) mysql2mongodb world數(shù)據(jù)庫下city表進(jìn)行導(dǎo)出,導(dǎo)入到mongodb (1)mysql開啟安全路徑 vim /etc/my.cnf --->添加以下配置 secure-file-priv=/data/backup/ /etc/init.d/mysqld restart --重啟數(shù)據(jù)庫生效 (2)導(dǎo)出mysql的city表數(shù)據(jù) select * from world.city into outfile '/tmp/t100w.csv' fields terminated by ',' ENCLOSED BY '"' ; (3)獲取列信息 mysql> select table_name,group_concat(column_name) from information_schema.columns where table_schema='test' group by table_name order by null ; +------------+---------------------------+ | TABLE_NAME | group_concat(column_name) | | t100w | dt,id,k1,k2,num | +------------+---------------------------+ 在mongodb中導(dǎo)入備份 mongoimport -uroot -proot123 --port 27017 --authenticationDatabase admin -d test -c city --type=csv -f ID,Name,CountryCode,District,Population --file /tmp/t100w.csv use world db.t100w.find({});
將MySQL大量表遷移到MongoDB (1) 批量從MySQL導(dǎo)出多張表 mysqldump --fields-terminated-by ',' --fields-enclosed-by '"' world -T /tmp/ cd /data/backup rm -rf /data/backup/*.sql find ./ -name "*.txt" | awk -F "." '{print $2}' | xargs -i -t mv ./{}.txt ./{}.csv (2) 拼接語句 select concat("mongoimport -uroot -proot123 --port 27017 --authenticationDatabase admin -d ",table_schema, " -c",table_name ," --type=csv "," -f ", group_concat(column_name) ," --file /data/backup/",table_name ,".csv") from information_schema.columns where table_schema='world' group by table_name; (3) 導(dǎo)入數(shù)據(jù) [mongod@db01 backup]$ ll total 256 -rwxrwxrwx 1 mysql mysql 184355 Jul 22 13:45 city.csv -rwxrwxrwx 1 mysql mysql 38659 Jul 22 13:45 country.csv -rwxrwxrwx 1 mysql mysql 26106 Jul 22 13:45 countrylanguage.csv -rwxrwxrwx 1 mysql mysql 656 Jul 22 13:45 import.sh [mongod@db01 backup]$ sh import.sh
MongoDB 數(shù)據(jù)庫
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。