Trouble with mongorestore (don't know what to do with subdirectory) - mongodb

I use this cmd to restore mongodb to remote db on mLab
$ ./mongorestore -h ds*****.mlab.com:**** -u <user> -p <pass> -d loc8r
but I got this error
2018-05-01T20:22:18.463+0700 using default 'dump' directory
2018-05-01T20:22:18.463+0700 building a list of collections to restore from dump dir
2018-05-01T20:22:18.464+0700 don't know what to do with subdirectory "dump\Loc8r", skipping...
2018-05-01T20:22:18.464+0700 don't know what to do with subdirectory "dump\admin", skipping...
2018-05-01T20:22:18.464+0700 done
I wonder if I did something wrong? there's no problem when I use the same cmd with the dump folder only contains .bson file

With mongorestore when attempting to restore a backup dump (originally created using mongodump utility), the mongorestore utility needs to have access to the .bson file.
When you took the backup/dump, if the .bson file did not exist in the Root folder (of your Data folder), then you will have to explicitly specify its path. What I mean is:
Say you took a backup/dump in D:\mongo_bkup_dir\ folder and if you do have the .bson file in the root (where the backup was taken), then just providing the root folder location, e.g.:
mongorestore --port 27017 --db dbName --username usr1 --password P#ssw0rd --authenticationDatabase authDbName --dir D:\mongo_bkup_dir\
However, if you had .bson file in a sub-folder called myDir (when backup was taken), then use:
mongorestore --port 27017 --db dbName --username usr1 --password P#ssw0rd --authenticationDatabase authDbName --dir D:\mongo_bkup_dir\mybackup1.dmp\myDir
HTH.

Related

mongorestore in Dockerfile not locating dump files

Been trying to land this Dockerfile to work properly but seems there's no human way for me to do so.
The directory where the Dockerfile is placed looks like this:
my_dir
|
|- Dockerfile
|- samplesdb
|- samplesdb/samples.bson
|- samplesdb/samples_metadata.json
Now, the Dockerfile looks like this:
FROM mongo
COPY . /dump/
WORKDIR /dump
CMD mongorestore --host 127.0.0.1 --port 27017 --db samplesdb --drop samples
However, it will always Failed: mongorestore target 'dump' invalid: stat dump: no such file or directory.
Is there any reason for this? How should I copy those dumps?
mongorestore looks for a dump directory in your current directory, if it is named something else, you need to explicitly specify it as the last argument.
Your COPY command is creating /dump/samplesdb, however, mongorestore expects the bson files to be at the root of the dump directory
So, change the below line:
CMD mongorestore --host 127.0.0.1 --port 27017 --db samplesdb --drop samples
to (notice the samplesdb - your custom dump directory - as the last argument)
CMD mongorestore --host 127.0.0.1 --port 27017 --db samplesdb --drop samples samplesdb

Restore mongodb dump to different db [duplicate]

In MongoDB, is it possible to dump a database and restore the content to a different database? For example like this:
mongodump --db db1 --out dumpdir
mongorestore --db db2 --dir dumpdir
But it doesn't work. Here's the error message:
building a list of collections to restore from dumpdir dir
don't know what to do with subdirectory "dumpdir/db1", skipping...
done
You need to actually point at the "database name" container directory "within" the output directory from the previous dump:
mongorestore -d db2 dumpdir/db1
And usually just <path> is fine as a positional argument rather than with -dir which would only be needed when "out of position" i.e "in the middle of the arguments list".
p.s. For archive backup file (tested with mongorestore v3.4.10)
mongorestore --gzip --archive=${BACKUP_FILE_GZ} --nsFrom "${DB_NAME}.*" --nsTo "${DB_NAME_RESTORE}.*"
mongodump --db=DB_NAME --out=/path-to-dump
mongorestore --nsFrom "DB_NAME.*" --nsTo "NEW_DB_NAME.*" /path-to-dump
In addition to the answer of Blakes Seven, if your databases use authentication I got this to work using the --uri option, which requires a recent mongo version (>3.4.6):
mongodump --uri="mongodb://$sourceUser:$sourcePwd#$sourceHost/$sourceDb" --gzip --archive | mongorestore --uri="mongodb://$targetUser:$targetPwd#$targetHost/$targetDb" --nsFrom="$sourceDb.*" --nsTo="$targetDb.*" --gzip --archive
Thank you! #Blakes Seven
Adding Docker notes:
container names are interchangeable with container ID's
(assumes authenticated, assumes named container=my_db and new_db)
dump:
docker exec -it my_db bash -c "mongodump --uri mongodb://db:password#localhost:27017/my_db --archive --gzip | cat > /tmp/backup.gz"
copy to workstation:
docker cp my_db:/tmp/backup.gz c:\backups\backup.gz
copy into new container(form backups folder):
docker cp .\backup.gz new_db:/tmp
restore from container tmp folder:
docker exec -it new_db bash -c "mongorestore --uri mongodb://db:password#localhost:27017/new_db --nsFrom 'my_db.*' --nsTo 'new_db.*' --gzip --archive=/tmp/backup.gz"
You can restore DB with another name. The syntax is:
mongorestore --port 27017 -u="username" -p="password"
--nsFrom "dbname.*"
--nsTo "new_dbname.*"
--authenticationDatabase admin /backup_path

MongoDb: How to import dump data from .gz file?

I want to import dump data from my .gz file.
Location of file is home/Alex/Documents/Abc/dump.gz and the name of db is "Alex".
I have tried mongorestore --gzip --db "Alex" /home/Alex/Documents/Abc/dump.gz
But it shows error:
2018-10-31T12:54:58.359+0530 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
2018-10-31T12:54:58.359+0530 Failed: file
/home/Alex/Documents/Abc/dump.gz does not have .bson extension.
How can I import it?
Dump command:
mongodump --host localhost:27017 --gzip --db Alex --out ./testSO
Restore Command:
mongorestore --host localhost:27017 --gzip --db Alex ./testSO/Alex
Works perfectly!
While using archive:
Dump command:
mongodump --host localhost:27017 --archive=dump.gz --gzip --db Alex
Restore Command:
mongorestore --host localhost:27017 --gzip --archive=dump.gz --db Alex
Note:- While using archive you need to stick with the database name.
Different database name or collection name is not supported. For more info.
This is what worked for me in the latest versions (100.5.1) of mongodump.
mongorestore --uri=<CONNECTION_URI> --gzip --archive=<ARCHIVE_NAME> --nsFrom "<SOURCE_DB_NAME>.*" --nsTo "<DEST_DB_NAME>.*"
Unpack .tgz files and restore the DB
tar zxvf fileNameHere.tgz
mongorestore --port 27017 -u="username" -p="password" --authenticationDatabase admin /bacup_path
mongorestore doesn't find the BSON files inside the gzip file because the mongodump was made with different paths than the restore one.
To solve the problem, the fastest and safest way is to extract the gzip file and go to the upper folder containing the json and bson files for run the mongorestore.
For example, the dump.gz file was made in such a way that the backup are saved within the data/backup/mongo/dump/ path folders
Extracting the dump.gz file with command tar -xvf dump.gz you will find a folder named data with the subfolders data/backup/mongo/dump/ inside (inside the dump folder are present all backup file with json and bson extension, these files represent databases and collections, etc.)
Go to the higher folder, that containing the dump folder eg. cd data/backup/mongo/
Now you can run the restore command
mongorestore --authenticationDatabase admin dump/
Where dump/ is the folder that containing the backup files.
You may need to use the arguments -h to point the server host (eg. localhost) and -u followed by the username enabled to make the restore operations (eg. root)

mongodump with different folder name

Is there a way to dump mongo database to a specific folder name?
Lets say I have a db named myDb
When I use:
mongodump --db myDb
It generates a dump folder with a myDb folder inside it
Is it possible to get something like dump/mySpecifiedFolder from mongodump?
You cannot change the name of the myDB folder, since that's named for the database which it contains, but you can change the location of the myDb folder. To do this, use the --out / -o parameter.
From the docs:
--out , -o
Specifies the directory where mongodump will write BSON files for the dumped databases. By default, mongodump saves output files in a directory named dump in the current working directory.
So, this command ...
mongodump --db myDb -o /some/path/
... would cause mongodump to create /some/path/myDb.
In my discovery of doing a mongodump, please note that the default location can be the Desktop when outputting.
Example
"C:\Program Files\MongoDB\Server\3.4\bin\mongodump.exe" --host localhost --port 27017 --db local --out dump/dpusers
On your Desktop, the dump folder will be created with the subfolder "dpusers" and inside that folder is another folder named the same as your database name.

restoring mongo database from dump

I am not able to get the correct command to issue in order to restore a mongodb from a dump dir.
Any idea what is wrong and how to fix it? Thanks
appParent$ ls
dir1 dir2 dump
appParent$ ls dump/
meteor
appParent$ mongorestore --maintainInsertionOrder -h 127.0.0.1 --port 3001 -d meteor
error:
using default 'dump' directory
building a list of collections to restore from dump dir
don't know what to do with subdirectory "dump/meteor", skipping...
done
Apart from the -d (database) parameter I think you may need to explicitly send the final path argument
So the command would be (considering you are in the folder containing the dump folder):
$ mongorestore --maintainInsertionOrder -h 127.0.0.1 --port 3001 -d meteor dump/meteor
If the directory name is the default you don't need further arguments. Just run mongorestore.
appParent$ ls
dir1 dir2 dump
appParent$ mongorestore --port 3001