Background

因為工作上有在使用Mongodb, 所以研究一下如何backup是最好的.

那當然為啥不用 mongodump / mongorestore 就好, 以下是一些原因, 所以才讓我一直在尋找比較適合的方法.

  • The mongodump and mongorestore utilities work with BSON data dumps, and are useful for creating backups of small deployments.

  • Because mongodump and mongorestore operate by interacting with a running mongod instance, they can impact the performance of your running database.

Methods

找了老半天還, 有以下幾種方法, 當然你可以用 MongoDB Cloud Manager 提供的service 但應該要錢就是了.

1. Snapshots

如果你有用AWS那直接用AWS提供的snapshots的功能就好. 因該比較方便. 那我查了一下Mongodb 3.2後好像也有提供類似的功能.

Back Up and Restore Using LVM on Linux

https://docs.mongodb.com/v3.2/tutorial/backup-with-filesystem-snapshots/#lvm-backup-and-restore

2. Back Up with cp or rsync

這就不用說明了吧. 蠻暴力的解法. 但是因為還包含了Index和其他有的沒有. 所以所需的storage size也相對的大很多

3. Back Up with mongodump

再來就是原有的指令, 在3.0 & 3.2之後有針對 mongodump 做些指令上的優化. 但還是提醒一下根據官網所說, 所以在使用前還是評估一下.

mongodump and mongorestore are simple and efficient tools for backing up and restoring small MongoDB deployments, but are not ideal for capturing backups of larger systems.

  • archive mode

In 3.2, the “archive” mode, which writes all the dump data, even from disparate databases and collections, to a single output file.

# single collection
mongodump --db country --collection nigeria --archive=nga.archive

# single db
mongodump --db country --archive=country.archive

# entire server
mongodump --archive=server.archive 
  • gzip

In addition to archiving, we’ve also added support for compression using gzip. This is exposed by the introduction of a new command line option “--gzip” in both mongodump and mongorestore.

# dump
mongodump --db country --gzip

# restore
mongorestore --gzip --archive=country.archive

請愛用gzip來搭配mongodump使用

4. Mongodump with Oplog

應該是可以利用 mongodump + Oplog 以及linux的一些script來達到分鐘級的steaming back up.

5. Replication

MongoDB handles replication through replication sets. Conceptually, replication sets are similar to the master-slave configuration explained in our MySQL post.

有興趣的就參考這篇文章吧:

Reference