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

Instance Attribute Summary

Attributes inherited from Base

#args, #options

Instance Method Summary collapse

Methods included from Helpers::HerokuPostgresql

#app_attachments, #app_config_vars, #find_database_url_real_attachment, #forget_config!, #hpg_addon_name, #hpg_databases, #hpg_resolve, #hpg_translate_fork_and_follow, #match_attachments_by_name, #resource_url

Methods included from Helpers

#action, #ask, #confirm, #confirm_billing, #confirm_command, #create_git_remote, #deprecate, #display, #display_header, #display_object, #display_row, #display_table, #error, 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?, #home_directory, #host_name, #hprint, #hputs, included, included_into, #json_decode, #json_encode, #launchy, #line_formatter, #longest, #output_with_bang, #quantify, #redisplay, #retry_on_exception, #run_command, #running_on_a_mac?, #running_on_windows?, #set_buffer, #shell, #spinner, #status, #string_distance, #styled_array, #styled_error, #styled_hash, #styled_header, #suggestion, #time_ago, #truncate, #with_tty

Methods inherited from Base

#api, #app, #heroku, #initialize, namespace

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 = hpg_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 `pogo 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: pogo 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']),
      'created_at'  => t['created_at'],
      'status'      => transfer_status(t),
      'size'        => t['size'],
      'database'    => t['from_name']
    }
  }

  if backups.empty?
    no_backups_error!
  else
    display_table(
      backups,
      %w{ id created_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 = hpg_resolve(nil, "DATABASE_URL")
    to_name = attachment.display_name
    to_url  = attachment.url
    backup_id = :latest
  elsif 1 == args.size
    attachment = hpg_resolve(shift_argument)
    to_name = attachment.display_name
    to_url  = attachment.url
    backup_id = :latest
  else
    attachment = hpg_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 `pogo logs --ps pgbackups` for details."
      end
      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