I am running a container with image mongo:latest. Starting the container:
sudo docker run -it -v /tmp/adhock-container/mongo_latest:/tmp/ mongo:latest /bin/bash
I want to take the backup of Mongo database so I run the following:
# mongodump -vvvv --host MY_HOSTNAME --port 27017 --username MY_USERNAME --password MY_PASSWORD --gzip --archive=ARCHIVE_PATH
In the console log I am getting only the following output:
2020-04-06T08:00:08.007+0000 done dumping ******** (1104 documents)
2020-04-06T08:00:08.007+0000 writing ************ to archive 'standalone.gzip'
2020-04-06T08:00:08.007+0000 MuxIn open ************_log
Killed
#
Server memory stats
$ free -m
total used free shared buff/cache available
Mem: 459 328 6 0 124 123
Swap: 0 0 0
Not sure why my mongodump process is getting killed. I think it may be because of memory issues but I am not sure how to fine tune those customizations.
I would start with consulting the MongoDB server log. See "Container shell access and viewing MongoDB logs" here if this is the image you are using.
To determine whether your container is running out of memory, get a shell on it and run top while dumping.
I faced the same issue, it happened when I was executing mongodump on the database host system. However, I did small workaround, for me the the infra looked like:
Source Mongo <---> My PC <---> Dest Mongo (K8S)
I did run mongorestore on my PC, in container, connected to remote machine and dumped to another remote instance. This was slower but didn't kill the process.
Run mongorestore on your PC and ensure you have access to remote DB or do a port-forwarding thingy.
$ docker run --network host --entrypoint bash -it mongo:4.2
Dump the archive
$ mongodump --archive=test.archive --db db --username username --host <address>
Restore the archive from inside the container
$ mongorestore --archive=test.archive --db db --username username --host <address>
This is the only way I managed to not crash the mongodump. If someone runs into this issue, I would consider having mongo container on your OS/some different machine, do a dump from this point and then restore as mentioned.
I am trying to restore MongoDB from db_dump via terminal.
$ mongorestore -h localhost:27017 --db aaaa_production --archive=db_dump_071118.gz --gzip
Error parsing command line: unrecognised option '--archive=db_dump_071118.gz'
try 'mongorestore --help' for more information
Before running mongorestore command make sure that MongoDB is running on the same port as specified in mongorestore command. In your case, MongoDB should be running on localhost:27017.Then run following command to restore database:
$ mongorestore -h localhost:27017 --db aaaa_production --gzip --archive=db_dump_071118.gz
We have a remote MongoDB server and we have mongodump data on a local developer's machine. What is the best way to restore the remote MongoDB server data with the local data? Is there a mongo command that we can use?
Alright so we did this in two steps. I think you can do it in one step, with just mongorestore.
First we moved the data from the local machine to the remote machine with the scp command:
scp <path-to-mongofile> <remote-host>:<absolute-file-path>
then we ssh'd into the remote mongod server, and used mongorestore to restore the db
mongorestore --host=$HOST --port=$PORT -u $ADMIN_USER -p $PSWD --db <your-db> <absolute-path-to-restore-db> --authenticationDatabase "admin"
but I think the first scp command is redundant. In fact, if you cannot ssh into the server running mongod, then you will have to use the mongorestore command directly from the local developer's machine.
Just use mongorestore but point it towards the remote server, such as:
$ mongorestore -h ds01234567.mlab.com:12345 -d heroku_fj33kf -u <user> -p <password> <input db directory>
From MongoLab's docs
We recently ported some data over to MongoDB and are now looking into running daily backups, preferably from a cron job, and restore one of the backups to a secondary mongo database.
Our system is set up as follows:
server 1: the development mongo database
server 2: two mongo databases, one for staging data and one for production
server 3: is where we run all of our cron jobs/batch scripts from.
I checked the mongo docs, and logged into our cron job server and tried to run the following command: (username, host, and password changed for security, I'm not actually connecting to localhost)
mongodump --host 127.0.0.1/development --port 27017 --username user --password pass --out /opt/backup/mongodump-2013-10-07-1
I get the following messages:
Mon Oct 7 10:03:42 starting new replica set monitor for replica set 127.0.0.1 with seed of development:27017
Mon Oct 7 10:03:42 successfully connected to seed development:27017 for replica set 127.0.0.1
Mon Oct 7 10:03:42 warning: node: development:27017 isn't a part of set: 127.0.0.1 ismaster: { ismaster: true, maxBsonObjectSize: 16777216, ok: 1.0 }
Mon Oct 7 10:03:44 replica set monitor for replica set 127.0.0.1 started, address is 127.0.0.1/
Mon Oct 7 10:03:44 [ReplicaSetMonitorWatcher] starting couldn't connect to [127.0.0.1/development:27017] connect failed to set 127.0.0.1/development:27017
I confirmed that I can connect to the mongo database using mongo -u -p ip/development
Our ultimate goal will be to dump the data from the production database and store it in the staging database. These two databases are both located on the same box, if that makes a difference, but for testing purposes I am just trying to get a backup of development test data.
mongo client can parse MongoDB connection string URI, so instead of specifying all connection parameters separately you may pass single connection string URI.
In your case you're trying to pass connection URI as a host, but 127.0.0.1/development is not a valid host name. It means you should specify database parameter separately from the host:
mongodump --host 127.0.0.1 -d development --port 27017 --username user --password pass --out /opt/backup/mongodump-2013-10-07-1
You can use with mongodump with --uri
mongodump --uri "mongodb://usersname:password#127.0.0.1:27100/dbname?replicaSet=replica_name&authSource=admin" --out "C:\Umesh"
All your collections will store inside the out folder it will create directory name as your Database name and all the collections are bson and metadata will store as json format.
For restore
mongorestore --uri "mongodb://usersname:password#127.0.0.1:27100/dbname?replicaSet=replica_name&authSource=admin" -d dbname mongodbumppath
Try this it will work.
This worked for me.
Reference: https://docs.mongodb.com/manual/reference/program/mongodump
Syntax 1:
mongodump --host <hostname:port> --db <database> --username <username> --password <password> --out <path>
Syntax 2:
mongodump -h <hostname:port> -d <database> -u <username> -p <password> -o <path>
Example 1:
mongodump --host 127.0.0.1:27017 --db db_app --username root --password secret --out /backup/db/app-17-03-07
Example 2:
mongodump -h 127.0.0.1:27017 -d db_app -u root -p secret -o /backup/db/app-17-03-07
mongodump --host remotehostip:port --db dbname -u username -p password
Here is an example of exporting collection from node server to local machine:
Host : xxx.xxx.xxx.xx
Port :27017
Username:”XXXX”
Password :”YYYY”
AuthDB : “admin”
“DB”: “mydb”
D:\mongodb-backup>mongodump -h xxx.xxx.xxx.xxx –port 27017 -u “XXXX” -p “YYYY” –authenticationDatabase “admin” –db “mydb”
Use this to get dump using URI:
mongodump --uri=mongodb+srv://john:xxxxxxxxxxxxxxx#cluster0-jdtjt.mongodb.net/sales
You can also use gzip for taking backup of one collection and compressing the backup on the fly
mongodump --db somedb --collection somecollection --out - | gzip > collectiondump.gz
Or with a date in the file name:
mongodump --db somedb --collection somecollection --out - | gzip > dump_`date "+%Y-%m-%d"`.gz
This worked to me like a charm for a single collection with a remote Windows Server.
mongodump --host <remote_ip> --port <mongo_port> --db <remote_db_name> --authenticationDatabase <remote_auth_db> --username <remote_mongo_username> --password <remote_db_pwd> --out <local_DB_backup_folder> --collection <remote_collection_name>
On Mac, this is what worked for me (but be sure to use your own real credentials):
brew tap mongodb/brew
brew install mongodb-community#5.0
brew services start mongodb/brew/mongodb-community
mongodump --uri "mongodb://usersname:password#127.0.0.1:27100/dbname" --out "/Users/some_username/code/mongodb_dumps/dump/"
cd /Users/some_username/code/mongodb_dumps/
mongorestore --nsInclude "*.*"
mongodump --host hostip -d dbname --port portnumber --username username --password password --authenticationDatabase admin -o ./path/of/backupfolder
note: "./path/of/backupfolder" path is in your client
This worked for me:
Step1: Export remote/local DB.
mongodump --uri "mongodb+srv://USER:PASSWORD........." --out "/Users/Hardik/Desktop/mongo_bkp"
Step2: Import
mongorestore ./mongo_bkp/
Posting this here in case it helps somebody.
It was impossible for me to connect using mongodump. I ended up installing the VS Code Mongo extension and it generated the string for me. The command looks like this:
mongodump -o dump_destination --uri "mongodb://<USERNAME>:<PASSWORD>#<HOST>:<PORT>/<DATABASENAME>?authSource=admin&readPreference=primary&ssl=true"
Perhaps I have a complete misunderstanding of how mongodump is supposed to work, but I can't seem to get it to do anything besides returning a JavaScript execution failed: SyntaxError: Unexpected identifier error.
Here's what I'm doing:
Mongod is running
I want to backup a database called "mydb"
I'm inside the mongo shell
I tried the command mongodump --db mydb and get the above error
I've tried both mongodump and mongoexport, both have the same issue
What am I doing wrong here?
Try the following it will work
i.Open the terminal
ii. Enter mongodump --collection collectionname --db dbname (Don't go inside mongo shell);
iii.If default port is different(other than 27017) then go for the following command
mongodump --host mongodb1.example.net --port 37017 --username user --password pass --out /opt/backup/mongodump-2011-10-24
mongodump,mongorestore is not commands of mongodb shell. It is separate mongodb utlity. You can find it under mongodb bin folder.
Usually you will need to add all mongodb utilities to the system Path variable and after this easy backup/restore databases from any place in the command line or in the terminal.
Your command looks mongodump --db mydb good if your databases in on default port(27017).
I faced the problem in taking mongo dump and I also wanted to store the dump to S3. Finally I ended up with a bash script to take mongo dump and store it to S3. I used mongodump to take backup.
mongodump -h $MONGO_HOST:$MONGO_PORT -d $MONGO_DATABASE
Where $MONGO_HOST,$MONGO_PORT and $MONGO_DATABASE are bash variables for host, port and database-name respectively.
You can also use --username user --password pass option for mongodump command if you have username and password setup on the database.
Here is the script to take mongodb dump and store it to S3 with a cron.