Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can accomplish this with SSH Tunneling, setting up your remote MongoDB instance to run on one of your local ports. By default, MongoDB runs on 27017, so in the example below, I've chosen to map my remote MongoDB instance to my local 27018 port. </p> <p>If on your trying to copy a database from SERVER1 to LOCALHOST, you could run this command on your LOCALHOST:</p> <p><code>ssh -L27018:localhost:27017 SERVER1</code></p> <p>(Obviously replace SERVER1 with your actual server or ssh alias)</p> <p>This opens an SSH connection to SERVER1, but also maps the port 27018 on LOCALHOST to the remote port 27017 on SERVER1. Don't close that SSH connection, and now try to connect to MongoDB on your localhost machine with port 27018, like so:</p> <p><code>mongo --port 27018</code></p> <p>You'll notice this is now the data on SERVER1, except you're accessing it from your local machine. </p> <p>Just running MongoDB normally:</p> <p><code>mongo</code> (or <code>mongo --port 27107</code>)</p> <p>Will be your local machine. </p> <p>Now, since you technically have (on your LOCALHOST, where you ran the SSH tunnel):</p> <ul> <li>MongoDB (LOCALHOST) on 27017 </li> <li>MongoDB (SERVER1) on 27018</li> </ul> <p>You can just use the <code>db.copyDatabase()</code> function inside MongoDB (LOCALHOST) to copy over data. </p> <p><strong>FROM LOCALHOST ON PORT 27017 (Executing on live will DROP YOUR DATA)</strong></p> <pre><code>// Use the right DB use DATABASENAME; // Drop the Existing Data on LOCALHOST db.dropDatabase(); // Copies the entire database from 27018 db.copyDatabase("DATABASENAME", "DATABASENAME", "localhost:27018"); </code></pre> <p>You should be able to wrap this all up into a shell script that can execute all of these commands for you. I have one myself, but it actually has a few extra steps that would probably make it a bit more confusing :)</p> <p>Doing this, and using MongoDB's native db.copyDatabase() function will prevent you from having to dump/zip/restore. Of course, if you still want to go that route, it wouldn't be too hard to run <code>mongodump</code>, export the data, tar/gzip it, then use <code>scp TARGETSERVER:/path/to/file /local/path/to/file</code> to pull it down and run a <code>mongorestore</code> on it.</p> <p>Just seems like more work!</p> <p><strong>Edit</strong> - Here's a SH and JS file that go together to make a shell script you can run this with. <strong><em>Run these on your LOCALHOST</em></strong>, don't run them on live or it'll do the db.dropDatabase on live. Put these two files in the same folder, and replace <em>YOURSERVERNAME</em> in <code>pull-db.sh</code> with the domain/ip/ssh alias, and then in <code>pull-db.js</code> change DBNAMEHERE to whatever your database name is. </p> <p>I normally create a folder called <code>scripts</code> in my projects, and using Textmate, I just have to hit <code>⌘+R</code> while having <code>pull-db.sh</code> open to edit in order to execute it.</p> <p><strong>pull-db.sh</strong></p> <pre><code>ssh -L27018:localhost:27017 YOURSERVERNAME ' echo "Connected on Remote End, sleeping for 10"; sleep 10; exit' &amp; echo "Waiting 5 sec on local"; sleep 5; echo "Connecting to Mongo and piping in script"; cat pull-db.js | mongo </code></pre> <p><strong>pull-db.js</strong></p> <pre><code>use DBNAMEHERE; db.dropDatabase(); use DBNAMEHERE; db.copyDatabase("DBNAMEHERE","DBNAMEHERE","localhost:27018"); </code></pre> <p>I added some extra code to the shell script to echo out what it's doing (sorta). The sleep timers in the script are just to give the SSH connections time to get connected before the next line is run. Basically, here's what happens:</p> <ol> <li>First line of the code creates the tunnel on your machine, and sends the ECHO, SLEEP, then EXIT to the remote SSH session. </li> <li>It then waits 5 seconds, which allows the SSH session in step 1 to connect.</li> <li>Then we pipe the pull-db.js file into the local mongo shell. (Step #1 should be done within 5 sec...)</li> <li>The pull-db.js should be running in mongo now, and the SSH terminal in Step #1 has probably run for 10 seconds after it's connection opened, and the EXIT is sent to it's session. The command is issued, HOWEVER, the SSH session will actually stay open until the activity from Step #3 is complete. </li> <li>As soon as your pull-db.js script finishes pulling all of your data from the remote server, the EXIT command issued in Step #1 on the remote server is finally allowed to close the connection, unbinding 27108 on your localhost.</li> </ol> <p>You should now have all of the data from your remote database in your localhost.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload