Stop mongodump from overwriting existing files (rename instead) - mongodb

From mongodocs:
Overwrite Files
"Mongodump overwrites output files if they exist in the backup data folder. Before running the mongodump command multiple times, either ensure that you no longer need the files in the output folder (the default is the dump/ folder) or rename the folders or files."
Hey guys,
I want to do a daily backup and sometimes even two backups a day. The Dump-filename gets named by the actual date. If I backup twice a day, the first backup gets overwritten due to same names.
Is there any way to tell mongodump to rename (in e.g. 5.9.2016(1)) the file if it already exists?

You can use the --out option of mongodump to specify the path where to dummp the data.
Create a script that run mongodump and give different name for your path, i.e. using a date:
mongodump --out /data/dump/090516/
Shell script example:
#!/bin/sh
DIR=`date +%m%d%y`
DEST=$DIR
mkdir $DEST
mongodump --out=/data/dump/$DEST

Related

Get data from a mongodbdump.tar GZ file onto a localhost db

I am looking to make a new db on my localhost from a mongodbdump file. the file looks like this:
C:\Users\ME\FILEPATH\database\somedata-mongodbdump.tar.gz
How can I use mongo shell commands to "restore" this data to a new db on my local host? I know how to set up a new DB and insert records no problem, but I am not sure how to unload this data into one.
I am not finding anything in my searches or the documentation as to how to straightforwardly do this.
I'd like to get this file into a DB that I can host locally and use as the back end for testing an app.
I was misguided here and the previous answers is incorrect. While it may be pretty beginner stuff for some people, I will answer my own question in case anyone else runs into this:
A .tar.gz file is effectively a double zipped file. For Mongo, we need a .bson file. Unzip the file with 7zip or through CMD.
Once you have the BSON in a directory, in CMD (not mongo shell) type:
mongorestore --db 'yourdbname' --collection 'yourcollectionname' C\filepath\filepath\filename.bson
use mongo restore
mongorestore -u 'user' -p 'password -d 'database' -c 'collection' 'path to file'
if you are using a different database for auth then add
--authenticationDatabase='db for auth'

Option to exclude files in pg_basebackup command Postgres

When cloning a standby, how can I prevent pg_basebackup from copying postgresql.conf and pg_hba.conf from the master to /var/lib/pgsql/9.9/data directory?
Currently I am using this command
[root#xyz..]# pg_basebackup -h {master ipAddr} -D /var/lib/pgsql/9.6/data -U postgres -v -P
according to docs:
The backup will include all files in the data directory and
tablespaces, including the configuration files and any additional
files placed in the directory by third parties. But only regular files
and directories are copied. Symbolic links (other than those used for
tablespaces) and special device files are skipped.
So there is no such option. If you still want to force it, move config files away from data directory (and optionally ln them to data_dir)
This answer is for Postgres 14. pg_basebackup takes backup of the entire data directory. https://www.postgresql.org/docs/14/app-pgbasebackup.html states that the backup utility will skip all directory/file that are symbolic links. So, that could be a workaround to get only desired content into the tar ball.
I had faced similar situations where I wanted to exclude the content of multiple directories like pg_replslot,pg_dynshmem, pg_notify etc. I made the tar ball the usual way: pg_basebackup -D /backup/ -F t -P -v. After the tar ball was made, and before restoring it to another server, I updated the tar manually by excluding content of all the required directories.

mongorestore to restore the dump is failing

When I run below command , I'm getting the output as shown, How to fix this?
C:\Users\tadoori\Downloads\dump\m101>mongorestore dump
2016-10-21T13:54:09.883-0600 Failed: mongorestore target 'dump' invalid: GetFileAttributesEx dump: The system cannot find the file specified.
The argument you pass to mongodump is the dump path
As it seems you are already on the dump folder you can also specify the absolute path.
mongorestore C:\Users\tadoori\Downloads\dump
This will restore all dump folder contents (if the files are valid)

Mongo - how do I find the install path of where the data is for the mongo database?

I am trying to do a mongorestore command and I am not sure how to find the directory of where the data is.
The command is something like this:
mongorestore -v --db new_db_dump [path to the dump directory]
and I am not sure how to find where on my local computer the current dump is so I don't know what the [path to the dump directory] is supposed to be.
Any ideas for how to find it? I am on a mac.
Thanks!
By default, mongodump places its output in a sub-directory named "dump" in the current working directory. If you forgot where you were when you executed mongodump, try searching for "dump" in the finder, look at the resulting folders named "dump", and examine the contents. There will be a sub-directory inside "dump" for each of your databases.

How to use the dumped data by mongodump?

I have used mongodump to dump my database of mongodb, it created some bson files under dump/mydb
But I don't know how to use them. I tried mongoimport, but seems it can't import bson data. Then how to use these bson files? How to import them to another mongodb?
You need to use mongorestore, not mongoimport ... which is used for things like importing json, or csv, etc.
From the back-up-with-mongodump docs:
mongodump reads data from a MongoDB database and creates high fidelity BSON files which the mongorestore tool can use to populate a MongoDB database.
mongodump and mongorestore are simple and efficient tools for backing
up and restoring small MongoDB deployments, but are not ideal for
capturing backups of larger systems.
You can read more about mongorestore in the docs below; I'd take a look and read up on them as they are very helpful.
http://www.mongodb.org/display/DOCS/Import+Export+Tools#ImportExportTools-mongorestore
You can also check out http://learnmongo.com for tips and help!
I am using mongodump, mongorestore for daily backups and restoring from backup. I have two .bat files:
First, for backup, where you need just specify host database name and backup folder:
SET host=localhost:27020
SET dbNameToDump=MyDB
SET backupsFolder=Backups
mongodump.exe --host %host% --db %dbNameToDump%
SET date="%date:~10,4%-%date:~4,2%-%date:~7,2%.%time:~0,2%-%time:~3,2%"
cd %backupsFolder%
md %date%
xcopy /e ..\dump %date%
rmdir /s /q ..\dump
Above bat file create folder with name like this 2011-03-31.11-17(yyyy-MM-dd.hh-ss) in folder Backups with dumped collections from specified database. In files explorer it looks like so:
Second bat file i use for retore specified dumped files(here you also need specify database name and folder with dumped files):
SET host=localhost:27020
SET dbNameToRestore=MyDB
SET restoreFolder=Restore
mongorestore.exe --host %host% --db %dbNameToRestore% %restoreFolder%
In files explorer:
In additional, i am using windows schedule to automate backup process.
Hope above information will be useful for someone.
As mentioned in the previous answers, you have to use mongorestore instead of mongoimport. Adding to the previous answers, when your mongodb is running, execute the following command to restore your dump from the dump directory,
mongorestore dump
This will import all the collections into your mydb database. However this doesn't drop the database before restoring. If you wish to drop the database before importing,
mongorestore --drop dump
The bson files in the mydb directory will be restored as the collections inside mydb database. For more info on mongorestore check the documentation here.
Use mongorestore. mongoimport works on the output of mongoexport. mongodump & mongorestore work on binary data files while import / export work on json, csv, etc.. (human readable formats)
For resolving this, I copied the dump folder,dbdump(which contains bson files) to bin directory of mongodb and executed the below commands in command prompt:
1.
cd "path to MongoDB's bin folder"
(Example: cd C:\Program Files\MongoDB\Server\3.2\bin)
2.
mongorestore.exe --dir ./directory name --db database-name
(Example: mongorestore --dir ./dbdump --db testdb)
All bson files in the dump folder will be imported into your database.
You can verfiy this by executing the below commands :
cd "path to MongoDB's bin folder"
mongo.exe
show dbs;
For mongo version 3 and above use the command below:
mongorestore --host=localhost --port=27017 --username=root --authenticationDatabase=admin --db=test dump_folder/
Mongo will ask password after that