Is there a simple way to export the data from a meteor deployed app?

So, for example, if I had deployed an app named test.meteor.com...

How could I easily download the data that has been collected by that app - so that I could run it locally with data from the deployed app?

Solution 1

To get the URL for your deployed site at meteor.com use the command (you may need to provide your site password if you password protected it):

meteor mongo --url YOURSITE.meteor.com

Which will return something like :

mongodb://client:[email protected]:27017/YOURSITE_meteor_com

Which you can give to a program like mongodump

mongodump -u client -h sky.member1.mongolayer.com:27017 -d YOURSITE_meteor_com\
          -p PASSWORD

The password is only good for one minute. For usage:

$ meteor --help mongo

Solution 2

And here's how to do the opposite: (uploading your local monogo db to meteor)

https://gist.github.com/IslamMagdy/5519514

# How to upload local db to meteor:

# -h = host, -d = database name, -o = dump folder name
mongodump -h 127.0.0.1:3002 -d meteor -o meteor

# get meteor db url, username, and password
meteor mongo --url myapp.meteor.com

# -h = host, -d = database name (app domain), -p = password, folder = the path to the dumped db
mongorestore -u client -h c0.meteor.m0.mongolayer.com:27017 -d myapp_meteor_com -p 'password' folder/

Solution 3

Based on Kasper Souren's solution I created an updated script that works with current versions of Meteor and also works when you protect your remote Meteor app with a password.

Please create the following script parse-mongo-url.coffee:

spawn = require('child_process').spawn
mongo = spawn 'meteor', ['mongo', '--url', 'YOURPROJECT.meteor.com'], stdio: [process.stdin, 'pipe', process.stderr]

mongo.stdout.on 'data', (data) ->
    data = data.toString()
    m = data.match /mongodb:\/\/([^:]+):([^@]+)@([^:]+):27017\/([^\/]+)/
    if m?
        process.stdout.write "-u #{m[1]} -p #{m[2]} -h #{m[3]} -d #{m[4]}"
    else
        if data == 'Password: '
            process.stderr.write data

Then execute it like this in a *nix shell:

mongodump `coffee parse-mongo-url.coffee`

Solution 4

I have created a tool, mmongo, that wraps all the Mongo DB client shell commands for convenient use on a Meteor database. If you use npm (Node Package Manager), you can install it with:

npm install -g mmongo

Otherwise, see README.

To back up your Meteor database, you can now do:

mmongo test.meteor.com dump 

To upload it to your local development meteor would be:

mmongo restore dump/test_meteor_com

And if you accidentally delete your production database:

mmongo test.meteor.com --eval 'db.dropDatabase()'   # whoops!

You can easily restore it:

mmongo test.meteor.com restore dump/test_meteor_com 

If you'd rather export a collection (say tasks) to something readable:

mmongo test.meteor.com export -c tasks -o tasks.json

Then you can open up tasks.json in your text editor, do some changes and insert the changes with:

mmongo test.meteor.com import tasks.json -c tasks --upsert

Github, NPM

Solution 5

I suppose your data is in a mongodb database, so if that's the case, the question is more mongo-related than meteor. You may take a look at mongoexport and mongoimport command line tools.

Edit (for example):

mongoexport -h flame.mongohq.com:12345 -u my_user -p my_pwd -d my_db -c my_coll

You need to install mongodb on your computer to have this command line tool, and obviously you need your mongodb informations. In the above example, I connect to MongoHQ (flame.mongohq.com is the host, '12345' is the port of your mongo server), but I don't know which Mongo host is actually used by the meteor hosting. If you tried the Meteor examples (TODOs, Leaderboard, etc.) locally, chances are you already installed Mongo, since it uses a local server by default.

Solution 6

Here is another solution in bash

#! /bin/bash
# inspired by http://stackoverflow.com/questions/11353547/bash-string-extraction-manipulation

# http://www.davidpashley.com/articles/writing-robust-shell-scripts/
set -o nounset
set -o errexit
set -o pipefail
set -x

# stackoverflow.com/questions/7216358/date-command-on-os-x-doesnt-have-iso-8601-i-option
function nowString {
    date -u +"%Y-%m-%dT%H:%M:%SZ"
}

NOW=$(nowString)

# prod_url="mongodb://...:[email protected]:.../..."
prod_pattern="mongodb://([^:]+):([^@]+)@([^:]+):([^/]+)/(.*)"
prod_url=$(meteor mongo katapoolt --url | tr -d '\n')
[[ ${prod_url} =~ ${prod_pattern} ]]
PROD_USER="${BASH_REMATCH[1]}"
PROD_PASSWORD="${BASH_REMATCH[2]}"
PROD_HOST="${BASH_REMATCH[3]}"
PROD_PORT="${BASH_REMATCH[4]}"
PROD_DB="${BASH_REMATCH[5]}"
PROD_DUMP_DIR=dumps/${NOW}
mkdir -p dumps

# local_url="mongodb://...:.../..."
local_pattern="mongodb://([^:]+):([^/]+)/(.*)"
local_url=$(meteor mongo --url | tr -d '\n')
[[ ${local_url} =~ ${local_pattern} ]]
LOCAL_HOST="${BASH_REMATCH[1]}"
LOCAL_PORT="${BASH_REMATCH[2]}"
LOCAL_DB="${BASH_REMATCH[3]}"

mongodump --host ${PROD_HOST} --port ${PROD_PORT} --username ${PROD_USER} --password ${PROD_PASSWORD} --db ${PROD_DB} --out ${PROD_DUMP_DIR}
mongorestore --port ${LOCAL_PORT} --host ${LOCAL_HOST} --db ${LOCAL_DB} ${PROD_DUMP_DIR}/${PROD_DB}

Solution 7

meteor-backup is by far the easiest way to do this.

sudo npm install -g meteor-db-utils
meteor-backup [domain] [collection...]

As of March 2015 you still need to specify all collections you want to fetch though (until this issue is resolved).


Stuff from the past below

I'm doing

mongodump $(meteor mongo -U example.meteor.com | coffee url2args.cfee)

together with this little coffeescript, with a mangled extension in order not to confuse Meteor, url2args.cfee:

stdin = process.openStdin()
stdin.setEncoding 'utf8'
stdin.on 'data', (input) ->
  m = input.match /mongodb:\/\/(\w+):((\w+-)+\w+)@((\w+\.)+\w+):27017\/(\w+)/
  console.log "-u #{m[1]} -h #{m[4]} -p #{m[2]} -d #{m[6]}"

(it would be nicer if meteor mongo -U --mongodumpoptions would give these options, or if mongodump would accept the mongo:// URL)

Solution 8

# How to upload local db to meteor:

# -h = host, -d = database name, -o = dump folder name
mongodump -h 127.0.0.1:3001 -d meteor -o meteor

# get meteor db url, username, and password
meteor mongo --url myapp.meteor.com

# -h = host, -d = database name (app domain), -p = password, folder = the path to the dumped db
mongorestore -u client -h http://production-db-a2.meteor.io:27017 -d myapp_meteor_com -p 'password' folder/

While uploading local db to remote db, having an assertion Exception

[email protected]PC:$ mongorestore -u client -h http://production-db-a2.meteor.io:27017 -d myapp_meteor_com -p my_password local/
2015-04-22T16:37:38.504+0530 Assertion failure _setName.size() src/mongo/client/dbclientinterface.h 219
2015-04-22T16:37:38.506+0530 0xdcc299 0xd6c7c8 0xd4bfd2 0x663468 0x65d82e 0x605f98 0x606442 0x7f5d102f8ec5 0x60af41 
 mongorestore(_ZN5mongo15printStackTraceERSo+0x39) [0xdcc299]
 mongorestore(_ZN5mongo10logContextEPKc+0x198) [0xd6c7c8]
 mongorestore(_ZN5mongo12verifyFailedEPKcS1_j+0x102) [0xd4bfd2]
 mongorestore(_ZN5mongo16ConnectionStringC2ENS0_14ConnectionTypeERKSsS3_+0x1c8) [0x663468]
 mongorestore(_ZN5mongo16ConnectionString5parseERKSsRSs+0x1ce) [0x65d82e]
 mongorestore(_ZN5mongo4Tool4mainEiPPcS2_+0x2c8) [0x605f98]
 mongorestore(main+0x42) [0x606442]
 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5) [0x7f5d102f8ec5]
 mongorestore() [0x60af41]
terminate called after throwing an instance of 'mongo::AssertionException'
  what():  assertion src/mongo/client/dbclientinterface.h:219
Aborted (core dumped)

Solution 9

I made this simple Rakefile to copy the live db to local.

To restore the live db to my local machine I just do...

rake copy_live_db

Replace myapp with the name of your meteor.com - e.g myapp.meteor.com.

require 'rubygems'
require 'open-uri'

desc "Backup the live db to local ./dump folder"
task :backup_live_db do
  uri = `meteor mongo myapp --url`
  pass = uri.match(/client:([^@]+)@/)[1]
  puts "Using live db password: #{pass}"
  `mongodump -h meteor.m0.mongolayer.com:27017 -d myapp_meteor_com -u client -p #{pass}`
end


desc "Copy live database to local"
task :copy_live_db => :backup_live_db do
  server =  `meteor mongo --url`
  uri = URI.parse(server)
  `mongorestore --host #{uri.host} --port #{uri.port} --db meteor --drop dump/myapp_meteor_com/`
end

desc "Restore last backup"
task :restore do
  server =  `meteor mongo --url`
  uri = URI.parse(server)
  `mongorestore --host #{uri.host} --port #{uri.port} --db meteor --drop dump/myapp_meteor_com/`
end

Solution 10

To use an existing local mongodb database on your meteor deploy myAppName site, you need to dump, then restore the mongodb.

Follow the instructions above to mongodump (remember the path) and then run the following to generate your 'mongorestore' (replaces the second step and copy/pasting):

CMD=meteor mongo -U myAppName.meteor.com | tail -1 | sed 's_mongodb://\([a-z0-9\-]*\):\([a-f0-9\-]*\)@\(.*\)/\(.*\)_mongorestore -u \1 -p \2 -h \3 -d \4_'

then

$CMD /path/to/dump 

From Can mongorestore take a single url argument instead of separate arguments?

Solution 11

I think you can use a remotely mounted file system via sshfs and then rsync to synchronize the mongodb's folder itself or your entire Meteor folder I believe as well. This is like doing an incremental backup and potentially more efficient. It's possible to use the same solution for sending changes of your code, etc. so why not get you database changes back at the same time too?! (killing 2 birds with 1 stone)

Solution 12

Here is a simple bash script that lets you dump your database from meteor.com hosted sites.

#!/bin/bash

site="rankz.meteor.com"
name="$(meteor mongo --url $site)"
echo $name

IFS='@' read -a mongoString <<< "$name"

echo "HEAD: ${mongoString[0]}"
echo "TAIL: ${mongoString[1]}"

IFS=':' read -a pwd <<< "${mongoString[0]}"

echo "${pwd[1]}"
echo "${pwd[1]:2}"
echo "${pwd[2]}"


IFS='/' read -a site <<< "${mongoString[1]}"

echo "${site[0]}"
echo "${site[1]}"


mongodump -u ${pwd[1]:2} -h ${site[0]} -d ${site[1]}\
          -p ${pwd[2]}