-
-
Save jashkenas/1130537 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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