backup mongodb using ansible - using mongodump? - mongodb

We need to develop new system that using ansible code to backup few data bases to local windows machine.
I tried unsuccessfully to use "win_command" module and try to transfer the complete command but it didn't work.
- name: Backup MongoDb
win_command:
free_form: mongodump.exe --username 'user' --password 'password' --authenticationDatabase 'admin' -d "db_name" -o "path_to_db_backup"
chdir: path_to_mongodump.exe
I find some ansible module that related to mongodb but noun of related to mongodump.
well, my question is how I can use ansible to backup mongodb DBs with or without mongodump.
thanks for your help.

Related

How to restore database using mongodb compass?

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

How do I mongo restore using an input directory with multiple local dbs in /data/db/?

I have 4 local dbs in my /data/db directory
$ mongo
MongoDB shell version: 3.2.9
connecting to: db-A
$ show dbs
db-A 0.00gb
db-B 0.02gb
db-C 0.08gb
db-D 0.00gb
When I try to run:
$ mongorestore -h dsxxxxx.mlab.com:xxxxx -d <dbname> -u <user> -p <password> /data/db`
It imports the db-A database to my mlab db, how do I specify that I want to import the db-C databse?
This is an old post, but I figured i would post my findings.
If you use mongodump -d dbname -o /path/to/folder for all your data bases, you can then use mongorestore /path/to/folder and it will read all of the databases inside the folder.
If you are moving data from one mongodb instance to other, you need first dump data out from first database and then you can import it to other. You cannot use mongorestore to read directly source database and "restore" it to new one.
You can use copyDatabase() command at destination mongo to copy whole database from other machine.
There is mongodb-connector what you can use too. Actually it's very handy tool to "replicate" one way between two instances.

mongodb - mongodump is not defined

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

What is the easiest to backup a mongoDB deployed with mup?

I deployed my app on a Ubuntu server using mup deploy (https://github.com/arunoda/meteor-up) with the option "setupMongo": true in the mup.json file.
Everything works fine, and I would like to save the mongoDB database daily to FTP or S3, or to set a mongoDB replica to another server (to avoid copying the whole database every time, but it seems more complicated).
If deployed with mup, you are in luck.
You can find the steps here: https://github.com/xpressabhi/mup-data-backup
Here are the steps again:
MongoDB Data Backup deployed via mup
These commands run well only if meteor deployed with mup tool. Mup creates docker for mongodb hence taking backup becomes easy with these commands.
Backup
Take backup of running app data from docker then copy to local folder out of docker.
docker exec -it mongodb mongodump --archive=/root/mongodump.gz --gzip
docker cp mongodb:/root/mongodump.gz mongodump_$(date +%Y-%m-%d_%H-%M-%S).gz
Copy backup to server
Move data to another server/local machine or a backup location
scp /path/to/dumpfile root#serverip:/path/to/backup
Delete old data from meteor deployment
Get into mongo console running in docker then drop current database before getting new data.
docker exec -it mongodb mongo appName
db.runCommand( { dropDatabase: 1 } )
Restore data to meteor docker
docker cp /path/to/dumpfile mongodb:/root/mongodump.gz
docker exec -it mongodb mongorestore --archive=/root/mongodump.gz --gzip
The best way is to mongodump it.
Assuming its running on the mup instance itself since it only listens to 127.0.0.1 you would have to ssh in and use mongodump.
If you simply run it:
mongodump
It will create a directory dump containing your backup.
If you want to do this remotely you would have to edit /etc/mongodb.conf to ensure it binds globally, you will have to create users though since it will be publicly accessible. Then set auth to true.
You could then mongodump from your own machine (you can download the mongodump binary from mongodb.org):
./mongodump --host <your server ip address> --username <username> --password <password>
This answer is inspired by:
sheharyar.me/blog/regular-mongo-backups-using-cron
It uses a script to: mongodump -> tar -> wput (ftp)
First, create a bash script:
#!/bin/bash
MONGO_DATABASE="your_db_name"
APP_NAME="your_app_name"
MONGO_HOST="127.0.0.1"
MONGO_PORT="27017"
TIMESTAMP=`date +%F-%H%M`
MONGODUMP_PATH="/usr/bin/mongodump"
BACKUPS_DIR="/home/username/backups/$APP_NAME"
BACKUP_NAME="$APP_NAME-$TIMESTAMP"
# mongo admin --eval "printjson(db.fsyncLock())"
# $MONGODUMP_PATH -h $MONGO_HOST:$MONGO_PORT -d $MONGO_DATABASE
$MONGODUMP_PATH -d $MONGO_DATABASE
# mongo admin --eval "printjson(db.fsyncUnlock())"
mkdir -p $BACKUPS_DIR
mv dump $BACKUP_NAME
tar -zcvf $BACKUPS_DIR/$BACKUP_NAME.tgz $BACKUP_NAME
rm -rf $BACKUP_NAME
wput $BACKUP_NAME.tgz ftp://login:password#ftp.domain.com/backups/
Save it as mongo_backup.sh and run:
chmod +x mongo_backup.sh
bash mongo_backup.sh
sudo su
crontab -e
And enter this new line:
00 00 * * * /bin/bash /home/username/scripts/mongo_backup.sh
That's it.

Mongorestore command not found with meteor

Ok guys I receive a bunch of files of a mongodb (.bson and .json) I want to replace my current meteor mongodb, I have read al this post about it:
meteor: how can I backup my mongo database
Mongorestore, from meteor production server to local
Restore a mongodb in meteor production server
MongoDB: mongorestore: command not found (this actually have nothing to do with my problem)
and I end writing in my terminal while meteor app was running
mongorestore -h 127.0.0.1:3001 --port 3001 -d meteor ~/Github/mongo/mongoDBRESTORE/admin/
but the problem its that terminal output is not command found
does anybody know what Im doing wrong, what its the right way to do this?
As #rgoomar pointed in the comments I need to download https://www.mongodb.org/downloads the mongodb bin files then I simple execute ~/mongodb/bin/mongorestore -h 127.0.0.1:3001 -db meteor ~/Github/mongo/mongoDBRESTORE/admin
and worked!