How to backup and restore Mongodb database

How to use C # to backup and restore Mongodb database? So far there is only a server for saving mongodb data, and I want to make a backup in case the server is destroyed so that I can restore it.

Does anyone know how to do this with C #?

+3
source share
4 answers

If you just want automatic 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, just run the command below. You can put it in statup-script / daemon to execute it at regular frequencies:

Backup:

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

Recovery:

 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 
+3
source

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

In backup use mongodump :

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

In recover use mongorestore :

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

Make a backup using mongodump using the following command

mongodump -d data_name -o directory_where_mongodb_exists

To restore the use of this command

mongorestore -d database_name mongodb_restore_directory

0
source

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

First, make sure your MongoDB installation directory is in the environment path and you can access the mongodump command . MongoDB is usually installed in ~\ProgramFiles\MongoDB\Server\<vr>\bin on Windows.

Once the utility is accessible from CMD, go to the directory in which you want to save the backup (or reset).

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

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

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

Now, to restore, follow these steps. Notice that I supported a Mongo instance running on Azure (Mongo Labs) and restored it on my local machine as follows. In the same directory (on cmd) do the following:

mongorestore /host:localhost /port:27017

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

Hope this helps!

0
source

Source: https://habr.com/ru/post/905586/


All Articles