点击上方“IT那活儿”公众号,关注后了解更多内容,不管IT什么活儿,干就完了!!!
2. mongodump/ mongorestore备份数据库,备份格式为bson、gzip,可读性不高,还原数据时遇到过和json兼容性问题,不建议使用。
1. 导出工具mongoexport
1.1 mongoexport参数说明
-h:指明数据库宿主机的IP --host:如果是集群环境建议使用,例如:--host="my_repl/10.0.0.10:28017,10.0.0.10:28018,10.0.0.10:28019" -u:指明数据库的用户名 -p:指明数据库的密码 -d:指明数据库的名字 -c:指明collection的名字 -f:指明要导出哪些列 -o:指明到要导出的文件名 -q:指明导出数据的过滤条件 --authenticationDatabase:保存用户凭据的数据库
1.2 单表备份为json格式
for(i=0;i<1000;i++){db.log.insert({"uid":i,"name":"mongodb","age":6})}
mongoexport -uroot -proot123456 --port 28017 --
authenticationDatabase admin -d test -c log -o /mongodb/backup/log.json
1.3 单表备份至csv格式
mongoexport -uroot -proot123456 --port 28017 --
authenticationDatabase admin -d test -c log --type=csv -f
uid,name,age -o /mongodb/backup/log.csv
2. 导入工具mongoimport
2.1 mongoimport参数说明
-h:指明数据库宿主机的IP -u:指明数据库的用户名 -p:指明数据库的密码 -d:指明数据库的名字 -c:指明collection的名字 -f:指明要导入哪些列 -j , --numInsertionWorkers=<number>:要同时运行的插入操作数 --type:指定导入格式 --headerline:使用输入源中的第一行作为字段列表(仅CSV和TSV)
2.2 恢复json格式表数据到log
mongoimport -uroot -proot123456 --port 28017 --
authenticationDatabase admin -d test -c log /mongodb/backup/log.json
2.3 恢复csv格式的文件到log
mongoimport -uroot -proot123456 --port 28017 --
authenticationDatabase admin -d test -c log --type=csv --headerline --file /mongodb/backup/log.csv
mongoimport -uroot -proot123456 --port 28017 --
authenticationDatabase admin -d test -c log --type=csv -f id,name,age --file /mongodb/backup/log.csv
2.4 异步架构迁移:MySQL2MongoDB
vim /etc/my.cnf
secure-file-priv=/tmp secure-file-priv=/tmp
/etc/init.d/mysqld restart
select * from world.city into outfile '/tmp/city.csv' fields terminated by ',';
mysql> use information_schema
mysql> desc COLUMNS;
mysql> select TABLE_SCHEMA,TABLE_NAME,group_concat(COLUMN_NAME) from COLUMNS where TABLE_SCHEMA='test' and TABLE_NAME='city';
mongoimport -uroot -proot123 --port 27017 --
authenticationDatabase admin -d world -c city --type=csv -f
ID,Name,CountryCode,District,Population --file /tmp/city1.csv
1. 备份工具mongodump
1.1 mongodump参数说明
-h:指明数据库宿主机的IP -u:指明数据库的用户名 -p:指明数据库的密码 -d:指明数据库的名字 -c:指明collection的名字 -o:指明到要导出的文件名 -q:指明导出数据的过滤条件 -j , --numParallelCollections:要并行转储的集合数(默认为4) --oplog:备份的同时备份oplog --gzip:压缩备份 --drop:恢复数据前先进行删除 --oplog:使用oplog获取时间点快照
1.2 全库备份
mongodump -uroot -proot123456 --port 28018 --
authenticationDatabase admin -o /mongodb/backup
mongodump -uroot -proot123456 --port 28018 --
authenticationDatabase admin -o /mongodb/backup/ --gzip
1.3 单库备份
mongodump -uroot -proot123456 --port 28018 --
authenticationDatabase admin -d test -o /mongodb/backup/
mongodump -uroot -proot123456 --port 28018 --
authenticationDatabase admin -d test -o /mongodb/backup/ --gzip
1.4 单表备份
mongodump -uroot -proot123456 --port 28018 --authenticationDatabase
admin -d test -c log -o /mongodb/backup/
mongodump -uroot -proot123456 --port 28018 --authenticationDatabase
admin -d test -c log -o /mongodb/backup/ --gzip
2. 恢复工具mongorestore
mongorestore -uroot -proot123456 --port 28018 --authenticationDatabase
admin -d test /mongodb/backup/test
mongorestore -uroot -proot123456 --port 28018 --authenticationDatabase
admin -d test -c log --gzip /mongodb/backup.bak/oldboy/log.bson.gz
3. mongodump和mongorestore高级应用(--oplog)
3.1 oplog的说明
my_repl:PRIMARY> use local
my_repl:PRIMARY> db.oplog.rs.find().pretty()
{
"ts" : Timestamp(1657440633, 1),
"h" : NumberLong(0),
"v" : 2,
"op" : "n",
"ns" : "",
"wall" : ISODate("2022-07-10T08:10:33.825Z"),
"o" : {
"msg" : "initiating set"
}
}
以上op字段代表的是:
"i": insert "u": update "d": delete "c": db cmd代表表及索引的增删改查等操作
test:PRIMARY> rs.printReplicationInfo()
configured oplog size: 1561.5615234375MB <--集合大小
log length start to end: 423849secs (117.74hrs) <--预计窗口覆盖时间
oplog first event time: Wed Sep 09 2015 17:39:50 GMT+0800 (CST)
oplog last event time: Mon Sep 14 2015 15:23:59 GMT+0800 (CST)
now: Mon Sep 14 2015 16:37:30 GMT+0800 (CST)
3.2 mongorestore结合oplog日志截取进行恢复
[mongod@db01 ~]$ mongo -uroot -proot123456 --port 28018 admin
use test
for(var i = 1 ;i < 2; i++) {
db.b.insert({id:i});
}
mongodump -uroot -proot123456 --port 28018 --
authenticationDatabase admin --oplog -o /mongodb/backup
db.b.insert({id:2})
db.b.insert({id:3})
db.b.drop()
show tables;
mongodump -uroot -proot123456 --port 28018 --authenticationDatabase admin
-d local -c oplog.rs -o /mongodb/backup
my_repl:PRIMARY> use local
my_repl:PRIMARY> db.oplog.rs.find({op:"c"}).pretty();
{
"ts" : Timestamp(1657519679, 1),
"t" : NumberLong(3),
"h" : NumberLong(0),
"v" : 2,
"op" : "c",
"ns" : "test.$cmd",
"ui" : UUID("2d290e84-8e61-4baf-ad05-3a57361f13e5"),
"o2" : {
"numRecords" : 19
},
"wall" : ISODate("2022-07-11T06:00:21.531Z"),
"o" : {
"drop" : "zhang"
}
}
cd /mongodb/backup/local/
\cp oplog.rs.bson ../oplog.bson
cd ..
rm -rf /mongodb/backup/local/
mongorestore -uroot -proot123456 --port 28018 --
authenticationDatabase admin --oplogReplay --oplogLimit
"1657519679:1" /mongodb/backup

本文作者:张学衡(上海新炬中北团队)
本文来源:“IT那活儿”公众号

文章转载自IT那活儿,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




