Class: Heroku::Command::Pgbackups

Inherits:
Base
  • Object
show all
Includes:
Helpers::HerokuPostgresql
Defined in:
lib/heroku/command/pgbackups.rb

Overview

manage backups of heroku postgresql databases

Defined Under Namespace

Classes: TransferEndpoint

Instance Attribute Summary

Attributes inherited from Base

#args, #options

Instance Method Summary collapse

Methods included from Helpers::HerokuPostgresql

#hpg_resolve, #hpg_translate_db_opts_to_urls, #hpg_translate_fork_and_follow

Methods included from Helpers

#action, #app_owner, #ask, #confirm, #confirm_command, #create_git_remote, #debug, #debugging?, #default_org_host, #deprecate, #display, #display_header, #display_object, #display_row, #display_table, #error, #error_log, #error_log_path, error_with_failure, error_with_failure=, extended, extended_into, #fail, #format_bytes, #format_date, #format_error, #format_with_bang, #get_terminal_environment, #git, #has_git?, #has_git_remote?, #has_http_git_entry_in_netrc, #home_directory, #hprint, #hputs, included, included_into, #json_decode, #json_encode, #launchy, #line_formatter, #longest, #org?, #org_host, #output_with_bang, #quantify, #redisplay, #retry_on_exception, #run_command, #running_on_a_mac?, #running_on_windows?, #set_buffer, #shell, #spinner, #status, #stderr_print, #stderr_puts, #string_distance, #styled_array, #styled_error, #styled_hash, #styled_header, #suggestion, #time_ago, #truncate, #update_git_remote, #warn_if_using_jruby, #with_tty

Methods inherited from Base

#api, #app, #heroku, #initialize, namespace, #org, #org_api

Constructor Details

This class inherits a constructor from Heroku::Command::Base

Instance Method Details

#captureObject

pgbackups:capture [DATABASE]

capture a backup from a database id

if no DATABASE is specified, defaults to DATABASE_URL

-e, –expire # if no slots are available, destroy the oldest manual backup to make room


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
# File 'lib/heroku/command/pgbackups.rb', line 73

def capture
  attachment = resolve(shift_argument, "DATABASE_URL")
  validate_arguments!

  from_name = attachment.display_name
  from_url  = attachment.url
  to_url    = nil # server will assign
  to_name   = "BACKUP"

  opts      = {:expire => options[:expire]}

  backup = transfer!(from_url, from_name, to_url, to_name, opts)

  to_uri = URI.parse backup["to_url"]
  backup_id = to_uri.path.empty? ? "error" : File.basename(to_uri.path, '.*')
  display "\n#{from_name}  ----backup--->  #{backup_id}"

  backup = poll_transfer!(backup)

  if backup["error_at"]
    message  =   "An error occurred and your backup did not finish."
    message += "\nPlease run `heroku logs --ps pgbackups` for details."
    message += "\nThe database is not yet online. Please try again." if backup['log'] =~ /Name or service not known/
    message += "\nThe database credentials are incorrect."           if backup['log'] =~ /psql: FATAL:/
    error(message)
  end
end

#destroyObject

pgbackups:destroy BACKUP_ID

destroys a backup


176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/heroku/command/pgbackups.rb', line 176

def destroy
  unless name = shift_argument
    error("Usage: heroku pgbackups:destroy BACKUP_ID\nMust specify BACKUP_ID to destroy.")
  end
  backup = pgbackup_client.get_backup(name)
  if backup["destroyed_at"]
    error("Backup #{name} already destroyed.")
  end

  action("Destroying #{name}") do
    pgbackup_client.delete_backup(name)
  end
end

#indexObject

pgbackups

list captured backups


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
# File 'lib/heroku/command/pgbackups.rb', line 16

def index
  validate_arguments!

  backups = []
  pgbackup_client.get_transfers.each { |t|
    next unless backup_types.member?(t['to_name']) && !t['error_at'] && !t['destroyed_at']
    backups << {
      'id'          => backup_name(t['to_url']),
      'started_at'  => t['started_at'],
      'status'      => transfer_status(t),
      'size'        => t['size'],
      'database'    => t['from_name']
    }
  }

  if backups.empty?
    no_backups_error!
  else
    display_table(
      backups,
      %w{ id started_at status size database },
      ["ID", "Backup Time", "Status", "Size", "Database"]
    )
  end
end

#restoreObject

pgbackups:restore [<DATABASE> [BACKUP_ID|BACKUP_URL]]

restore a backup to a database

if no DATABASE is specified, defaults to DATABASE_URL and latest backup if DATABASE is specified, but no BACKUP_ID, defaults to latest backup


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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/heroku/command/pgbackups.rb', line 108

def restore
  if 0 == args.size
    attachment = resolve(nil, "DATABASE_URL")
    to_name = attachment.display_name
    to_url  = attachment.url
    backup_id = :latest
  elsif 1 == args.size
    attachment = resolve(shift_argument)
    to_name = attachment.display_name
    to_url  = attachment.url
    backup_id = :latest
  else
    attachment = resolve(shift_argument)
    to_name = attachment.display_name
    to_url  = attachment.url
    backup_id = shift_argument
  end

  if :latest == backup_id
    backup = pgbackup_client.get_latest_backup
    no_backups_error! if {} == backup
    to_uri = URI.parse backup["to_url"]
    backup_id = File.basename(to_uri.path, '.*')
    backup_id = "#{backup_id} (most recent)"
    from_url  = backup["to_url"]
    from_name = "BACKUP"
  elsif backup_id =~ /^http(s?):\/\//
    from_url  = backup_id
    from_name = "EXTERNAL_BACKUP"
    from_uri  = URI.parse backup_id
    backup_id = from_uri.path.empty? ? from_uri : File.basename(from_uri.path)
  else
    backup = pgbackup_client.get_backup(backup_id)
    abort("Backup #{backup_id} already destroyed.") if backup["destroyed_at"]

    from_url  = backup["to_url"]
    from_name = "BACKUP"
  end

  message = "#{to_name}  <---restore---  "
  padding = " " * message.length
  display "\n#{message}#{backup_id}"
  if backup
    display padding + "#{backup['from_name']}"
    display padding + "#{backup['created_at']}"
    display padding + "#{backup['size']}"
  end

  if confirm_command
    restore = transfer!(from_url, from_name, to_url, to_name)
    restore = poll_transfer!(restore)

    if restore["error_at"]
      message  =   "An error occurred and your restore did not finish."
      if restore['log'] =~ /Invalid dump format: .*: XML  document text/
        message += "\nThe backup url is invalid. Use `pgbackups:url` to generate a new temporary URL."
      else
        message += "\nPlease run `heroku logs --ps pgbackups` for details."
      end
      error(message)
    end
  end
end

#transferObject

pgbackups:transfer [SOURCE DATABASE] DESTINATION DATABASE

direct database-to-database transfer

If no DATABASE is specified, defaults to DATABASE_URL. The pgbackups add-on is required to use direct transfers

Example:

$ heroku pgbackups:transfer green teal –app example

note that both the FROM and TO database must be accessible to the pgbackups service $ heroku pgbackups:transfer DATABASE postgres://user:[email protected]/dbname –app example


204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/heroku/command/pgbackups.rb', line 204

def transfer
  db1 = shift_argument
  db2 = shift_argument

  if db1.nil?
    error("pgbackups:transfer requires at least one argument")
  end

  if db2.nil?
    db2 = db1
    db1 = "DATABASE_URL"
  end

  from = resolve_transfer(db1)
  to = resolve_transfer(db2)

  validate_arguments!

  if from.url == to.url
    error("source and target database are the same")
  end

  opts       = {}
  verify_app = to.app || app
  if confirm_command(verify_app, "WARNING: Destructive Action\nTransferring data from #{from.name} to #{to.name}")
    backup = transfer!(from.url, from.name, to.url, to.name, opts)
    backup = poll_transfer!(backup)

    if backup["error_at"]
      message  =   "An error occurred and your backup did not finish."
      message += "\nThe database is not yet online. Please try again." if backup['log'] =~ /Name or service not known/
      message += "\nThe database credentials are incorrect."           if backup['log'] =~ /psql: FATAL:/
      error(message)
    end
  end
end

#urlObject

pgbackups:url [BACKUP_ID]

get a temporary URL for a backup


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/heroku/command/pgbackups.rb', line 46

def url
  name = shift_argument
  validate_arguments!

  if name
    b = pgbackup_client.get_backup(name)
  else
    b = pgbackup_client.get_latest_backup
  end
  unless b['public_url']
    error("No backup found.")
  end
  if $stdout.isatty
    display '"'+b['public_url']+'"'
  else
    display b['public_url']
  end
end