Tutorial for backing up and restoring MongoDB data on Windows or Linux systems

  • 2020-05-30 21:15:10
  • OfStack

mongodb database backup and restore below windows

I can backup the data to the c:\data\dump directory, first create this path. Then go to mongodb's bin directory

My is:


C:\Program Files\mongodb\bin

The backup script is:


// The backup 
mongodump -h 127.0.0.1:27017 -d test -o c:\data\dump

The recovery script is:


// restore 
 mongorestore -h 127.0.0.1:27017 -d test --directoryperdb c:\data\dump\test

Explain the command used in step 1

-h: server address where MongoDB is located -d: the database instance that needs to be restored, for example, test. Of course, this name can be different from that of the backup, for example, test2 -o: backup data location, e.g. : c:\data\dump, of course, this directory needs to be set up in advance. After the backup is completed, the system will automatically set up an test directory under the dump directory to store the backup data of this database instance. --directoryperdb: backup data location, e.g. c:\data\dump\test, why add an test instead of dump at backup time? --drop: when restoring, first delete the current data, and then restore the backup data. That is to say, after the recovery, the backup after the addition of modified data will be deleted, careful!

Original explanation:


 -v [ --verbose ]           be more verbose (include multiple times
                    for more verbosity e.g. -vvvvv)
 --version               print the program's version and exit
 -h [ --host ] arg           mongo host to connect to ( <set
                    name>/s1,s2 for sets)
 --port arg              server port. Can also use --host
                    hostname:port
 --ipv6                enable IPv6 support (disabled by
                    default)
 -u [ --username ] arg         username
 -p [ --password ] arg         password
 --authenticationDatabase arg     user source (defaults to dbname)
 --authenticationMechanism arg (=MONGODB-CR)
                    authentication mechanism
 --dbpath arg             directly access mongod database files
                    in the given path, instead of
                    connecting to a mongod server - needs
                    to lock the data directory, so cannot
                    be used if a mongod is currently
                    accessing the same path
 --directoryperdb           each db is in a separate directly
                    (relevant only if dbpath specified)
 --journal               enable journaling (relevant only if
                    dbpath specified)
 -d [ --db ] arg            database to use
 -c [ --collection ] arg        collection to use (some commands)
 --objcheck              validate object before inserting
                    (default)
 --noobjcheck             don't validate object before inserting
 --filter arg             filter to apply before inserting
 --drop                drop each collection before import
 --oplogReplay             replay oplog for point-in-time restore
 --oplogLimit arg           include oplog entries before the
                    provided Timestamp (seconds[:ordinal])
                    during the oplog replay; the ordinal
                    value is optional
 --keepIndexVersion          don't upgrade indexes to newest version
 --noOptionsRestore          don't restore collection options
 --noIndexRestore           don't restore indexes
 --w arg (=0)             minimum number of replicas per write

linux below mongodb database backup and restore

Next we can create an automatic backup script for linux. We can set up the timing task or manually backup it. I backed it up manually.

First create an sh command, which I put under home.


vim /home/mongoBeiFen.sh

Enter the following:


#!/bin/bash
shijie=`date +%Y%m%d%H`
backmongodbFile=mongodb$shijie.tar.gz
cd /home/mongoDbback/
/usr/local/mongo/bin/mongodump -h 127.0.0.1 --port 27017 -u mongo -p 123456 -d my_mongodb -o my_mongodb_dump/
tar czf $backmongodbFile my_mongodb_dump/
rm my_mongodb_dump -rf 

Explanation:

The folder to store the backup is /home/mongoDbback/

-u is the database username -- p is the password -- d is the database username -- d is the database username -- window is the database username -- d is the database username.

Only run 1 time for backup


./mongoBeiFen.sh  

That's it.

Database recovery:


/usr/local/mongo/bin/mongorestore -d my_mongodb my_mongodb_dump/my_mongodb/* 

Point to each file


/usr/local/mongo/bin/mongorestore -h 127.0.0.1 --port 27017 -- drop --directoryperdb  my_mongodb_dump/my_mongodb

Point to 1 directory
If you have any questions, you can try window:


/usr/local/mongo/bin/mongorestore -h 127.0.0.1:27017 -d test --drop  --directoryperdb  my_mongodb_dump/my_mongodb


Related articles: