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