-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathrestore.ts
146 lines (128 loc) · 5.1 KB
/
restore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import color from '@heroku-cli/color'
import {Command, flags} from '@heroku-cli/command'
import {Args, ux} from '@oclif/core'
import heredoc from 'tsheredoc'
import confirmCommand from '../../../lib/confirmCommand'
import backupsFactory from '../../../lib/pg/backups'
import {getAttachment} from '../../../lib/pg/fetcher'
import host from '../../../lib/pg/host'
import type {BackupTransfer} from '../../../lib/pg/types'
import {nls} from '../../../nls'
function dropboxURL(url: string) {
if (url.match(/^https?:\/\/www\.dropbox\.com/) && !url.endsWith('dl=1')) {
if (url.endsWith('dl=0'))
url = url.replace('dl=0', 'dl=1')
else if (url.includes('?'))
url += '&dl=1'
else
url += '?dl=1'
}
return url
}
export default class Restore extends Command {
static topic = 'pg'
static description = 'restore a backup (default latest) to a database'
static flags = {
'wait-interval': flags.integer({default: 3}),
extensions: flags.string({
char: 'e',
description: heredoc(`
comma-separated list of extensions to pre-install in the public schema
defaults to saving the latest database to DATABASE_URL
`),
}),
verbose: flags.boolean({char: 'v'}),
confirm: flags.string({char: 'c'}),
app: flags.app({required: true}),
remote: flags.remote(),
}
static args = {
backup: Args.string({description: 'URL or backup ID from another app'}),
database: Args.string({description: `${nls('pg:database:arg:description')} ${nls('pg:database:arg:description:default:suffix')}`}),
}
static examples = [
heredoc(`
# Basic Restore from Backup ID
$ heroku pg:backups:restore b101 DATABASE_URL --app my-heroku-app
`),
heredoc(`
# Restore from Another App
$ heroku pg:backups:restore example-app::b101 DATABASE_URL --app my-heroku-app
`),
heredoc(`
# Restore from a Public URL
$ heroku pg:backups:restore 'https://s3.amazonaws.com/my-bucket/mydb.dump' DATABASE_URL --app my-heroku-app
`),
heredoc(`
# Verbose Output
$ heroku pg:backups:restore b101 DATABASE_URL --app my-heroku-app --verbose
`),
heredoc(`
# Restore with Confirmation Prompt
$ heroku pg:backups:restore b101 DATABASE_URL --app my-heroku-app --confirm my-heroku-app
`),
heredoc(`
# Restore with a Specific Database Name
$ heroku pg:backups:restore b101 HEROKU_POSTGRESQL_PINK --app my-heroku-app
`),
]
public async run(): Promise<void> {
const {flags, args} = await this.parse(Restore)
const {app, 'wait-interval': waitInterval, extensions, confirm, verbose} = flags
const interval = Math.max(3, waitInterval)
const {addon: db} = await getAttachment(this.heroku, app as string, args.database)
const {name, wait} = backupsFactory(app, this.heroku)
let backupURL
let backupName = args.backup
if (backupName && backupName.match(/^https?:\/\//)) {
backupURL = dropboxURL(backupName)
} else {
let backupApp
if (backupName && backupName.match(/::/)) {
[backupApp, backupName] = backupName.split('::')
} else {
backupApp = app
}
const {body: transfers} = await this.heroku.get<BackupTransfer[]>(`/client/v11/apps/${backupApp}/transfers`, {hostname: host()})
const backups = transfers.filter(t => t.from_type === 'pg_dump' && t.to_type === 'gof3r')
let backup
if (backupName) {
backup = backups.find(b => name(b) === backupName)
if (!backup)
throw new Error(`Backup ${color.cyan(backupName)} not found for ${color.app(backupApp)}`)
if (!backup.succeeded)
throw new Error(`Backup ${color.cyan(backupName)} for ${color.app(backupApp)} did not complete successfully`)
} else {
backup = backups.filter(b => b.succeeded).sort((a, b) => {
if (a.finished_at < b.finished_at) {
return -1
}
if (a.finished_at > b.finished_at) {
return 1
}
return 0
}).pop()
if (!backup) {
throw new Error(`No backups for ${color.app(backupApp)}. Capture one with ${color.cyan.bold('heroku pg:backups:capture')}`)
}
backupName = name(backup)
}
backupURL = backup.to_url
}
await confirmCommand(app, confirm)
ux.action.start(`Starting restore of ${color.cyan(backupName)} to ${color.yellow(db.name)}`)
ux.log(heredoc(`
Use Ctrl-C at any time to stop monitoring progress; the backup will continue restoring.
Use ${color.cyan.bold('heroku pg:backups')} to check progress.
Stop a running restore with ${color.cyan.bold('heroku pg:backups:cancel')}.
`))
const {body: restore} = await this.heroku.post<{uuid: string}>(`/client/v11/databases/${db.id}/restores`, {
body: {backup_url: backupURL, extensions: this.getSortedExtensions(extensions as string)}, hostname: host(),
})
ux.action.stop()
await wait('Restoring', restore.uuid, interval, verbose, db.app.id as string)
}
protected getSortedExtensions(extensions: string | null | undefined): string[] | undefined {
return extensions?.split(',').map(ext => ext.trim().toLowerCase()).sort()
}
}