In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
How to carry out mongdb backup and recovery, in view of this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
I. physical backup (copying files)
#! / bin/shDATE= `date +% Y% m% d`Del _ DATE=$ (date-d'- 30 days' "+% Y%m%d") HOST=10.10.17.27PORT=27000USER=adminPASSWORD=123456DATA_DIR= "/ data/mongodb/navy_db/db" BACKUP_PATH= "/ data/mongodbbackup/$DATE" date +% Y%m%d%H%M > > / data/log/mongodb_bak.log# first step lock table lock () {echo "db.fsyncLock ()" | mongo-- host $HOST-- port $PORT- U $USER-p $PASSWORD admin} execute () {lock if [$?-eq 0] then echo "mongodb lock successfully!" > / data/log/mongodb_bak.log else echo "mongodb lock fail!" > / data/log/mongodb_bak.log fi} execute# second step backup back () {rsync-av $DATA_DIR $BACKUP_PATH/} execute () {back if [$?-eq 0] then echo "mongodb back successfully!" > > / data/log/mongodb_ Bak.log else echo "mongodb back fail!" > / data/log/mongodb_bak.log fi} execute# part III unlocks unlock () {echo "db.fsyncUnlock ()" | mongo-- host $HOST-- port $PORT-u $USER-p $PASSWORD admin} execute () {unlock if [$?-eq 0] then echo "mongodb unlock successfully!" > / data/log/mongodb_bak.log else echo "mongodb unlock fail!" > > / data/log/mongodb_bak.log fi } execute# deletes historical data rm-rf "/ data/mongodbbackup/$ {DEL_DATE} /"
PS:
Physical backup can only be restored in full database, not a single table.
Restore steps:
1. Stop the instance that needs to be restored 2, backup clear / data/mongodb/navy_db/db directory 3, copy all files and folders under / data/mongodbbackup/$DATE to / data/mongodb/navy_db/db directory 4, chown mongod:mongod / data/mongodb/navy_db/db/-R5, start the instance
II. Mongodump backup
#! / bin/shDATE= `date +% Y% m% d`Del _ DATE=$ (date-d'- 30 days' "+% Y%m%d") HOST=10.10.17.27:27000USER=adminPASSWORD=123456BACKUP_PATH= "/ backup/mongodbbackup/$DATE" date +% Y%m%d%H%M > > / data/log/mongodb_bak.logstart () {mongodump-h $HOST-u $USER-p $PASSWORD-o $BACKUP_PATH} execute () {start if [$?-eq 0] then echo "mongodbback successfully ! "> > / data/log/mongodb_bak.log else echo" mongodbback fail! "> > / data/log/mongodb_bak.log fi} executerm-rf" / backup/mongodbbackup/$ {DEL_DATE} / "
Restore:
1. Restore the whole database
Mongorestore-- host 10.10.17.27-- port 27000 / backup/mongodbbackup/20150818/
2. Recovery of single set (the concept of table)
Mongorestore-- host 10.10.17.27-- port 27001-- db navy_db_new-- collection navy_db_table_new / backup/mongodbbackup/20150818/navy_db/navy_db_table.bson
III. Backup of replication sets
#! / bin/shDATE= `date +% Y% m% d`Del _ DATE=$ (date-d'- 30 days' "+% Y%m%d") HOST= "dbset/10.10.17.27:27000,10.10.17.26:27000" USER=adminPASSWORD=123456BACKUP_PATH= "/ data/mongodbbackup/$DATE" date +% Y%m%d%H%M > > / data/log/mongodb_bak.logstart () {mongodump-h $HOST-u $USER-p $PASSWORD-oplog-o $BACKUP_PATH} execute () { Start if [$?-eq 0] then echo "mongodbback successfully!" > / data/log/mongodb_bak.log else echo "mongodbback fail!" > > / data/log/mongodb_bak.log fi} executerm-rf "/ backup/mongodbbackup/$ {DEL_DATE} /"
PS:
Backing up a replication set is similar to backing up a single mongodb instance using mongodump backup, except that the host is written differently, so you need to specify the replication set name and add-- oplog parameter to track all writes that occur on the master database during backup and obtain a pointin-time snapshot, otherwise the backup status will not match with other nodes in the cluster. When restoring, you must also create an oplog and specify the-- oplogReplay parameter to apply these operations, otherwise the restored members will not know where to start synchronization and will be consistent with the source server at some point in time.
Restore:
1. Remove the node to be restored in the replication set
Rs.remove ("10.10.17.26 27000")
2. Run the mongorestore-- oplogReplay command
Mongorestore-host 10.10.17.26-port 27000-oplogReplay / data/mongodbbackup/20150820/
3. Create oplog
Use local db.createCollection ("oplog.rs", {"capped": true, "size": 10000000})
4. Restore oplog
Mongorestore-- host 10.10.17.26-- port 27000-d local-c oplog.rs / data/mongodbbackup/20150820/oplog.bson
5. Add the node to the replication set
Rs.add ("10.10.17.26 27000") this is the answer to the question about how to perform mongdb backup and recovery. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.