I have a MongoDB container running the latest version of the database, and I can manually use the mongodump command to download my data in production and later run mongorestore to insert this data into a local container.
Then I thought: Oh, if I just mongodump --things && mongorestore --otherthings then the mongorestore command will be run after the mongodump command.
However, the mongodump command (at least inside the container shell) is a little tricky, since mongodump doesn't lock the shell, and I think as soon as I hit enter, mongorestore command gets activated too since it thinks that mongodump has finished. Then, mongorestore completed with failures since there is no data downloaded yet. After a couple of seconds mongodump STARTS downloading the files and the shell gets locked. Even after the files are completely downloaded, the shell doesn't get unlocked and I can't type new commands unless I hit enter.
So my question is: How do I run one single script (.sh file or something) that can wait for mongodump to finish, and only then execute the mongorestore command?)
I encounter the same problem but found that the mongodump is able to "lock" the terminal finally after adding double quote "" to your mongodump
i.e. something like mongodump --uri="mongodb://localhost:27017" --gzip --archive="backup.gz"
Related
I've dumped some databases using mongodump which means that I currently have a folder(which name, is the date I dumped the databases) and one folder for each database that I had and inside each database folder I have 2 files per collection, one .bson and one .metadata.json.
Now I want to use mongorestore to rebuild databases and their collections using the following command:
mongorestore --db user --verbose /home/nvsh120/projects/database/11-26-20/user
but it doesn't work and exits with the following error:
uncaught exception: SyntaxError: unexpected token: identifier :
#(shell):1:15
So the problem was that I was trying to run mongorestore from mongo shell whereas I should've run it from windows\linux command prompt\terminal.
In case someone needs this, it is for running a mongorestore command for a docker pulled DB.
You cannot run the mongorestore in the docker mongo shell.
First run the command to get the bash of the specific docker container:
sudo docker exec -it <name-of-container> bash
Next, run the restore command in this bash.
root#52f66390ae10:/# mongorestore -d <db-name> <path-to-dump-in-docker-container>
Keep in mind, I have run my Docker MongoDB with a docker-compose.yml file in which I have mounted the database dump as a volume.
Here is a snippet from the docker-compose file:
volumes:
- <path-to-dump-in-local>:<path-to-db-in-container> (Usually /data/db)
Volumes can also be mounted as a part of the docker run command.
I have a mongodb database in a dump folder which contain some bson and json files. I want to restore it using mongorestore, but I can't connect to the cluster through mongo shell. So I use mongo compass to connect but I have no clue how to restore it in the mongo compass.
You run mongorestore runs from the system command line, not mongo shell. Use the Command Prompt in Windows or Terminal in OS X / Linux to enter the command and you should be good to go. The connection string will be the same one you used to connect through Compass.
To restore you have to follow these steps carefully:-
1: First check dump type if it is a zip file then extract it.
2: open your cmd prompt and go to the folder where your database XYZ (folder) exists.
mi#sujeet-mishra:~/Downloads/dump$
Inside the dump, there is a folder called XYZ (folder of XYZ database)
Now run the command: mongorestore -db XYZ
mi#sujeet-mishra:~/Downloads/dump$ mongorestore -db XYZ
that's it, now refresh your MongoDB and you will get a DB named XYZ.
To create a database dump and then restore it, you need to have (have been installed) tools for working with the database on your system - a set of utilities (executable files) of the command line for working with MongoDB,
such as: mongodump, mongorestore, bsondump, mongoimport, mongoexport and other...
The set of utilities supports all popular platforms. For example, system support by the mongorestore utility can be viewed here.
To install a set of utilities into your system, you need to use the installation instructions for your operating system: Linux, MacOS, Windows.
Since problems can usually arise when using utilities on Windows, let's look at an example of installing and using utilities specifically for this system. We will be using Windows 10.
So...
Open MongoDB Download Center.
Select the desired operating system and package type on the page in
the form. Downloading the archive!
Unpack the contents of the downloaded archive, for example, to the
C:\utilities folder.
Further, in order for the system to see our utilities and avoid an
error the utility
command is not an internal or external command,
executable program or batch file
, you must add a new entry to environment variables (read more about it).
* once environment variables are set, MongoDB utility commands can be executed from any directory of any console (simple console, PowerShell, IDE code editor console)
* You can`t use environment variables, but in this case, keep in mind that running utility commands will only be possible from the directory in which you placed the executable file. In other words, whenever you need to execute a console command of one of the utilities, you will need to change to the directory where the executable file is located!
Now it remains only to run the command to restore the database on your server, it will look like this - in the latest version of MongoDB, everything is as simple as impossible:
mongorestore C:\backups\db
// or, for example, specifying the host and port like this
mongorestore --host localhost --port 27017 C:\backups\db
// or like this
mongorestore --host localhost:27018 C:\backups\db
Most Important! Since the --db option for mongorestore is deprecated , please note that we do not specify the name of the database to restore the database to!
The name of the database where the data will be imported is the name of the directory in the db folder, in which, in turn, you should have dump files in .bson and .json formats.
If you specify a specific directory in which you have dumps, for example: C:\backups\db\my_app, then running the command will result in an error -
... don't know what to do with file....
Also keep in mind that running the mongorestore command from the C:\backups\db directory will restore the dumps of all applications located in this directory (if any)!
Before 4 version of MongoDB, the mongorestore command could look like this (deprecated):
mongorestore --host localhost --port 27017 --db name_your_db C:\backups\db\my_app // a
mongorestore --host localhost --port 27017 -d name_your_db C:\backups\db\my_app // b
// or short records:
mongorestore -h localhost --port 27017 -d name_your_db C:\backups\db\my_app // a
mongorestore -h localhost:27017 -d name_your_db C:\backups\db\my_app // b
mongorestore --uri="mongodb://localhost:27017/name_your_db" C:\backups\db\my_app // c
In fact, there may be more options for using the command, with various options, using authorization on the server. In this case, only a few examples are given.
You can see the full list of options on the official website.
To create a dump, use the command:
mongodump --out=C:\backups\db // <- to make dump all databases
mongodump -h localhost:27017 -d my_app --out=C:\backups\db // <- to create a dump of a specific single database
You can see the full list of options of mongodump on the official website.
* for Linux, commands are executed in a similar way
I’m currently trying to restore a mongodump made with mongodb:3.4-jessie into a newer version, mongodb:4.2.3-bionic.
When I try to execute my command:
sudo docker exec mongo mongorestore —db=mock —gzip /mongorestore/app
It returns me with this error:
2020-05-01T00:01:29.405+0000 the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2020-05-01T00:01:29.406+0000 Failed: mongorestore target '/home/user1/mongorestore/app' invalid: stat /home/user1/mongorestore/app: no such file or directory
2020-05-01T00:01:29.406+0000 0 document(s) restored successfully. 0 document(s) failed to restore.
The folder app contains BSON files and json.gz too.
I can’t upgrade the older dump, as it’s the only thing left and really want to use a newer version of mongo.
Thanks a lot!
Your command was blocked by problems before it could attempt to restore data to a newer mongodb release.
You're running mongorestore inside a docker container but the input data directory /mongorestore/app does not seem to be inside the container (unless you mounted it in a previous step not seen here and passed the wrong path to mongorestore). You can use the Docker run command's --mount or --volume options to mount host directories into a container. Then pass its in-container path to the mongorestore command. See the docker run command docs.
mongorestore is warning about this use of the --db option but it's not clear if that's because it can't find the input data directory or if it needs the --nsInclude option instead. See the mongorestore command docs.
You shouldn't need to use sudo with docker exec, and doing so could cause permission problems with the mounted output files. The mongorestore command shouldn't need sudo either, but if I'm wrong about that, write docker exec mongo sudo mongorestore ....
The mongodump and mongorestore docs suggest that the --gzip option expects all the files to be compressed, not just some of them. Maybe it notices each file's .gz filename extensions to decide whether to decompress it, but the docs don't say that it supports that case.
I'm betting mongorestore can restore BSON files from an older release. That file format should be very stable.
I ran into the same issue but with Mongo 5 and it worked like so:
mongorestore --host=hostname --port=portnum \
--archive=/path/to/archive.gz --gzip --verbose \
--nsInclude="mydbname.*" \
--convertLegacyIndexes
where mydbname is the name of the db that I used when dumping the collections.
If you use another dbname now, then you need to convert them using --nsFrom="mydbname.*" --nsTo="newdbname.*"
All from: https://docs.mongodb.com/database-tools/mongorestore/
I am a newbie to mongodb. I am trying to take a backup of my database using mongodump. But whenever I use this command I get the below error
Referenceerror: mongodump is not defined
I tried creating a new user with all the roles but still I get the same error. Should I add a specific role to take a backup? Or am I doing something wrong?
'mongodump' is a command/tool which is included in the 'mongodb-tools' package. If you don't have this package installed on your machine, it makes sense that it is not defined. The mongodb-tools also provide several other tools used for importing and exporting DBs (like mongorestore).
That being said, 'mongodump' is not a mongo-shell command, you shouldn't be using it in mongo-shell. It's a whole different command that you would be executing just like you execute 'mongod' or 'mongo' etc.
One simple way is to right-click mongodump.exe and "Run as administrator". It will create dump folder in bin of your mongodb home containing all database backups.
If you want to go with commands, open cmd as administrator and go to bin of your mongodb where you'll be able to fire commands such as mongorestore, mongodump etc. with intended parameters e.g for specific db or interact with remote mongodb.
tested on (v4.2.3)
Note: many folks try to execute these commands in mongod where you can execute queries which is wrong, these tools needs to be used separately.
Here ar e 2 simple exemples for a full backup using authentication and without
mongodump -h hostname -v -u sys_account -p ys_password --authenticationDatabase admin --out folder_location_for_backup
if no authentication
mongodump -h hostname -v --out folder_location_for_backup
here are the mongorestore commands as well
mongorestore -h hostname -v -u admin_user -p admin_password --authenticationDatabase admin --dir folder_location_where_backup_is_located
if no authentication
mongorestore -h hostname -v --dir folder_location_where_backup_is_located
for windows, you need to start executable file by running following command
"C:\Program Files\MongoDB\Server\3.4\bin\mongodump.exe" --db your_database_name
Above command will export your database to dump folder. This folder will be located where you have kept your "data" folder. If you use default "data/db" folder then it will be there but if you use different location then it will be kept over there.
This command must be run in normal command prompt, not in mongo shell. It is executable not mongo shell command. Official docs link is here.
Download MongoDB Command Line Database Tools .The mongodump tool is part of the MongoDB Database Tools package.
There are great chances that mongodump is being performed under Mongo Shell
mongodump should be run directly on command line or terminal (NOT inside mongo shell)
For creating dump of database
mongo --db database_name
For creating dump of any collection of database
mongo --db databas_name --collection collection_name
This post is about backing up your meteor database
I am trying to back up my meteor database and I understand what this post is telling me to do, but I must not be in the right directory when I run the mongodump command, b/c I keep getting 'Command not found'. Or do I need to export a path?
[EDIT]
OK, now I have the binaries installed but when I run 'mongodump', I get:
couldn't connect to [127.0.0.1] couldn't connect to server 127.0.0.1:27017
... and when I run 'mongodump --host localhost:3002', I get:
couldn't connect to [localhost:3002] couldn't connect to server localhost:3002
Now what?
OK, thanks to #David Weldon, I can provide a fairly complete answer to this issue:
Backing up and restoring your local MongoDB for Meteor users (OSX)
Backup:
1) Your app must be running, so start up your Meteor server.
2) In a terminal window (NOT in the meteor mongo shell), enter: mongodump -h 127.0.0.1 --port 3001 -d meteor
This will create a 'dump' directory inside your home folder (your name under Users).
3) If you get a 'command not found' message, you probably just installed Mongo as a part of Meteor, meaning you don't have the mongo command line tools. Use a package like Homebrew to reinstall Mongo and you will have the command line tools. This will also add the correct PATH information to your system, so that it can find the tools.
Restoring:
1) From MiniMongo shell (run ‘meteor mongo’ inside your Meteor project dir), enter:
db.[collectionName].drop(); //repeat for all collections you wish to restore
2) Then, from a terminal window, enter:
mongorestore -h 127.0.0.1 --port 3001 -d meteor dump/meteor
Caveats:
The individual documents will not necessarily be in the same order after they are restored. So you need some way to sort documents that need to be presented in a certain order.
Caveats:
The individual documents will not necessarily be in the same order after they are restored. So you need some way to sort documents that need to be presented in a certain order.
There is a flag for this
mongorestore --maintainInsertionOrder
If you are restoring to a mup'ed meteor app, the command would be:
mongorestore -h 127.0.0.1 --port 3001 -d dump/meteor
If you forgot the name of your app, you can see it by going into the mongo shell and listing all databases.