How to backup and restore the Mongodb database

旧城冷巷雨未停 提交于 2019-12-05 01:33:10

问题


How to use C# to backup and restore the Mongodb database? For now there is only an server to save the mongodb data, and I want to make an backup in case of the server is destroyed so that I can restore it.

Dose anybody know how to do that using C#?


回答1:


If you just want automated backups, there is an easier way than resorting to a full-fledged programming language:

http://docs.mongodb.org/manual/tutorial/backup-databases-with-filesystem-snapshots/

As shown in the link, the below command would suffice. You may put it in a statup-script/daemon to execute it at regular frequencies:

Backup:

lvcreate --size 100M --snapshot --name mdb-snap01 /dev/vg0/mongodb

Restore:

lvcreate --size 1G --name mdb-new vg0
gzip -d -c mdb-snap01.gz | dd of=/dev/vg0/mdb-new
mount /dev/vg0/mdb-new /srv/mongodb



回答2:


From the mongolab website (http://docs.mongolab.com/backups/) a useful/simple example:

To backup use mongodump:

% mongodump -h ds012345.mongolab.com:56789 -d dbname -u dbuser -p dbpassword -o dumpdir

To restore use mongorestore:

% mongorestore -h ds023456.mongolab.com:45678 -d dbname -u dbuser -p dbpassword dumpdir/*



回答3:


Take backup using mongodump use the following command

mongodump -d database_name -o directory_where_mongodb_exists

To restore use this command

mongorestore -d database_name mongodb_restore_directory




回答4:


In this answer, I will emphasize on delineating a few caveats that I experienced while doing this for the first time. Hope this proves useful to somebody in the same situation. Although, the example uses the command line, the utilities are available out-of-the-box with the Mongo installation and can very well be used with C#.

First of, ensure that your MongoDB installation directory is in the environment path and you are able to access the mongodump command. MongoDB is normally installed in ~\ProgramFiles\MongoDB\Server\<vr>\bin on Windows.

Once the utility is accessible from CMD, navigate to the directory where you'd like to save (or dump) the backup.

Check the mongodump --help for options. I used a simple (full) backup and restore. So, use the command and specify the required options. Here is an example:

mongodump /host:ds062199.mlab.com /port:62199 /username:mydbuser /password:mypwd /db:mydbname

The moment you hit enter, the utility will create BSON and metadata JSON backup files in a dump/dbname subdirectory.

Now, to restore, do the following. Note, I backed up a Mongo instance running on Azure (Mongo Labs) and restored it on my local machine as follows. While in the same directory (on the cmd), execute the following:

mongorestore /host:localhost /port:27017

And it will create a new db, as the replica of the backup on the localhost mongodb instance.

Hope this helps!



来源:https://stackoverflow.com/questions/15755608/how-to-backup-and-restore-the-mongodb-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!