NoSQL database case combat - MongoDB data backup and recovery

Migrate MySQL data to MongoDB database

Preface

This environment is based on the Centos 7.8 system to build mongodb-enterprise-4.2.8 learning environment For
specific construction, please refer to mongodb-enterprise-4.2.8 environment construction


1. Data backup

MongoDB data backup

  • Logical backup mongodump
  • You can use mongodump to do a database or table-level backup of MongoDB. The following example illustrates the backup of the my_mongodb database.

Actual case

[root@localhost ~]# mongodump -d my_mongodb
2019-10-28T10:41:59.147+0800 writing my_mongodb.user to
2019-10-28T10:41:59.148+0800 done dumping my_mongodb.user (5 documents)
此时会在当前目录下创建一个 dump 目录,用于存放备份出来的文件
[root@localhost ~]# ll dump/my_mongodb/
总用量 8
-rw-r--r-- 1 root root 280 10月 28 10:41 user.bson
-rw-r--r-- 1 root root 87 10月 28 10:41 user.metadata.json
指定备份存放目录
[root@localhost ~]# mongodump -d my_mongodb -o my_mongodb_dump
2019-10-28T10:43:35.826+0800 writing my_mongodb.user to
2019-10-28T10:43:35.827+0800 done dumping my_mongodb.user (5 documents)
其他参数:
-h: MongoDB 所在服务器地址,例如,127.0.0.1。 当然也可以指定端口号:127.0.0.1:27017
--port:端口号。
-d: 需要备份的数据库实例,例如, test 。
-c: 需要备份的集合 。
-o: 备份数据的存放位置 。
-u: 用户名 。
-P: 密码。
--gzip: 压缩 。
--oplog: point in time 恢复用,只支持全库备份 。
--authenticationDatabase: 认证库 。
--dumpDbUsersAndRoles
dump 用户和角色 ,只有在单库备份时才需要这么做。
--archive=dbname.gz: 3.2 版本新增,不能和-o同时使用 。
归档备份为 l 个文件,但不能和-o 同时使用。

Full database backup

mongodump -h 192.168.150.15:27017 -uroot -proot --authenticationDatabase admin
-o /home/mongod/backup/full

Backup test library

mongodump -h 192.168.150.15:27017 -uroot -proot --authenticationDatabase admin
-d test -o /home/mongod/backup/

Back up the vast collection under the test library

mongodump -h 192.168.150.15:27017 -uroot -proot --authenticationDatabase admin
-d test -c vast -o /home/mongod/backup/

Compressed backup library

mongodump -h 192.168.150.15:27017 -uroot -proot --authenticationDatabase admin
-d test -o /home/mongod/backup/ --gzip

Compressed backup single table

mongodump -h 192.168.150.15:27017 -uroot -proot --authenticationDatabase admin
-d test -c vast -o /home/mongod/backup/ --gzip

Two, data recovery

MongoDB data recovery

  • mongorestore is similar to mongoimport parameters

Actual case

Restore a single database in the full database backup (based on the previous full database backup)

mongorestore -h 192.168.150.15:27017 -uroot -proot --authenticationDatabase
admin -d test --drop /home/mongod/backup/full/test/

Restore the test library

mongorestore -h 192.168.150.15:27017 -uroot -proot --authenticationDatabase
admin -d test /home/mongod/backup/test/

Restore the vast collection under the test library

mongorestore -h 192.168.150.15:27017 -uroot -proot --authenticationDatabase
admin -d test -c vast /home/mongod/backup/test/vast.bson

--Drop parameter practice recovery

# 恢复单库
mongorestore -h 192.168.150.15:27017 -uroot -proot --authenticationDatabase
admin -d test --drop /home/mongod/backup/test/
# 恢复单表
mongorestore -h 192.168.150.15:27017 -uroot -proot --authenticationDatabase
admin -d test -c vast --drop /home/mongod/backup/test/vast.bson

Guess you like

Origin blog.csdn.net/XY0918ZWQ/article/details/113838021