|
| 1 | +# Description: |
| 2 | +# A script that allows hubot to make a dump of the database and send it to a public access folder. |
| 3 | +# |
| 4 | +# Dependencies: |
| 5 | +# SSH2 (https://github.com/mscdex/ssh2) |
| 6 | +# |
| 7 | +# Configuration: |
| 8 | +# |
| 9 | +# Commands: |
| 10 | +# hubot dbdump stage - creates a dump of the db from the staging database |
| 11 | +# hubot dbdump prod - creates a dump of the db from the production database |
| 12 | +ConnectServ = require("ssh2") |
| 13 | + |
| 14 | +module.exports = (robot) -> |
| 15 | + robot.respond /dbdump\s?(.*)?/i, (msg) -> |
| 16 | + if msg.match[1] == 'stage' |
| 17 | + version = 'stage' |
| 18 | + envhost = process.env['STAGING_SSH_HOST'] |
| 19 | + envport = process.env['STAGING_DB_SSH_PORT'] |
| 20 | + envuser = process.env['STAGING_DB_SSH_USER'] |
| 21 | + envprivatekey = process.env['PRIVATEKEY'] |
| 22 | + |
| 23 | + if msg.match[1] == 'prod' |
| 24 | + version = 'prod' |
| 25 | + envhost = process.env['PROD_SSH_HOST'] |
| 26 | + envport = process.env['PROD_DB_SSH_PORT'] |
| 27 | + envuser = process.env['PROD_DB_SSH_USER'] |
| 28 | + envprivatekey = process.env['PRIVATEKEY'] |
| 29 | + |
| 30 | + moo = "" |
| 31 | + c = new ConnectServ() |
| 32 | + c.on "connect", -> |
| 33 | + |
| 34 | + c.on "ready", -> |
| 35 | + c. exec "pg_dump whwn | gzip > whwn.gz; scp whwn.gz [email protected]:~/public_html/whwn.gz", ( err, stream) -> |
| 36 | + throw err if err |
| 37 | + stream.on "data", (data, extended) -> |
| 38 | + console.log ((if extended is "stderr" then "STDERR: " else "STDOUT: ")) + data |
| 39 | + moo = data.toString('utf-8') |
| 40 | + |
| 41 | + stream.on "exit", (code, signal) -> |
| 42 | + c.end() |
| 43 | + |
| 44 | + c.on "error", (err) -> |
| 45 | + console.log "Connection :: error :: " + err |
| 46 | + |
| 47 | + c.on "end", -> |
| 48 | + console.log "Connection :: end" |
| 49 | + |
| 50 | + c.on "close", (had_error) -> |
| 51 | + console.log "Connection :: close" |
| 52 | + msg.send moo |
| 53 | + msg.send "DB Dump Complete, download at dev1.churenshao.com/~robot/whwn.gz" |
| 54 | + |
| 55 | + c.connect |
| 56 | + host: envhost |
| 57 | + port: envport |
| 58 | + username: envuser |
| 59 | + privateKey: envprivatekey |
0 commit comments