pg_drive 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +3 -3
- data/lib/pg_drive.rb +10 -16
- data/lib/pg_drive/dump.rb +14 -27
- data/lib/pg_drive/uploader.rb +10 -10
- data/lib/pg_drive/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad9049fe5eeefab5f1b32cca17b6863feb9a8d12
|
4
|
+
data.tar.gz: 56bc5de109fded551223a9a7cc92ae2aae98caaa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f006f4d57614aa7136dbd6c7715e935c8d505ca18ca6f577435795ab786000b55c2501cc547dd82c45ac4eca128d0b6f5d64fbccf0b85084e61fe31f27d07fc
|
7
|
+
data.tar.gz: 28450a1ebcad83158ae19212a63584335a331eae57fa338d76593de95e8385c8fbf88f98e338856141e6ea23fdae781f8b4215dcb49536dedd24d030bfbf73d3
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# PgDrive
|
2
2
|
|
3
|
-
This library is intended to be a minimalist backup solution for Postgres, Rails and Goodle Drive.
|
3
|
+
This library is intended to be a minimalist backup solution for Postgres, Rails and Goodle Drive. Basically a free aletrnative to [dumper](https://dumper.io).
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -18,13 +18,13 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
$ gem install pg_drive
|
20
20
|
|
21
|
-
You need to (https://console.developers.google.com)
|
21
|
+
You need to [create](https://console.developers.google.com) a new project and credential in order to use google drive. Make sure the credential is for "Oauth Client" and the application type is "Other".
|
22
22
|
Set the following environment variables:
|
23
23
|
```
|
24
24
|
ENV['PG_DRIVE_GOOGLE_KEY']="Your credential client id"
|
25
25
|
ENV['PG_DRIVE_GOOGLE_SECRET']="Your credential secret"
|
26
26
|
```
|
27
|
-
These values should be easy to find after you created new credentials.
|
27
|
+
These values should be easy to find after you created new credentials in google's developer console.
|
28
28
|
|
29
29
|
After that you should run the following command inside rails console and follow the instructions.
|
30
30
|
```ruby
|
data/lib/pg_drive.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require "pg_drive/version"
|
2
|
-
require
|
2
|
+
require "open3"
|
3
3
|
require "google/apis/drive_v2"
|
4
4
|
require "googleauth"
|
5
5
|
require "pg_drive/uploader"
|
@@ -13,12 +13,12 @@ module PgDrive
|
|
13
13
|
DEFAULT_BACKUP_TIMEOUT_SECONDS = 60 * 5 # 5 minutes
|
14
14
|
MISSING_CRED_WARNING = "Please use the run #{self}.setup_credentials"\
|
15
15
|
" from console to set up credentials".freeze
|
16
|
-
CREDENTIALS_INTRO = "Please open your browser and go to the following url
|
16
|
+
CREDENTIALS_INTRO = "Please open your browser and go to the following url:"\
|
17
17
|
"Login with the user you wish to use as target for backup".freeze
|
18
18
|
CREDENTIALS_ENV_INSTRUCTIONS = "Please set the following line as the value of "\
|
19
|
-
'"PG_DRIVE_CREDENTIALS" key
|
19
|
+
'"PG_DRIVE_CREDENTIALS" key the environment:'.freeze
|
20
20
|
|
21
|
-
BACKUP_CMD = "pg_dump -c -C -b".freeze
|
21
|
+
BACKUP_CMD = "pg_dump -Fc -c -C -b".freeze
|
22
22
|
PG_ENV_MAP = {
|
23
23
|
"PGPASSWORD" => "password",
|
24
24
|
"PGDATABASE" => "database",
|
@@ -26,32 +26,26 @@ module PgDrive
|
|
26
26
|
"PGPORT" => "port",
|
27
27
|
"PGUSER" => "username",
|
28
28
|
}.freeze
|
29
|
-
|
29
|
+
BINARY_MIME_TYPE = "application/octet-stream".freeze
|
30
30
|
|
31
31
|
class << self
|
32
32
|
def perform
|
33
|
-
|
33
|
+
Dump.call do |pipe|
|
34
|
+
Uploader.call(pipe)
|
35
|
+
end
|
34
36
|
end
|
35
37
|
|
36
38
|
def setup_credentials
|
37
39
|
puts CREDENTIALS_INTRO
|
38
|
-
puts authorizer.get_authorization_url(base_url: OOB_URI)
|
40
|
+
puts Uploader.authorizer.get_authorization_url(base_url: OOB_URI)
|
39
41
|
puts "Please enter the token you receive for further instructions"
|
40
42
|
code = gets
|
41
43
|
puts CREDENTIALS_ENV_INSTRUCTIONS
|
42
44
|
puts refresh_token_from_code(code)
|
43
45
|
end
|
44
46
|
|
45
|
-
def authorizer
|
46
|
-
Google::Auth::UserAuthorizer.new(
|
47
|
-
Uploader.client_id,
|
48
|
-
Uploader::AUTH_SCOPE,
|
49
|
-
nil
|
50
|
-
)
|
51
|
-
end
|
52
|
-
|
53
47
|
def refresh_token_from_code(code)
|
54
|
-
authorizer.get_credentials_from_code(
|
48
|
+
Uploader.authorizer.get_credentials_from_code(
|
55
49
|
user_id: :owner,
|
56
50
|
code: code,
|
57
51
|
base_url: OOB_URI
|
data/lib/pg_drive/dump.rb
CHANGED
@@ -2,33 +2,24 @@ module PgDrive
|
|
2
2
|
module Dump
|
3
3
|
class << self
|
4
4
|
def call
|
5
|
-
stdin,
|
6
|
-
read_with_timeout(out_and_error, wait_thr)
|
7
|
-
ensure
|
8
|
-
stdin&.close
|
9
|
-
out_and_error&.close
|
10
|
-
end
|
11
|
-
|
12
|
-
def read_with_timeout(input, wait_thr)
|
13
|
-
result = Timeout.timeout(backup_timeout_seconds) { input.read }
|
14
|
-
|
15
|
-
unless wait_thr.value.success? || result.blank?
|
16
|
-
raise BackupFailed, "Exit status: #{wait_thr.value.exitstatus}: #{result}"
|
17
|
-
end
|
18
|
-
result
|
19
|
-
rescue Timeout::Error
|
20
|
-
if (kill_pid = wait_thr[:pid])
|
21
|
-
Process.kill(9, Process.getpgid(kill_pid))
|
22
|
-
end
|
23
|
-
raise BackupFailed, "Timeout error for backup_command #{kill_pid}"
|
24
|
-
end
|
25
|
-
|
26
|
-
def exec_pg_dump
|
27
|
-
Open3.popen2e(
|
5
|
+
stdin, stdout, stderr, wait_thr = Open3.popen3(
|
28
6
|
pg_env,
|
29
7
|
BACKUP_CMD,
|
30
8
|
pgroup: true
|
31
9
|
)
|
10
|
+
|
11
|
+
tmpfile = Tempfile.new("pg_backup")
|
12
|
+
tmpfile.write(stdout)
|
13
|
+
tmpfile.rewind
|
14
|
+
|
15
|
+
errors = stderr.read
|
16
|
+
unless wait_thr.value.success? && errors.empty?
|
17
|
+
raise BackupFailed, "Exit status: #{wait_thr.value.exitstatus}: #{errors}"
|
18
|
+
end
|
19
|
+
|
20
|
+
yield tmpfile
|
21
|
+
ensure
|
22
|
+
[stdin, stdout, stderr, tmpfile].compact.each(&:close)
|
32
23
|
end
|
33
24
|
|
34
25
|
def pg_env
|
@@ -38,10 +29,6 @@ module PgDrive
|
|
38
29
|
def db_conf
|
39
30
|
@db_conf ||= Rails.configuration.database_configuration[Rails.env]
|
40
31
|
end
|
41
|
-
|
42
|
-
def backup_timeout_seconds
|
43
|
-
DEFAULT_BACKUP_TIMEOUT_SECONDS
|
44
|
-
end
|
45
32
|
end
|
46
33
|
end
|
47
34
|
end
|
data/lib/pg_drive/uploader.rb
CHANGED
@@ -5,24 +5,24 @@ module PgDrive
|
|
5
5
|
RETRY_COUNT = 3
|
6
6
|
|
7
7
|
class << self
|
8
|
-
def call(
|
8
|
+
def call(pipe)
|
9
9
|
drive = Drive::DriveService.new
|
10
10
|
drive.authorization = credentials
|
11
11
|
app_name = Rails.application.class.parent_name
|
12
12
|
drive.insert_file(
|
13
|
-
Drive::File.new(title: "
|
14
|
-
upload_source:
|
15
|
-
content_type:
|
13
|
+
Drive::File.new(title: "#{app_name}-#{Time.now.utc.iso8601}.dmp"),
|
14
|
+
upload_source: pipe,
|
15
|
+
content_type: BINARY_MIME_TYPE,
|
16
16
|
options: { retries: RETRY_COUNT }
|
17
17
|
)
|
18
18
|
end
|
19
19
|
|
20
|
-
def
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
20
|
+
def authorizer
|
21
|
+
Google::Auth::UserAuthorizer.new(
|
22
|
+
Uploader.client_id,
|
23
|
+
Uploader::AUTH_SCOPE,
|
24
|
+
nil
|
25
|
+
)
|
26
26
|
end
|
27
27
|
|
28
28
|
def client_id
|
data/lib/pg_drive/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pg_drive
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gal Tsubery
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|