How to use backup and restore all databases from mongodb?
Asked Answered
E

2

6

If i want to do a generic backup for all databases in mongodb, is it that all i have to do is:

$ mongodump

And if i want to restore the latest dump i've created, all i need to do is:

$ mongorestore
  • Where are the backups from the mongodump stored?
  • How to i specify a specific dump for all databases to be restored?
Earthward answered 30/1, 2013 at 4:57 Comment(0)
E
7

The backups are stored in the directory that you have specified with the --out option in command line. If you do not specify any output dir the backup will be placed to ./dump directory. With the mongorestore you have to specify the directory where you have dumped before as a command line argument.

On sharded environment the backup will be flattened if you use mongodump through a mongos. After restore you will have to re-shard the collections. so restoring not always effortless. See documentation: http://docs.mongodb.org/manual/tutorial/backup-small-sharded-cluster-with-mongodump/

You can dump directly the db folders too, check the cli options.

For sharded clusters you can check the possibilities here: http://docs.mongodb.org/manual/administration/backups/#sharded-cluster-backups

Engird answered 30/1, 2013 at 9:6 Comment(4)
so if i just did a mongodump through the unix terminal, is it a sharded environment? Actually, if i have did a mongostore on the same parent directory of my mongodump's ./dump, i did not specify the directory and it restored the db perfectly.Earthward
It depends on that your database sharded or not. If not than it must be restored perfectly. If you have sharding you have to make further steps.Engird
but i thought mongodb auto shards the collections in the database?Earthward
Once you have set up a sharded cluster the operation is automatic for balancing. But setting up the sharded environment, adding/removing nodes are manual and quite difficult. Check this: docs.mongodb.org/manual/shardingEngird
K
1

restore all

mongorestore --host=<host> --port=<port> --username=<username> --authenticationDatabase=<authenticationdatabase> --nsInclude "*.*"  <path to dump>

backup all

mongodump --ssl --host <domain> --port <port> -p <password>  --authenticationDatabase <authenticationdatabase> -u <username> -p <password> --out <dir path>

If you want to backup databases use:

mongodump --ssl --host <domain> --port <port> -p <password>  --authenticationDatabase <authenticationdatabase> -u <username> -p <password> --out <dir path>

if the database has ssl enabled include the --ssl flag

if you don't include the --out mongodump will create a "/dump" directory.

Inside the dump or specified backup directory you'll find directories with the names of your databases, inside each of them, you'll find the backups files, for each collection you'll find a ".bson" and a ".metadata.json"

To restore all the databases use:

mongorestore --ssl --host=<host> --port=<port> --username=<username> --authenticationDatabase=<authenticationdatabase> --nsInclude "*.*"  <path to dump>

Again if the database has ssl enabled include the --ssl flag if not just remove it. the --nsInclude flag tell mongorestore which databases or collections you want to restore.

Examples:

--nsInclude=test.users  

this will backup the users collection of the database test, and therefore will fail if the path to the dump is anything but the path to the users.bson of that particula database

To include all the database and all the collections use --nsInclude=*.* or --nsInclude *.* then define the path to all the collection directories of your backup

Kwok answered 7/1, 2021 at 18:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.