Skip to content

Instantly share code, notes, and snippets.

@jashkenas
Forked from jbarnette/Cakefile
Created August 7, 2011 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jashkenas/1130537 to your computer and use it in GitHub Desktop.
Save jashkenas/1130537 to your computer and use it in GitHub Desktop.
fs = require "fs"
path = require "path"
rimraf = require "rimraf"
{spawn} = require "child_process"
url = require "url"
heroku =
config: (callback) ->
child = spawn "heroku", ["config", "--shell"]
raw = ""
child.stdout.on "data", (data) ->
raw += data
child.on "exit", (code) ->
console.log "heroku config exit: #{code}" if code isnt 0
config = {}
pattern = /^([^=]+)=(.*)$/gm
config[m[1]] = m[2] while m = pattern.exec raw
callback config
task "db:pull", "Grab a copy of the production DB.", ->
heroku.config (cfg) ->
dbURL = url.parse cfg.MONGOHQ_URL
db = dbURL.pathname.substr 1
dir = "tmp/mongodump"
host = dbURL.hostname
port = dbURL.port
[username, password] = dbURL.auth.split ":"
args = ["--host", host, "--port", port, "--db", db,
"--username", username, "--password", password,
"--out", dir]
rimraf dir, (err) ->
console.log "rimraf: #{err}" if err
spawn("mongodump", args).on "exit", (code) ->
console.log "mongodump exit: #{code}" if code isnt 0
args = ["local-dev", "--eval", "db.dropDatabase()"]
spawn("mongo", args).on "exit", (code) ->
console.log "mongo exit: #{code}" if code isnt 0
args = ["--drop", "--db", "local-dev", path.join(dir, db)]
spawn("mongorestore", args).on "exit", (code) ->
console.log "mongorestore exit: #{code}" if code isnt 0
rimraf dir, (err) ->
console.log "last rimraf: #{err}" if err
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment