mongodb backup and recovery (on) --- mongodb ttlsa Tutorial Series (eight)

Regular backups of data is necessary, the importance of data is self-evident, pigs will know on Earth. mongodb backup in three ways, respectively, when it comes to the following: 1. 2. Copy the file snapshots 3. Use a data file mongodump a way to back up a single file snapshot mongodb 1. This is the easiest way to backup method. However, the need to support file system snapshots and mongod must be enabled journal. If you are in line with these two requirements, you can create a snapshot at any time. Recovery, to ensure that no run mongod, snapshot restore command and then start the process mongod, mongod will replay journal log. 2. Copy data files directly copy all the files in the data directory. But you must stop data file copy process changes. It is necessary to lock the database, to prevent data writing.
> db.fsyncLock()
The above command will block write operations, and dirty data is flushed to disk to ensure data consistency. Then, copy data files to the backup directory
# cp -R /data/db/* /backup
After the files are copied, the database is unlocked, allowing the write operation
> db.fsyncUnlock()
Note: when performing db.fsyncLock () and db.fsyncUnlock (), the shell can not close the current window, or it may not connect mongod need to restart the service. Recovery, ensure mongod not running, clearing the data directory and copy the backup data to the data directory, then start mongod
# cp -R /backup/* /data/db/
# mongod -f mongod.conf
3. Do not use mongodump way fsyncLock use mongodump with, if the database is locked, mongodump will always hang. Use mongodump slower backup, backup replication set Shihai some issues, we will discuss the follow-up. However, to back up a single database, a collection, or a subset of a better way.
# ./mongodump --help
options:
  --help                                显示帮助信息
  -v [ --verbose ]                      打印出更多信息,如时间等等 -vvvvv
  --version                             打印版本信息
  -h [ --host ] arg                     指定连接的mongodb主机,复制集时设置为<set name>/s1,s2
  --port arg                            指定mongodb端口号,也可以这么指定--host hostname:port
  --ipv6                                启用支持IPv6 support
  -u [ --username ] arg                 用户名
  -p [ --password ] arg                 密码
  --authenticationDatabase arg          user source (defaults to dbname)
  --authenticationMechanism arg (=MONGODB-CR)
                                        authentication mechanism
  --dbpath arg                          直接访问mongod的数据库文件,而不是连接到mongodb服务器。需要锁定数据目录,如果mongod当前在访问相同路径将不能使用。也就是说,mongod运行的情况下不能使用--dbpath,mongod未运行的情况下可以直接指定--dbpath
  --directoryperdb                      每个db一个单独的目录,需要指定dbpath 
  --journal                             启用journaling,需要指定dbpath
  -d [ --db ] arg                       指定数据库
  -c [ --collection ] arg               指定集合
  -o [ --out ] arg (=dump)              指定输出目录,"-"表示标准输出
  -q [ --query ] arg                    json查询
  --oplog                               使用oplog来生产时间点快照
  --repair                              尝试恢复崩溃的数据库
  --forceTableScan                      强制表扫描,不使用$snapshot
# mongodump -p 27017
Will be created in the current directory dump directory, back up all databases, all data is stored in .bson file, you can use tools bsondump mongodb provided to retrieve it. mongod not running the case:
# mongodump --dbpath /data/db
When restored, the use of tools to recover mongorestore
# ./mongorestore --help   //相同部分参数意义参加上面的mongodump
  -v [ --verbose ]                      
  --version                             
  -h [ --host ] arg                     
  --port arg                            
  --ipv6                                
  -u [ --username ] arg                 
  -p [ --password ] arg                 
  --authenticationDatabase arg          
  --authenticationMechanism arg (=MONGODB-CR)
  --dbpath arg                         
  --directoryperdb                      
  --journal 
  -d [ --db ] arg  
  -c [ --collection ] arg  
  --objcheck                            在插入前验证对象,默认启用
  --noobjcheck                          不在插入前验证对象
  --filter arg                          插入前过滤
  --drop                                在插入前删除所有文档
  --oplogReplay                         在恢复时应用oplog
  --oplogLimit arg                      include oplog entries before the 
                                        provided Timestamp (seconds[:ordinal]) 
                                        during the oplog replay; the ordinal 
                                        value is optional
  --keepIndexVersion                    don't upgrade indexes to newest version
  --noOptionsRestore                    don't restore collection options
  --noIndexRestore                      don't restore indexes
  --w arg (=0)                          minimum number of replicas per write
Restore the entire database:
# mongorestore -p 27017 dump/
To restore to a library and collections:
# mongorestore --db ttlsa_com --collection posts dump/old_ttlsa_com/old_posts.bson
Note: mongodump and mongorestore version of the best match. II. Normally set the backup copy, back up in the secondary, primary load reduction, only the secondary lock, so as not to affect the service (assuming no sends a read request to the secondary). You can use any way to back up above, but is recommended to use a snapshot file and copy the data files the way. Use mongodump backup, a problem mentioned above, it is in mongodump backup process, the occurrence of a write operation. In the replication set architecture environment, to prevent this from happening, mongodump --oplog need to add parameters to track all actions when the backup occurred on the server, get a pointin-time snapshot of the state or the backup will be with the other cluster It does not match the node. When restoring, you must also create oplog, and specify --oplogReplay parameters to apply these operations, otherwise the members of the recovery will not know where to begin synchronized to be consistent with the source server at some point in time. When the backup copying set, may be provided connected mongodump "setName / s1, s2, s3", it automatically selects an available secondary backup. Backup copy sets:
# mongodump -h "ttlsa/10.1.1.155,10.1.1.156,10.1.1.157" --oplog -o /backup/mongodbbackup/
Recovery replication set: 1. Copy the members of the focus to be restored to remove cluster 2. Run the command mongorestore --oplogReplay
# mongorestore --oplogReplay dump/
3. Create oplog
> use local
> db.createCollection("oplog.rs", {"capped" : true, "size" : 10000000})
4. Restore oplog
# mongorestore -d local -c oplog.rs dump/oplog.bson
Note: oplog.bson not located dump / local / oplog.rs.bson, oplog.bson recording operation takes place during mongodump. 5. Add the cluster node to the copy set copied sets as explained in section http://www.ttlsa.com/html/1679.html three Backup fragment at the cluster fragmentation is not possible at a time point get a complete snapshot of the cluster state. When the cluster is growing, a chance to restore the entire backup infrastructure from smaller and smaller. Therefore, for the backup of cluster fragmentation, simply copy the backup config server and set alone. In front of the slice cluster backup and recovery operations, to turn off balancer. For smaller slice cluster, you can back up and restore directly from mongos. In most cases, we only need to recover a node in the cluster. If you need to restore the entire cluster, you bad enough, the entire cluster is relatively small possibility of data loss. When backing up, direct connection mongod slice rather than by the cluster mongos. For relatively small cluster fragments, may be connected directly to mongos mongodump backup, the backup file will contain actual data and metadata information server config. For large fragment cluster, the backup steps as follows: 1. Close balancer. Note: not connected to mongos config server instance.
> sh.setBalancerState(false) 或
> sh.stopBalancer() 或
> use config
> db.settings.update( { _id: "balancer" }, { $set : { stopped: true } } , true );
2. Back up the cluster metadata. Use mongodump backup Renyiyitai config server. Examples mongod Renyiyitai config can be directly connected, or may be connected by mongos.
# mongodump --db config
3. Back up the replica set within each shard cluster. It can be executed in parallel. 4. Enable balancer. Note: not connected to mongos config server instance.
> sh.setBalancerState(true) 或
> sh.startBalancer() 或
> use config
> db.settings.update( { _id: "balancer" }, { $set : { stopped: false } } , true );
Part II "mongodb backup and recovery (under)," will provide an online backup script (mongodb applicable to any architecture) and the realization of incremental backups. For reprint please indicate the source: MongoDB backup and recovery (on)   http://www.ttlsa.com/html/1938.html

Reproduced in: https: //my.oschina.net/766/blog/211372

Guess you like

Origin blog.csdn.net/weixin_34405354/article/details/91548099