MongoDB 备份计划

发布于 2024-09-25 08:32:41 字数 442 浏览 1 评论 0原文

我想从 MySQL 切换到 MongoDB,但大量数据丢失(超过 1 小时)对我来说是不可接受的。

我需要有 3 个备份计划:

  1. 每小时备份计划。数据每 X 分钟刷新到磁盘,如果服务器出现问题,我将确保重新启动后它将拥有至少一个小时前的所有数据。我可以配置它吗?

  2. 每日备份计划。数据每天都会同步到备份磁盘,因此即使服务器爆炸,我也可以在几个小时内恢复昨天的数据。我应该使用fsync、主从还是其他什么?我希望流量最小化,因此最好只发送更改。

  3. 每周备份计划。数据同步到第二个备份磁盘,因此如果服务器和第一个备份磁盘都爆炸,我至少有上周的数据。这里涉及到可靠性问题,所以通过网络发送所有数据是可以的。

我该怎么做呢?

I want to switch from MySQL to MongoDB but great data losses (more than 1 hour) are not acceptable for me.

I need to have 3 backup plans:

  1. Hourly backup plan. Data is flushed to disk every X minutes and if something wrong with the server I shall be sure that after reboot it will have all data at least for an hour ago. Can I configure it?

  2. Daily backup plan. Data is synced to backup disk every day so even if server explodes I can recover data for yesterday in some hours. Should I use fsync, master-slave or something else? I would like to have minimal traffic so ideally only changes will be sent.

  3. Weekly backup plan. Data is synced to second backup disk so if both server and first backup disk explode I have at least data for last week. Here this is the question of reliability so it's ok to send all data via network.

How can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无所谓啦 2024-10-02 08:32:41
  1. fsync 命令将数据刷新到磁盘。默认情况下每 60 秒执行一次,但可以使用 --syncdelay 命令行参数进行配置。

  2. 备份文档对每日和每周备份提供了一些很好的指导。对于每日备份,主从配置似乎是最好的选择,因为它只会同步更改。

  3. 对于每周备份,您还可以使用主从配置或复制。另一个选择是 mongodump 实用程序,它将支持 -整个数据库。它能够在数据库运行时创建备份,因此您可以在主数据库或从属数据库之一上运行它。您还可以在备份之前锁定从属设备。 对于每周备份

  1. The fsync command flushes the data to disk. It is executed each 60 seconds by default, but can be configured using the --syncdelay command line parameter.

  2. The documentation on backups has some good pointers for daily and weekly backups. For the daily backup, a master-slave configuration seems like the best option, as it will only sync changes.

  3. For the weekly backup you can also use a master-slave configuration, or replication. Another option is the mongodump utility, which will back-up the entire database. It is capable of creating backups while the database is running, so you can run it on the main database or one of the slaves. You can also lock the slave before backing it up.

何以畏孤独 2024-10-02 08:32:41

如果您想完全外包备份解决方案,MongoDB 管理服务每六个小时拍摄一次快照。快照的默认保留策略将允许您获取 24 小时的时间点还原、一周的每日快照、一个月的每周快照以及一年的每月快照。

常见问题解答具有完整的保留策略。

备份服务通过读取 oplog 来持续备份副本集,因此开销低于完整的本地定期快照。

If you want to outsource the backup solution entirely, MongoDB Management Service takes snapshots every six hours. The default retention policy on the snapshots will allow you to get point-in-time restore for 24 hours, daily snapshots for a week, weekly snapshots for a month, and monthly snapshots for a year.

This FAQ has the full retention policy.

The backup service continually backs up your replica set by reading the oplog so the overhead is lower than full local periodic snapshots.

梦冥 2024-10-02 08:32:41

也许您可以使用 automongobackup

May be you can use automongobackup .

拒绝两难 2024-10-02 08:32:41

如果您想创建从从 mongodb 数据库到 S3 的备份,请尝试此备份脚本。

  1. 数据库主机(首选辅助数据库主机,以避免影响主要性能)

    HOST='SomeHost/mongodbtest-slave'

  2. 数据库名称
    DBNAME=***

  3. S3 存储桶名称
    BUCKET=*-备份

  4. Linux 用户帐户
    USER=ubuntu

  5. 当前时间
    TIME=/bin/date +%d-%m-%Y-%T

  6. 密码
    PASSWORD=somePassword#!2*1

  7. 用户名
    USERNAME=某个用户名

  8. 备份目录
    DEST=/home/ubuntu/tmp

  9. 备份目录的Tar文件
    TAR=$DEST/../$TIME.tar

  10. 创建备份目录(-p 以避免警告(如果已存在))
    /bin/mkdir -p $DEST

  11. 日志
    echo "在 $TIME 上将 $HOST/$DBNAME 备份到 s3://$BUCKET/";

  12. 从 mongodb 主机转储到备份目录

mongodump --port 27017 -d DBNAME -u USERNAME -p PASSWORD -o $DEST

  1. 创建备份目录的 tar
    /bin/tar cvf $TAR -C $DEST 。

  2. 上传 tar 到 s3
    /usr/bin/aws s3 cp $TAR s3://$BUCKET/

  3. 本地删除 tar 文件
    /bin/rm -f $TAR

  4. 删除备份目录
    /bin/rm -rf $DEST

全部完成
echo "备份可在 https://s3.amazonaws.com/$BUCKET/$TIME .tar

您可以使用上述步骤将它们放入 shell 可执行文件中,并使用 crontab 命令在任意时间间隔执行此文件。

Try this backup script if you want to create a backup from slave mongodb database to S3.

  1. DB host (secondary preferred as to avoid impacting primary performance)

    HOST='SomeHost/mongodbtest-slave'

  2. DB name
    DBNAME=***

  3. S3 bucket name
    BUCKET=*-backup

  4. Linux user account
    USER=ubuntu

  5. Current time
    TIME=/bin/date +%d-%m-%Y-%T

  6. Password
    PASSWORD=somePassword#!2*1

  7. Username
    USERNAME=someUsername

  8. Backup directory
    DEST=/home/ubuntu/tmp

  9. Tar file of backup directory
    TAR=$DEST/../$TIME.tar

  10. Create backup dir (-p to avoid warning if already exists)
    /bin/mkdir -p $DEST

  11. Log
    echo "Backing up $HOST/$DBNAME to s3://$BUCKET/ on $TIME";

  12. Dump from mongodb host into backup directory

mongodump --port 27017 -d DBNAME -u USERNAME -p PASSWORD -o $DEST

  1. Create tar of backup directory
    /bin/tar cvf $TAR -C $DEST .

  2. Upload tar to s3
    /usr/bin/aws s3 cp $TAR s3://$BUCKET/

  3. Remove tar file locally
    /bin/rm -f $TAR

  4. Remove backup directory
    /bin/rm -rf $DEST

All done
echo "Backup available at https://s3.amazonaws.com/$BUCKET/$TIME.tar

You can use the steps above put them in a shell executable file and execute this at any interval using crontab commands.

冰火雁神 2024-10-02 08:32:41

关于第一点。

MongoDB 有一个术语,例如“持久写入操作'。如果启用日志记录,则可能只会丢失尚未写入的数据日志。这是一个非常小的时间(默认为 100 毫秒)

关于第二点和第三点。

您可以设置主从复制,但这不会保护您免受数据错误的影响(例如,如果重要的话)数据被意外删除)。因此,您需要以某种方式提供定期备份。

这里有几种方法:

  1. 如果您的文件系统支持 LVM 快照并且在 mongoDB 中启用了日志记录,那么 LVM 快照是一个很好的解决方案。有关更多详细信息,请参阅此处
  2. mongodump -您可以创建一个shell 脚本将通过 cron 运行计划备份并将其发送到存储。这是一个一个好的脚本示例
  3. 备份即服务。有许多解决方案可以为您进行备份。

On the first point.

MongoDB has a term like 'durable write operation'. If journaling is enabled, you may only lose data that has not been writed in the log. This is a very small amount of time (100 milliseconds by default)

On the second and third points.

You can set master slave replication, but this will not protect you from data errors (for example, if important data is accidentally deleted). Therefore, you need to provide regular backups one way or another.

There are several approaches here:

  1. LVM snapshot is a good solution if your filesystem supports it and the journaling is enabled in your mongoDB. For more details see here
  2. mongodump -You can create a shell script that will run scheduled backups via cron and send them to storage. Here is an example of a good script
  3. Backup as a Service. There are many solutions to make the backup for you.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文