1 | 'use strict'
|
2 |
|
3 | const co = require('co')
|
4 | const cli = require('heroku-cli-util')
|
5 |
|
6 | function * run (context, heroku) {
|
7 | if (context.args.length > 0) {
|
8 |
|
9 |
|
10 | let pgbackupCommand = `backups:${context.args[0]}`
|
11 |
|
12 | const commands = require('../..').commands
|
13 | const cmd = commands.find(c => c.topic === 'pg' && c.command === pgbackupCommand)
|
14 |
|
15 | if (!cmd) {
|
16 | throw new Error(`Unknown pg:backups command: ${context.args[0]}`)
|
17 | }
|
18 |
|
19 | let args = {}
|
20 | context.args.slice(1).forEach(function (arg, index) {
|
21 | if (cmd.args[index]) {
|
22 | args[cmd.args[index].name] = arg
|
23 | } else {
|
24 | throw new Error(`Unexpected argument: ${arg}`)
|
25 | }
|
26 | })
|
27 |
|
28 | context = Object.assign(context, {args})
|
29 |
|
30 | yield cmd.run(context, heroku)
|
31 | } else {
|
32 | yield list(context, heroku)
|
33 | }
|
34 | }
|
35 |
|
36 | function * list (context, heroku) {
|
37 | const pgbackups = require('../../lib/pgbackups')(context, heroku)
|
38 | const sortBy = require('lodash.sortby')
|
39 | const host = require('../../lib/host')()
|
40 | const app = context.app
|
41 |
|
42 | function displayBackups (backups) {
|
43 | cli.styledHeader('Backups')
|
44 | if (!backups.length) {
|
45 | cli.log(`No backups. Capture one with ${cli.color.cmd('heroku pg:backups:capture')}`)
|
46 | } else {
|
47 | cli.table(backups, {
|
48 | columns: [
|
49 | {label: 'ID', format: (_, t) => cli.color.cyan(pgbackups.transfer.name(t))},
|
50 | {label: 'Created at', key: 'created_at'},
|
51 | {label: 'Status', format: (_, t) => pgbackups.transfer.status(t)},
|
52 | {label: 'Size', key: 'processed_bytes', format: b => pgbackups.filesize(b)},
|
53 | {label: 'Database', key: 'from_name', format: d => cli.color.configVar(d) || 'UNKNOWN'}
|
54 | ]
|
55 | })
|
56 | }
|
57 | cli.log()
|
58 | }
|
59 |
|
60 | function displayRestores (restores) {
|
61 | cli.styledHeader('Restores')
|
62 | restores = restores.slice(0, 10)
|
63 | if (!restores.length) {
|
64 | cli.log(`No restores found. Use ${cli.color.cmd('heroku pg:backups:restore')} to restore a backup`)
|
65 | } else {
|
66 | cli.table(restores, {
|
67 | columns: [
|
68 | {label: 'ID', format: (_, t) => cli.color.cyan(pgbackups.transfer.name(t))},
|
69 | {label: 'Started at', key: 'created_at'},
|
70 | {label: 'Status', format: (_, t) => pgbackups.transfer.status(t)},
|
71 | {label: 'Size', key: 'processed_bytes', format: b => pgbackups.filesize(b)},
|
72 | {label: 'Database', key: 'to_name', format: d => cli.color.configVar(d) || 'UNKNOWN'}
|
73 | ]
|
74 | })
|
75 | }
|
76 | cli.log()
|
77 | }
|
78 |
|
79 | function displayCopies (copies) {
|
80 | cli.styledHeader('Copies')
|
81 | copies = copies.slice(0, 10)
|
82 | if (!copies.length) {
|
83 | cli.log(`No copies found. Use ${cli.color.cmd('heroku pg:copy')} to copy a database to another`)
|
84 | } else {
|
85 | cli.table(copies, {
|
86 | columns: [
|
87 | {label: 'ID', format: (_, t) => cli.color.cyan(pgbackups.transfer.name(t))},
|
88 | {label: 'Started at', key: 'created_at'},
|
89 | {label: 'Status', format: (_, t) => pgbackups.transfer.status(t)},
|
90 | {label: 'Size', key: 'processed_bytes', format: b => pgbackups.filesize(b)},
|
91 | {label: 'From', key: 'from_name', format: d => cli.color.configVar(d) || 'UNKNOWN'},
|
92 | {label: 'To', key: 'to_name', format: d => cli.color.configVar(d) || 'UNKNOWN'}
|
93 | ]
|
94 | })
|
95 | }
|
96 | cli.log()
|
97 | }
|
98 |
|
99 | let transfers = yield heroku.get(`/client/v11/apps/${app}/transfers`, {host})
|
100 | transfers = sortBy(transfers, 'created_at').reverse()
|
101 |
|
102 | let backups = transfers.filter(t => t.from_type === 'pg_dump' && t.to_type === 'gof3r')
|
103 | let restores = transfers.filter(t => t.from_type !== 'pg_dump' && t.to_type === 'pg_restore')
|
104 | let copies = transfers.filter(t => t.from_type === 'pg_dump' && t.to_type === 'pg_restore')
|
105 |
|
106 | displayBackups(backups)
|
107 | displayRestores(restores)
|
108 | displayCopies(copies)
|
109 | }
|
110 |
|
111 | module.exports = {
|
112 | topic: 'pg',
|
113 | command: 'backups',
|
114 | description: 'list database backups',
|
115 | needsApp: true,
|
116 | needsAuth: true,
|
117 | variableArgs: true,
|
118 | flags: [
|
119 |
|
120 | {name: 'verbose', char: 'v', hidden: true},
|
121 | {name: 'confirm', char: 'c', hasValue: true, hidden: true},
|
122 | {name: 'output', char: 'o', hasValue: true, hidden: true},
|
123 | {name: 'wait-interval', hasValue: true, hidden: true},
|
124 | {name: 'at', hasValue: true, hidden: true},
|
125 | {name: 'quiet', char: 'q', hidden: true}
|
126 | ],
|
127 | run: cli.command({preauth: true}, co.wrap(run))
|
128 | }
|