MongoDB 使用 Lambda 函数的副本集快照最佳实践

MongoDB Replica Set Snapshot with Lambda Function best practice

我对 MongoDB 很陌生,有点困惑。我正在尝试在 AWS 上创建一个用于生产的自动备份例程,我想确保我做的是正确的。

到目前为止,我已经设置了一个副本集,其中包含 1 个仲裁器和 3 个成员(1 个主要,1 个次要,1 个隐藏延迟 4 小时)。每个成员有 3 个独立的 EBS 卷(数据 100GB,日志 20GB,日志 10GB)。

我使用 NodeJS 创建了一个 Lambda 函数,运行 每小时(使用 CloudWatch Event)拍摄快照,执行以下操作:

  1. MongoClient连接到隐藏延迟会员mongodb://admin:password@ec2.private.ip:27017/admin
  2. 刷新所有挂起的写操作db.command({ fsync: 1, lock: true })
  3. 创建 EC2 连接以检索标记为 LambdaSnapshot:
  4. 的所有 EBS 卷
const ec2Conn = new AWS.EC2({ region: 'us-west-1' })
const params = {
  Filters: [
    {
      Name: "tag-key",
      Values: ["LambdaSnapshot"],
    },
  ],
};
const volumes = (await ec2Conn.describeVolumes(params).promise()).Volumes;
const volumeIds = volumes.map((volume) => volume.VolumeId);
  1. 在每个卷上创建快照:
return Promise.all(
  volumeIds.map(async (volumeId) => {
    const formattedDate = moment().format("DD/MM/YYYY HH:mm:ss");
    const snapshot = await ec2Conn
      .createSnapshot({
        Description: "Snapshot " + volumeId + " taken on " + formattedDate,
        VolumeId: volumeId,
        TagSpecifications: [
          {
            ResourceType: "snapshot",
            Tags: [
              {
                Key: "Name",
                Value: volume + " " + formattedDate,
              },
              {
                Key: snapshotTag,
                Value: volume + " " + formattedDate,
              },
            ],
          },
        ],
      })
      .promise();
    return snapshot.SnapshotId;
  });
);
  1. 使用 db.command({ fsyncUnlock: 1 })
  2. 解锁实例以进行写入

我有两个主要疑惑。

  1. 我已将包含隐藏延迟成员的数据(100GB 之一)的 EBS 卷标记为 LambdaSnapshot 。我不确定是否还必须拍摄日志和日志卷的快照。
  2. 我注意到即使我使用 await 到 运行 创建快照的命令,该函数仍然会继续并在快照处于 pending 时解锁实例状态。我不确定,但我认为命令 createSnapshot() 仅向 AWS 提供输入以启动快照并解决承诺,而无需等待完成。所以我怀疑快照完成后是否必须在 lambda 函数之外解锁数据库;在那种情况下,我不知道如何监听 运行 解锁数据库的第二个 lambda 函数的完整事件。

提前致谢

如前所述in docs EBS 快照创建是异步的:

Snapshots occur asynchronously; the point-in-time snapshot is created immediately, but the status of the snapshot is pending until the snapshot is complete (when all of the modified blocks have been transferred to Amazon S3), which can take several hours for large initial snapshots or subsequent snapshots where many blocks have changed. While it is completing, an in-progress snapshot is not affected by ongoing reads and writes to the volume.

Backup documentation 说:

To get a correct snapshot of a running mongod process, you must have journaling enabled and the journal must reside on the same logical volume as the other MongoDB data files. Without journaling enabled, there is no guarantee that the snapshot will be consistent or valid.

除非您有其他文档参考说明您不需要将日志或日志数据与所有其他数据一起备份,否则我建议将所有内容一起备份。