Mongodump not working on AWS EC2 instance - mongodb

I tried Mongodump on AWS EC2 instance. There is no error, but the files are not dumped.
[ec2-user ~]$ sudo mongodump --host localhost:27017 --db test--out /var/backups/
connected to: localhost:27017
2017-01-19T01:56:05.608+0000 DATABASE: test to /var/backups/test
How to take a dump inside AWS EC2? The database is in data/db folder.

In my mongodb folder first I made a folder for backups:
sudo mkdir backups
Then used 777 just in case, later I changed it to 766
sudo chmod 777 -R backups
sudo mongodump -h ec2-xx-xx-xxx-xxx.compute-1.amazonaws.com --port 27017 --db your_db_name_here -u your_username_here -p your_password_here --out backups/
Of course change your host also
and if you want backup zipped then add this
--oplog --gzip
Hope that helps

First you should set required permission for out directory
sudo chmod 777 -R /var/backups
then
sudo mongodump --port 27017 --db test --out /var/backups/

Take Mongodb backup (Aws DocumentDB)
mongodump --host="Documentdb endpoint" --port=27017 -u "username" -p "" -d "dbname" --authenticationDatabase "admin" --gzip --archive > /path/dbname.gz
enter password
Note
--gzip: zip used in linux.
path: where you want to download the db zip file.

Related

Why doesn't mongoexport generate the output file

I have a Mongo database on my server and I can connect to it using docker.
I run the following command:
docker exec -it mongodb mongoexport --port 27017 -u "username" -p "password" --authenticationDatabase "admin" -d databaseName -c user --out /var/www/myTest/output.json
It seems that the command works without any problem and I get the message:
connected to: mongodb://localhost:27017/
exported 16 records
However, when I use FileZilla and look at the directory /var/www/myTest, there is no file there. The folder is empty (I had created the folder myself earlier. If I use a new folder name, the folder is also not generated).
What have I done wrong? Is the file somewhere else?
Thanks in advance,
That directory and the file is inside the mongoexport container.
UPDATE:
You could also bind mount a directory from your host to the containers:
docker run --rm -v /var/www/myTestOnHost:/var/www/myTestOnContainer -it mongodb mongoexport --port 27017 -u "username" -p "password" --authenticationDatabase "admin" -d databaseName -c user --out /var/www/myTestOnContainer/output.json
docs: https://docs.docker.com/storage/bind-mounts/
--rm flag for docker run will remove the container when it finished running, that is executing the mongoexport --port 27017 -u "username" -p "password" --authenticationDatabase "admin" -d databaseName -c user --out /var/www/myTestOnContainer/output.json command

how to create a copy of my mongo database to remote server

I want to copy the db from a server to another server
since i found some answer that works from my machine to another machine at home
(the 2 machine have the same username):
ssh -fN -L 27117:localhost:27017 remote_host
mongodump --port 27117 --db dbname --archive | mongorestore --archive
i tried doing it by
ssh -fN -L 27117:localhost:27017 myUsername#remote_host
mongodump --port 27117 --db dbname --archive | mongorestore --archive
but it doest work
i also want to know what does -fN and -L means
thanks in advance maam/sir

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

Mongorestore to a different database

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

Mongorestore, from meteor production server to local

I've found plenty of good instructions on how to use mongodump and mongorestore, to back up my meteor production server and restore the backup if need be:
meteor mongo --url myApp.meteor.com
mongodump -u client -h production-db-b2.meteor.io:27017 -d myApp_meteor_com -out dump/2014_10_21 -p [password from meteor mongo --url]
mongorestore -u client -h production-db-b2.meteor.io:27017 -d myApp_meteor_com dump/2014_10_21_v2/myApp_meteor_com -p [password from meteor mongo --url]
What I haven't found is an explanation of is how to restore a backup-dump to my local meteor app. I have a mongodump output in my app folder. I'm not sure if I can use mongorestore or if there's something else I'm supposed to be doing.
The easiest way that I found:
cd in your project and execute meteor command
in another terminal:
mongorestore -h 127.0.0.1 --port 3001 -d meteor dump/meteor
change 127.0.0.1 if your localhost has different ip address and 3001 to a port you have mongodb on (it is usually 3001 or 3002, so try both), dump/meteor is the path to a dump you created previously.
Also the easiest way to export local db:
cd in your project and execute meteor command
In another terminal:
mongodump -h 127.0.0.1 --port 3001 -d meteor
again, change localhost ip and port if needed. . As a result, the dump/meteor folder with db files will be created in the folder you cd before running mongodump.
Good luck.
To accomplish the opposite, sending local app data to production app, I wrote this little shell script. It has been useful while I am developing locally and just getting the demo synced for the client to view. Note it has --drop at the end which will overwrite your production database, use with care!
It takes care of the client, pw, and server data from meteor mongo --url ... which expires after 1 minute and is really annoying to try to copy-paste within that time.
#!/usr/bin/env bash
mongodump -h 127.0.0.1:3001 -d meteor -o ~/www/APPNAME/server/dump
IN=`meteor mongo --url APPNAME.meteor.com`
client=`echo $IN | awk -F'mongodb://' '{print $2}' | awk -F':' '{print $1}'`
echo $client
pw=`echo $IN | awk -F':' '{print $3}' | awk -F'#' '{print $1}'`
echo $pw
serv=`echo $IN | awk -F'#' '{print $2}' | awk -F'/' '{print $1}'`
echo $serv
mongorestore -u $client -h $serv -d APPNAME_meteor_com dump/meteor -p $pw --drop
This is what I do:
I. Create a mongo dump in the server
DATE=$(date +%m%d%y_%H.%M);
mongodump --host localhost -d APPNAME -o /tmp/APPNAME_$DATE
tar -cjvvf /tmp/APPNAME_$DATE.tar.bz2 /tmp/APPNAME_$DATE
II. Download the dump in the development machine and unpack in /tmp
scp root#$HOST:/tmp/APPNAME_$DATE.tar.bz2 /tmp/
cp /tmp/APPNAME_$DATE.tar.bz2 .
mkdir -p /tmp/APPNAME_$DATE
cd /tmp/APPNAME_$DATE
tar -xjvf /tmp/APPNAME_$DATE.tar.bz2
III. Update local meteor development database
mongorestore --db meteor -h localhost --port 8082 --drop /tmp/APPNAME_$DATE/tmp/APPNAME_$DATE/APPNAME
You can use mongorestore.
It's pretty much the same as what you already did.
In your first line: meteor mongo --url myApp.meteor.com just remove the last part to so the line will read: meteor mongo --url.
When executed on your local machine you will get the information for the local instance of your meteor app. From that point you can just use mongorestore to restore your local db the way you already did remotely.
I use to do a meteor reset prior to a mongorestore, just to be sure that my db is empty, but I don't know if it's actual necessary.
Note that the app should be running when doing this.
I ended up writing a script to download the meteor database. Check it out at https://github.com/AlexeyMK/meteor-download
Usage (in your app's root):
curl https://raw.github.com/AlexeyMK/meteor-download/master/download.sh > download.sh
./download.sh yourapp.meteor.com`
I'm using Google Cloud for Meteor hosting, and wrote custom scripts.
I have this running on a cronjob to backup to google cloud storage:
https://github.com/markoshust/mongo-docker-backup-gcloud/blob/master/mongobackup.sh
#!/bin/bash
MONGO_DB=dbname
MONGO_HOST=127.0.0.1
HOST_DIR=/home/YOURNAME
BACKUP_DIR=/mongobackup
BUCKET=gs://BUCKET_NAME
DATE=`date +%Y-%m-%d:%H:%M:%S`
/usr/bin/docker run --rm \
-v $HOST_DIR/$BACKUP_DIR:$BACKUP_DIR \
markoshust/mongoclient \
mongodump --host $MONGO_HOST --db $MONGO_DB --out $BACKUP_DIR
sudo mkdir -p $HOST_DIR/$BACKUP_DIR/$MONGO_DB/$DATE
sudo mv $HOST_DIR/$BACKUP_DIR/$MONGO_DB/* $HOST_DIR/$BACKUP_DIR/$MONGO_DB/$DATE
$HOST_DIR/gsutil/gsutil rsync -r $HOST_DIR/$BACKUP_DIR $BUCKET
sudo /bin/rm -rf $HOST_DIR/$BACKUP_DIR
Then to restore locally, I created another script which downloads the backup from google cloud storage, stores locally, then does a local restore:
https://github.com/markoshust/mongorestore.sh/blob/master/.mongorestore.sh
#!/bin/bash
## This script syncs a mongodb backup from a Google Cloud Storage bucket and
## mongorestore's it to a local db.
##
## Author: Mark Shust <mark#shust.com>
## Version: 1.1.0
BUCKET=my-bucket-name
FOLDER=folder-name/$1
BACKUP_DIR=./.backups/
DB_NAME=localdb
DB_HOST=localhost
DB_PORT=27017
if [ -z $1 ]; then
echo 'Please specify a subdirectory to sync from...'
exit 0
fi
mkdir -p $BACKUP_DIR
if [ ! -d $BACKUP_DIR ]; then
gsutil -m cp -r gs://$BUCKET/$FOLDER $BACKUP_DIR
fi
mongorestore --db $DB_NAME -h $DB_HOST --port $DB_PORT --drop $BACKUP_DIR/$1/
echo 'Database restore complete.'
I have this working with Meteor, stupid simple and works great :) Just switch db name to meteor and port to 3001 (or whatever config you have). It's meteor-agnostic so works with any mongodb host/platform.