49

I created a database on my local ubuntu machine.

How can I transfer it to my remote server (ec2 Ubuntu)

Community
  • 1
  • 1
Praveen Singh Yadav
  • 1,831
  • 4
  • 20
  • 31

6 Answers6

91

TL;DR

Use mongodump and mongorestore to take (and restore) a full binary backup of your MongoDB database. Compress the backup dump directory to make it faster to copy to your Amazon instance (BSON tends to compress very well).

Best practices

Rather than following adhoc instructions, I would strongly recommend reading the standard Backup and Restore with MongoDB Tools tutorial in the MongoDB manual.

You can also use a Filesystem snapshot, but mongodump and mongorestore only export the data so your backup will be smaller (i.e. your remote server will not inherit any excessive storage allocation due to preallocation).

tugberk
  • 57,477
  • 67
  • 243
  • 335
Stennie
  • 63,885
  • 14
  • 149
  • 175
  • 1
    Hello..can you say what is the link want to i add to connect ubuntu server monogo db. like `mongodb://localhost:27017/myapp` we user this for connect localhost database,when connect to remoteserver only we want to add our server ip Address? – hash Oct 17 '14 at 04:06
  • @Stennie I got `recv(): message len 759714643 is too large. Max is 48000000`, do you know how to deal with this? – Bruce Yong Li May 01 '15 at 19:50
  • Note that when you restore database using `mongorestore` from `mongodump` files, it's very very very slow operation, even on few GB databases. It would take few hours to few days or even weeks! – Shinebayar G May 19 '20 at 01:15
  • I was able to `mongorestore` a 33 GB mongo dump in under 7 minutes on an 8 core VPS. – jorisw Jul 11 '23 at 20:36
14

Auto Sync between 2 Server
If your local host is available from outside you can use copydb in admin.
Migrate mongodb data one hardware to another hardware:

user@server:~$ mongo
MongoDB shell version: 2.6.11
connecting to: test
> use admin
switched to db admin
>db.runCommand({copydb:1,fromhost:'your previous host',fromdb:'Auctions_Data',todb:'Auctions_Data'})
{ "ok" : 1 }
Manoj Sahu
  • 2,774
  • 20
  • 18
6

In addition to the other solutions you can create a bash script and preform this very easily.

#!/bin/bash

HOST="somehost.com"
PORT="2345"
REMOTE_DB="some-remote-db"
LOCAL_DB="your-local-db"
USER="remote-user-name"
PASS="passwordForRemoteUser"

## DUMP REMOTE DATABASE
echo "Dumping '$HOST:$PORT/$REMOTE_DB'..."
mongodump --host $HOST:$PORT --db $REMOTE_DB -u $USER -p $PASS

## RESTORE DUMP DIRECTORY
echo "Restoring to '$LOCAL_DB'..."
mongorestore --db $LOCAL_DB --drop dump/$REMOTE_DB

## REMOVE DUMP FILES
echo "Removing dump files..."
rm -r dump

echo "Finished."
sirrele
  • 171
  • 1
  • 6
4

You can create a database backup and transfer it to a S3 bucket.

First, install s3cmd:

sudo yum --enablerepo epel install s3cmd

#to configure s3cmd
s3cmd --configure

Then create a backup routine in a backup.sh file:

#!/bin/bash

#Force file syncronization and lock writes
mongo admin --eval "printjson(db.fsyncLock())"

MONGODUMP_PATH="/usr/bin/mongodump"
MONGO_HOST="prod.example.com"
MONGO_PORT="27017"
MONGO_DATABASE="dbname"

TIMESTAMP=`date +%F-%H%M`
S3_BUCKET_NAME="bucketname"
S3_BUCKET_PATH="mongodb-backups"


# Create backup
$MONGODUMP_PATH -h $MONGO_HOST:$MONGO_PORT -d $MONGO_DATABASE

# Add timestamp to backup
mv dump mongodb-$HOSTNAME-$TIMESTAMP
tar cf mongodb-$HOSTNAME-$TIMESTAMP.tar mongodb-$HOSTNAME-$TIMESTAMP

# Upload to S3
s3cmd put mongodb-$HOSTNAME-$TIMESTAMP.tar s3://$S3_BUCKET_NAME/$S3_BUCKET_PATH/mongodb-$HOSTNAME-$TIMESTAMP.tar


#Unlock databases writes
mongo admin --eval "printjson(db.fsyncUnlock())"

When you run bash backup.sh a new file will be created like mongodb-localhost-10-10-2013.tar

On the remote server you can use a wget to download file from Amazon S3. Extract backup file using tar like tar -xvf backupname.tar.

To restore you can use:

mongorestore --dbpath <database path> <directory to the backup>

Like this:

mongorestore --dbpath /var/lib/mongo backup_directory_name

I hope this is enough to help you

  • Then how to add link of mondodb to my App. in `service.js` `mongoose.connect('mongodb://localhost:27017/mean-demo');` – hash Oct 13 '14 at 04:42
  • tried the `mongorestore ...` line, get 'If you are running a mongod on the same path you should connect to that instead of direct data file access'. Found that worked fine without --dbpath – Putnik Jun 05 '16 at 16:48
0

Install mongo software on your remote server Stop mongod on your local computer. copy the data files and configuration to the remote computer. verify permissions of the data files are the same as on your local computer. and then start mongod on the remote server.

Tata
  • 802
  • 9
  • 19
-1

Now that you have found your data files, and scp-ed them to the necessary server location, say /data/db/* , you can start your mongod command with the param dbpath as such : mongod --dbpath /data/db Ensure that you have copied all the files correctly into this new folder.

brayne
  • 1,355
  • 2
  • 16
  • 27