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

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

Related

Dumping and restoring MongoDB in a single command

I'm trying to dump an existing MongoDB into a new MongoDB without using dump files at all.
Is there a way to pass data from mongodump into mongorestore using pipes?
something like this:
mongodump --host=0.0.0.0 --port=27017 --db=database_name | mongorestore --host 0.0.0.0 -d database_name --port 27000
Thanks!
I found this solution:
mongodump -vvvvv --host=0.0.0.0 --port=27017 --db=database_name --archive | mongorestore --archive -vvvvv
Explained:
mongodump a MongoDB util to dump (backup) a database
-vvvvv v for verbose, write to stdout the output of the command, more data per v, you can use -v -vv -vvv etc...
--host specify the host of the MongoDB you want to dump
--host specify the port of the MongoDB you want to dump
--archive writes the output to a specified archive file or, if the archive file is unspecified, writes to the standard output (stdout)
| takes the output of the command and passes it as input to the next command
mongorestore a MongoDB util to restore a database

Backup mongodb directly to remote server

Is there a way to save mongodump archive directly on remote machine (via ssh or rsync) without saving in locally?
I see --out option in docs but no suitable examples
If you do not specify the name of the --archive, it will just go to stdout, where you catch it via | ssh. This is work for me
mongodump --db dbname --gzip --archive | ssh user#remotehost "cat > /path/to/dump.gz"
Or
mongodump --db dbname --archive | gzip -c | ssh user#remotehost "cat > /path/to/dump.gz"

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

Mongodump not working on AWS EC2 instance

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.

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