MongoDB 备份计划
我想从 MySQL 切换到 MongoDB,但大量数据丢失(超过 1 小时)对我来说是不可接受的。
我需要有 3 个备份计划:
每小时备份计划。数据每 X 分钟刷新到磁盘,如果服务器出现问题,我将确保重新启动后它将拥有至少一个小时前的所有数据。我可以配置它吗?
每日备份计划。数据每天都会同步到备份磁盘,因此即使服务器爆炸,我也可以在几个小时内恢复昨天的数据。我应该使用fsync、主从还是其他什么?我希望流量最小化,因此最好只发送更改。
每周备份计划。数据同步到第二个备份磁盘,因此如果服务器和第一个备份磁盘都爆炸,我至少有上周的数据。这里涉及到可靠性问题,所以通过网络发送所有数据是可以的。
我该怎么做呢?
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:
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?
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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
fsync
命令将数据刷新到磁盘。默认情况下每 60 秒执行一次,但可以使用--syncdelay
命令行参数进行配置。备份文档对每日和每周备份提供了一些很好的指导。对于每日备份,主从配置似乎是最好的选择,因为它只会同步更改。
对于每周备份,您还可以使用主从配置或复制。另一个选择是 mongodump 实用程序,它将支持 -整个数据库。它能够在数据库运行时创建备份,因此您可以在主数据库或从属数据库之一上运行它。您还可以在备份之前锁定从属设备。 对于每周备份
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.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.
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.
如果您想完全外包备份解决方案,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.
也许您可以使用 automongobackup 。
May be you can use automongobackup .
如果您想创建从从 mongodb 数据库到 S3 的备份,请尝试此备份脚本。
数据库主机(首选辅助数据库主机,以避免影响主要性能)
HOST='SomeHost/mongodbtest-slave'
数据库名称
DBNAME=
***
S3 存储桶名称
BUCKET=*-备份
Linux 用户帐户
USER=
ubuntu
当前时间
TIME=
/bin/date +%d-%m-%Y-%T
密码
PASSWORD=
somePassword#!2*1
用户名
USERNAME=
某个用户名
备份目录
DEST=/home/ubuntu/tmp
备份目录的Tar文件
TAR=$DEST/../$TIME.tar
创建备份目录(-p 以避免警告(如果已存在))
/bin/mkdir -p $DEST
日志
echo "在 $TIME 上将 $HOST/$DBNAME 备份到 s3://$BUCKET/";
从 mongodb 主机转储到备份目录
mongodump --port 27017 -d DBNAME -u USERNAME -p PASSWORD -o $DEST
创建备份目录的 tar
/bin/tar cvf $TAR -C $DEST 。
上传 tar 到 s3
/usr/bin/aws s3 cp $TAR s3://$BUCKET/
本地删除 tar 文件
/bin/rm -f $TAR
删除备份目录
/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.
DB host (secondary preferred as to avoid impacting primary performance)
HOST='SomeHost/mongodbtest-slave'
DB name
DBNAME=
***
S3 bucket name
BUCKET=*-backup
Linux user account
USER=
ubuntu
Current time
TIME=
/bin/date +%d-%m-%Y-%T
Password
PASSWORD=
somePassword#!2*1
Username
USERNAME=
someUsername
Backup directory
DEST=/home/ubuntu/tmp
Tar file of backup directory
TAR=$DEST/../$TIME.tar
Create backup dir (-p to avoid warning if already exists)
/bin/mkdir -p $DEST
Log
echo "Backing up $HOST/$DBNAME to s3://$BUCKET/ on $TIME";
Dump from mongodb host into backup directory
mongodump --port 27017 -d DBNAME -u USERNAME -p PASSWORD -o $DEST
Create tar of backup directory
/bin/tar cvf $TAR -C $DEST .
Upload tar to s3
/usr/bin/aws s3 cp $TAR s3://$BUCKET/
Remove tar file locally
/bin/rm -f $TAR
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.
关于第一点。
MongoDB 有一个术语,例如“持久写入操作'。如果启用日志记录,则可能只会丢失尚未写入的数据日志。这是一个非常小的时间(默认为 100 毫秒)
关于第二点和第三点。
您可以设置主从复制,但这不会保护您免受数据错误的影响(例如,如果重要的话)数据被意外删除)。因此,您需要以某种方式提供定期备份。
这里有几种方法:
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: