Database Manual / Self-Managed Deployments / Administration / Backup Methods

Restore a Self-Managed Replica Set from MongoDB Backups从MongoDB备份还原自我管理副本集

This procedure outlines the process for taking MongoDB data and restoring that data into a new replica set. Use this approach for seeding test deployments from production backups or as part of disaster recovery.此过程概述了获取MongoDB数据并将该数据还原到新副本集中的过程。使用此方法从生产备份或作为灾难恢复的一部分播种测试部署。

Important

You cannot restore a single data set to three new mongod instances and then create a replica set. 您不能将单个数据集还原到三个新的mongod实例,然后创建副本集。If you copy the data set to each mongod instance and then create the replica set, MongoDB will force the secondaries to perform an initial sync. 如果你将数据集复制到每个mongod实例,然后创建副本集,MongoDB将强制次级执行初始同步The procedures in this document describe the correct and efficient ways to deploy a restored replica set.本文档中的过程描述了部署还原副本集的正确有效方法。

You can also use mongorestore to restore database files using data created with mongodump. 您还可以使用mongorestore使用mongodump创建的数据还原数据库文件。See Back Up and Restore a Self-Managed Deployment with MongoDB Tools for more information.有关更多信息,请参阅使用MongoDB工具备份和还原自我管理部署

Considerations注意事项

Backups provide a snapshot of the current state of the database. When you restore from a backup, the restored database doesn't include any changes made after the backup was taken, which can result in data loss.备份提供数据库当前状态的快照。从备份还原时,还原的数据库不包括备份后所做的任何更改,这可能会导致数据丢失。

Restore Database into a Single Node Replica Set将数据库还原到单节点副本集中

1

Obtain backup MongoDB Database files.获取备份MongoDB数据库文件。

The backup files may come from a file system snapshot. The MongoDB Cloud Manager produces MongoDB database files for stored snapshots and point in time snapshots. 备份文件可能来自文件系统快照MongoDB云管理器存储的快照时间点快照生成MongoDB数据库文件。For Ops Manager, an on-premises solution available in MongoDB Enterprise Advanced, see also the Ops Manager Backup overview.对于MongoDB Enterprise Advanced中提供的本地解决方案Ops Manager,另请参阅Ops Manager备份概述

Considerations for Encrypted Storage Engines加密存储引擎的注意事项
For encrypted storage engines that use AES256-GCM encryption mode, AES256-GCM requires that every process use a unique counter block value with the key.对于使用AES256-GCM加密模式的加密存储引擎,AES256-GCM要求每个进程在键中使用唯一的计数器块值。For encrypted storage engine configured with AES256-GCM cipher: 对于配置了AES256-GCM密码的加密存储引擎
  • Restoring from Hot Backup从热备份恢复
    Starting in 4.2, if you restore from files taken via "hot" backup (i.e. the mongod is running), MongoDB can detect "dirty" keys on startup and automatically rollover the database key to avoid IV (Initialization Vector) reuse.从4.2开始,如果您从通过“热”备份(即mongod正在运行)获取的文件进行还原,MongoDB可以在启动时检测“脏”键,并自动滚动数据库键以避免IV(初始化向量)重用。
  • Restoring from Cold Backup从冷备份恢复

    However, if you restore from files taken via "cold" backup (i.e. the mongod is not running), MongoDB cannot detect "dirty" keys on startup, and reuse of IV voids confidentiality and integrity guarantees.但是,如果您从通过“冷”备份获取的文件中还原(即mongod未运行),MongoDB在启动时无法检测到“脏”键,重复使用IV会使机密性和完整性保证失效。

    Starting in 4.2, to avoid the reuse of the keys after restoring from a cold filesystem snapshot, MongoDB adds a new command-line option --eseDatabaseKeyRollover. 从4.2开始,为了避免从冷文件系统快照恢复后重用键,MongoDB添加了一个新的命令行选项--eseDatabaseKeyRolloverWhen started with the --eseDatabaseKeyRollover option, the mongod instance rolls over the database keys configured with AES256-GCM cipher and exits.当使用--eseDatabaseKeyRollover选项启动时,mongod实例会滚动使用AES256-GCM密码配置的数据库键并退出。

2

Drop the local database if it exists in the backup.如果备份中存在local数据库,请删除该数据库。

If you are restoring from a filesystem backup (or any backup with the local database), drop the local database.如果要从文件系统备份(或任何具有local数据库的备份)还原,请删除local数据库。

Start a standalone mongod using the data files from the backup as the data path.使用备份中的数据文件作为数据路径启动独立的mongod

You must also specify the same startup options that were used when the snapshot was created.您还必须指定创建快照时使用的启动选项

mongod --dbpath /data/db <startup options>

Drop the local database.删除local数据库。

Connect mongosh to the mongod instance and drop the local database.mongosh连接到mongod实例并删除local数据库。

use local
db.dropDatabase()

Shut down the standalone.关闭单机。

3

Start a new single-node replica set.启动新的单节点副本集。

Start a mongod instance as a new single-node replica set. mongod实例作为新的单节点副本集启动。Specify the path to the backup data files with --dbpath option and the replica set name with the --replSet option. 使用--dbpath选项指定备份数据文件的路径,使用--replSet选项指定副本集名称。For config server replica set (CSRS), include the --configsvr option. 对于配置服务器副本集(CSRS),请包含--configsvr选项。Include any other options as appropriate for your deployment.根据部署情况,包括任何其他选项。

You must also specify the same startup options that were used when the snapshot was created.您还必须指定创建快照时使用的启动选项

Note

If your replica set members are run on different hosts or if you wish remote clients to connect to your instance, you must specify the net.bindIp setting (or --bind_ip).如果副本集成员在不同的主机上运行,或者您希望远程客户端连接到实例,则必须指定net.bindIp设置(或--bind_ip)。

Warning

Before you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access. 在将实例绑定到可公开访问的IP地址之前,必须保护集群免受未经授权的访问。For a complete list of security recommendations, see Security Checklist for Self-Managed Deployments. 有关安全建议的完整列表,请参阅自我管理部署的安全检查表At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证加强网络基础设施

mongod --dbpath /data/db --replSet <replName> <startup options>

Note

All MongoDB collections have UUIDs by default. When MongoDB restores collections, the restored collections retain their original UUIDs. When restoring a collection where no UUID was present, MongoDB generates a UUID for the restored collection.默认情况下,所有MongoDB集合都有UUID。当MongoDB恢复集合时,恢复的集合将保留其原始UUID。当还原不存在UUID的集合时,MongoDB会为还原的集合生成UUID。

For more information on collection UUIDs, see Collections.有关集合UUID的更多信息,请参阅集合

4

Connect mongosh to the mongod instance.mongosh连接到mongod实例。

From the same machine where one of the mongod is running (in this tutorial, mongodb0.example.net), start mongosh. 在运行mongod的同一台机器上(在本教程中为mongodb0.example.net),启动mongoshTo connect to the mongod listening to localhost on the default port of 27017, simply issue:要连接到默认端口27017上监听本地主机的mongod,只需发出:

mongosh

Depending on your path, you may need to specify the path to the mongosh binary.根据路径,您可能需要指定mongosh二进制文件的路径。

If your mongod is not running on the default port, specify the --port option for mongosh.如果mongod没有在默认端口上运行,请为mongosh指定--port选项。

5

Initiate the new replica set.启动新的副本集。

Use rs.initiate() on one and only one member of the replica set:对副本集的一个且仅一个成员使用rs.initiate()

rs.initiate( {
_id : <replName>,
members: [ { _id : 0, host : <host:port> } ]
})

MongoDB initiates a set that consists of the current member and that uses the default replica set configuration.MongoDB启动一个由当前成员组成的集合,该集合使用默认的副本集配置。

Add Members to the Replica Set将成员添加到副本集

MongoDB provides two options for restoring secondary members of a replica set:MongoDB提供了两种恢复副本集次要成员的选项:

Note

If your database is large, initial sync can take a long time to complete. For large databases, it might be preferable to copy the database files onto each host.如果数据库很大,初始同步可能需要很长时间才能完成。对于大型数据库,最好将数据库文件复制到每个主机上。

Copy Database Files and Restart mongod Instance复制数据库文件并重新启动mongod实例

Use the following sequence of operations to "seed" additional members of the replica set with the restored data by copying MongoDB data files directly.使用以下操作序列,通过直接复制MongoDB数据文件,用恢复的数据为副本集的其他成员“播种”。

1

Shut down the mongod instance that you restored.关闭您恢复的mongod实例。

Use --shutdown or db.shutdownServer() to ensure a clean shut down.使用--shutdowndb.shutdownServer()来确保干净关闭。

2

Copy the primary's data directory to each secondary.将主服务器的数据目录复制到每个辅助服务器。

Copy the primary's data directory into the dbPath of the other members of the replica set.数据目录复制到副本集其他成员的dbPath中。

3

Start the mongod instance that you restored.启动您恢复的mongod实例。

4

Add the secondaries to the replica set.将次要对象添加到副本集中。

In a mongosh session that is connected to the primary, add the secondaries to the replica set using the rs.add() method. 在连接到primarymongosh会话中,使用rs.add()方法将secondaries添加到副本集中。See Deploy a Self-Managed Replica Set for more information about deploying a replica set.有关部署副本集的详细信息,请参阅部署自我管理副本集

Update Secondaries using Initial Sync使用初始同步更新借调人员

Use the following sequence of operations to "seed" additional members of the replica set with the restored data using the default initial sync operation.使用以下操作序列,使用默认的初始同步操作,用还原的数据为副本集的其他成员“播种”。

1

Empty the data directory for each prospective replica set member.清空每个预期副本集成员的数据目录。

For example, if the replica set member has a storage.dbPath or --dbpath of /data/db, you must ensure that directory exists and is empty.例如,如果副本集成员的storage.dbPath--dbpath/data/db,则必须确保目录存在且为空。

2

Start each replica set member.启动每个副本集成员。

3

Add each prospective member to the replica set.将每个潜在成员添加到副本集中。

Connect to the primary using the mongo shell and add each secondary to the replica set using rs.add().使用mongoshell连接到primary,并使用rs.add()将每个辅助服务器添加到副本集中。

When you add a member to the replica set, Initial Sync copies the data from the primary to the new member.将成员添加到副本集时,初始同步会将数据从主成员复制到新成员。