1 | 'use strict'
|
2 |
|
3 | const co = require('co')
|
4 | const cli = require('heroku-cli-util')
|
5 |
|
6 | const TZ = {
|
7 | 'PST': 'America/Los_Angeles',
|
8 | 'PDT': 'America/Los_Angeles',
|
9 | 'MST': 'America/Boise',
|
10 | 'MDT': 'America/Boise',
|
11 | 'CST': 'America/Chicago',
|
12 | 'CDT': 'America/Chicago',
|
13 | 'EST': 'America/New_York',
|
14 | 'EDT': 'America/New_York',
|
15 | 'Z': 'UTC',
|
16 | 'GMT': 'Europe/London',
|
17 | 'BST': 'Europe/London',
|
18 | 'CET': 'Europe/Paris',
|
19 | 'CEST': 'Europe/Paris'
|
20 | }
|
21 |
|
22 | function parse (at) {
|
23 | let m = at.match(/^([0-2]?[0-9]):00 ?(\S*)$/)
|
24 | if (!m) throw new Error("Invalid schedule format: expected --at '[HOUR]:00 [TIMEZONE]'")
|
25 | let [, hour, timezone] = m
|
26 |
|
27 | return {hour, timezone: TZ[timezone.toUpperCase()] || timezone || 'UTC'}
|
28 | }
|
29 |
|
30 | function * run (context, heroku) {
|
31 | const host = require('../../lib/host')
|
32 | const fetcher = require('../../lib/fetcher')(heroku)
|
33 |
|
34 | const {app, args, flags} = context
|
35 |
|
36 | let schedule = parse(flags.at)
|
37 | let db = yield fetcher.addon(app, args.database)
|
38 |
|
39 | let at = cli.color.cyan(`${schedule.hour}:00 ${schedule.timezone}`)
|
40 | yield cli.action(`Scheduling automatic daily backups of ${cli.color.addon(db.name)} at ${at}`, co(function * () {
|
41 | schedule.schedule_name = db.name
|
42 |
|
43 | yield heroku.post(`/client/v11/databases/${db.name}/transfer-schedules`, {
|
44 | body: schedule,
|
45 | host: host(db)
|
46 | })
|
47 | }))
|
48 | }
|
49 |
|
50 | module.exports = {
|
51 | topic: 'pg',
|
52 | command: 'backups:schedule',
|
53 | description: 'schedule daily backups for given database',
|
54 | needsApp: true,
|
55 | needsAuth: true,
|
56 | args: [
|
57 | {name: 'database', optional: true}
|
58 | ],
|
59 | flags: [
|
60 | {name: 'at', required: true, hasValue: true, description: "at a specific (24h) hour in the given timezone. Defaults to UTC. --at '[HOUR]:00 [TIMEZONE]'"}
|
61 | ],
|
62 | run: cli.command({preauth: true}, co.wrap(run))
|
63 | }
|