Module: PgDrive

Defined in:
lib/pg_drive.rb,
lib/pg_drive/dump.rb,
lib/pg_drive/version.rb,
lib/pg_drive/uploader.rb

Defined Under Namespace

Modules: Dump, Uploader

Constant Summary collapse

InvalidEnvironment =
Class.new(StandardError)
BackupFailed =
Class.new(StandardError)
OOB_URI =
"urn:ietf:wg:oauth:2.0:oob".freeze
DEFAULT_BACKUP_TIMEOUT_SECONDS =

5 minutes

60 * 5
MISSING_CRED_WARNING =
"Please use the run #{self}.setup_credentials"\
" from console to set up credentials".freeze
CREDENTIALS_INTRO =
"Please open your browser and go to the following url."\
"Login with the user you wish to use as target for backup".freeze
CREDENTIALS_ENV_INSTRUCTIONS =
"Please set the following line as the value of "\
'"PG_DRIVE_CREDENTIALS" key in the environment hash:'.freeze
BACKUP_CMD =
"pg_dump -c -C -b".freeze
PG_ENV_MAP =
{
  "PGPASSWORD" => "password",
  "PGDATABASE" => "database",
  "PGHOST" => "host",
  "PGPORT" => "port",
  "PGUSER" => "username",
}.freeze
GZIP_MIME_TYPE =
"application/x-gzip".freeze
VERSION =
"0.1.0".freeze

Class Method Summary collapse

Class Method Details

.authorizerObject



44
45
46
47
48
49
50
# File 'lib/pg_drive.rb', line 44

def authorizer
  Google::Auth::UserAuthorizer.new(
    Uploader.client_id,
    Uploader::AUTH_SCOPE,
    nil
  )
end

.performObject



31
32
33
# File 'lib/pg_drive.rb', line 31

def perform
  Uploader.call(Dump.call)
end

.refresh_token_from_code(code) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/pg_drive.rb', line 52

def refresh_token_from_code(code)
  authorizer.get_credentials_from_code(
    user_id: :owner,
    code: code,
    base_url: OOB_URI
  )
end

.setup_credentialsObject



35
36
37
38
39
40
41
42
# File 'lib/pg_drive.rb', line 35

def setup_credentials
  puts CREDENTIALS_INTRO
  puts authorizer.get_authorization_url(base_url: OOB_URI)
  puts "Please enter the token you receive for further instructions"
  code = gets
  puts CREDENTIALS_ENV_INSTRUCTIONS
  puts refresh_token_from_code(code)
end