This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
forked from will/pgcmd
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
241 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
$LOAD_PATH.unshift(File.dirname(__FILE__)) | ||
require 'addons' | ||
require 'pgresolver' | ||
require 'pg' | ||
require 'client' | ||
require 'pgbackups' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
require "heroku/commands/help" | ||
require "heroku/pgutils" | ||
require "pgbackups/client" | ||
|
||
module Heroku::Command | ||
class Pgbackups < BaseWithApp | ||
include PGResolver | ||
|
||
Heroku::Command::Help.group("pgbackups") do |group| | ||
group.command "pgbackups:capture [--db <DB_ID>]", "capture a backup from database ID (default: DATABASE_URL)" | ||
end | ||
|
||
def capture | ||
db = resolve_db(:allow_default => true) | ||
|
||
from_url = db[:url] | ||
from_name = db[:name] | ||
to_url = nil # server will assign | ||
to_name = "BACKUP" | ||
opts = {:expire => extract_option("--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#{db[:pretty_name]} ----backup---> #{backup_id}" | ||
|
||
backup = poll_transfer!(backup) | ||
|
||
if backup["error_at"] | ||
message = " ! An error occurred and your backup did not finish." | ||
message += "\n ! The database is not yet online. Please try again." if backup['log'] =~ /Name or service not known/ | ||
message += "\n ! The database credentials are incorrect." if backup['log'] =~ /psql: FATAL:/ | ||
abort(message) | ||
end | ||
|
||
end | ||
|
||
def restore | ||
db = resolve_db(:allow_default => true) | ||
to_name = db[:name] | ||
to_url = db[:url] | ||
|
||
backup_id = args.shift | ||
|
||
if 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 | ||
if backup_id | ||
backup = pgbackup_client.get_backup(backup_id) | ||
abort("Backup #{backup_id} already deleted.") if backup["destroyed_at"] | ||
else | ||
backup = pgbackup_client.get_latest_backup | ||
to_uri = URI.parse backup["to_url"] | ||
backup_id = File.basename(to_uri.path, '.*') | ||
backup_id = "#{backup_id} (most recent)" | ||
end | ||
|
||
from_url = backup["to_url"] | ||
from_name = "BACKUP" | ||
end | ||
|
||
message = "#{db[:pretty_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." | ||
message += "\n ! The backup url is invalid. Use `pgbackups:url` to generate a new temporary URL." if restore['log'] =~ /Invalid dump format: .*: XML document text/ | ||
abort(message) | ||
end | ||
end | ||
end | ||
|
||
end | ||
end |
Oops, something went wrong.