lib/heroku/command/pgbackups.rb
# 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
#
restore [ [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
restore [ [ 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
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
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
pgbackups:restore [<DATABASE> [BACKUP_ID|BACKUP_URL]]
restore a backup to a database
pgbackups : restore [ <DATABASE > [ BACKUP_ID|BACKUP_URL ]] restore a backup to a database