pg_drive 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ab3ae3369f6b249ed79922616d3e5899e76cf2be
4
+ data.tar.gz: 91f66db35af2c2c69187d6d27fd78f46b9ee4249
5
+ SHA512:
6
+ metadata.gz: e14d16c331a36ba42287b08edbab61dc28b068e4e5cb3b6fb6cd7cc1535a61cef1bd9e98e006312059fac13403865e5054613b3729926c10a7589913c8336b43
7
+ data.tar.gz: 996c3384f8e6bf9bc05829b41a84fade7719cfd4a3358ac627485a0318ac6a0fdc749d14f1148946aecb5b87a3bc16a8cda05158929de0bfab992d1a45935440
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,32 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+ Exclude:
4
+ - 'db/**/*'
5
+ - 'config/**/*'
6
+ - 'vendor/**/*'
7
+ - 'bin/**/*'
8
+ Style/StringLiterals:
9
+ EnforcedStyle: double_quotes
10
+ SupportedStyles:
11
+ - single_quotes
12
+ - double_quotes
13
+
14
+ Style/StringLiteralsInInterpolation:
15
+ EnforcedStyle: double_quotes
16
+ SupportedStyles:
17
+ - single_quotes
18
+ - double_quotes
19
+ Metrics/LineLength:
20
+ Max: 90
21
+ Documentation:
22
+ Enabled: false
23
+ Style/DoubleNegation:
24
+ Enabled: false
25
+ Style/MultilineBlockChain:
26
+ Enabled: false
27
+ Style/FrozenStringLiteralComment:
28
+ Enabled: false
29
+ TrailingCommaInLiteral:
30
+ EnforcedStyleForMultiline: comma
31
+ Style/IfUnlessModifier:
32
+ Enabled: false
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ pg_drive
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.3.1
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in pg_drive.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Gal Tsubery
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # PgDrive
2
+
3
+ This library is intended to be a minimalist backup solution for Postgres, Rails and Goodle Drive.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'pg_drive'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install pg_drive
20
+
21
+ You need to (https://console.developers.google.com)[create] 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
+ Set the following environment variables:
23
+ ```
24
+ ENV['PG_DRIVE_GOOGLE_KEY']="Your credential client id"
25
+ ENV['PG_DRIVE_GOOGLE_SECRET']="Your credential secret"
26
+ ```
27
+ These values should be easy to find after you created new credentials.
28
+
29
+ After that you should run the following command inside rails console and follow the instructions.
30
+ ```ruby
31
+ PgDrive.setup_credentials
32
+ ```
33
+ This process is will generate a new token so the app could use a specific user's google drive account to store the backup files.
34
+
35
+ ## Usage
36
+
37
+ ```ruby
38
+ PgDrive.perform
39
+ ```
40
+ Creates a new backup and uploads it to google drive.
41
+ You can run this command in your favorite queueing system and favorite scheduler.
42
+
43
+ ## Notes
44
+ Keep in mind the backup dump and zipping are all done in memory. If your database is more than 50mb you should find a better solution.
45
+
46
+ ## Contributing
47
+
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tsubery/pg_drive.
49
+
50
+
51
+ ## License
52
+
53
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
54
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pg_drive"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,47 @@
1
+ module PgDrive
2
+ module Dump
3
+ class << self
4
+ def call
5
+ stdin, out_and_error, wait_thr = exec_pg_dump
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(
28
+ pg_env,
29
+ BACKUP_CMD,
30
+ pgroup: true
31
+ )
32
+ end
33
+
34
+ def pg_env
35
+ PG_ENV_MAP.map { |k, v| [k, db_conf[v].to_s] }.to_h
36
+ end
37
+
38
+ def db_conf
39
+ @db_conf ||= Rails.configuration.database_configuration[Rails.env]
40
+ end
41
+
42
+ def backup_timeout_seconds
43
+ DEFAULT_BACKUP_TIMEOUT_SECONDS
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,53 @@
1
+ module PgDrive
2
+ module Uploader
3
+ Drive = Google::Apis::DriveV2
4
+ AUTH_SCOPE = "https://www.googleapis.com/auth/drive".freeze
5
+ RETRY_COUNT = 3
6
+
7
+ class << self
8
+ def call(content)
9
+ drive = Drive::DriveService.new
10
+ drive.authorization = credentials
11
+ drive.insert_file(
12
+ Drive::File.new(title: "backup-#{Time.now.to_i}"),
13
+ upload_source: gzip(content),
14
+ content_type: GZIP_MIME_TYPE,
15
+ options: { retries: RETRY_COUNT }
16
+ )
17
+ end
18
+
19
+ def gzip(string)
20
+ gzipped_io = StringIO.new
21
+ writer = Zlib::GzipWriter.new(gzipped_io)
22
+ writer.write(string)
23
+ writer.close
24
+ StringIO.new(gzipped_io.string)
25
+ end
26
+
27
+ def client_id
28
+ Google::Auth::ClientId.new(google_key, google_secret)
29
+ end
30
+
31
+ def google_key
32
+ ENV.fetch("PG_DRIVE_GOOGLE_KEY")
33
+ end
34
+
35
+ def google_secret
36
+ ENV.fetch("PG_DRIVE_GOOGLE_SECRET")
37
+ end
38
+
39
+ def credentials
40
+ refresh_token = ENV["PG_DRIVE_CREDENTIALS"]
41
+ if refresh_token.nil? || refresh_token.empty?
42
+ raise InvalidEnvironment, MISSING_CRED_WARNING
43
+ end
44
+ Google::Auth::UserRefreshCredentials.new(
45
+ client_id: google_key,
46
+ client_secret: google_secret,
47
+ refresh_token: refresh_token,
48
+ scope: AUTH_SCOPE
49
+ )
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module PgDrive
2
+ VERSION = "0.1.0".freeze
3
+ end
data/lib/pg_drive.rb ADDED
@@ -0,0 +1,60 @@
1
+ require "pg_drive/version"
2
+ require "google/apis/drive_v2"
3
+ require "googleauth"
4
+ require "pg_drive/uploader"
5
+ require "pg_drive/dump"
6
+
7
+ module PgDrive
8
+ InvalidEnvironment = Class.new(StandardError)
9
+ BackupFailed = Class.new(StandardError)
10
+
11
+ OOB_URI = "urn:ietf:wg:oauth:2.0:oob".freeze
12
+ DEFAULT_BACKUP_TIMEOUT_SECONDS = 60 * 5 # 5 minutes
13
+ MISSING_CRED_WARNING = "Please use the run #{self}.setup_credentials"\
14
+ " from console to set up credentials".freeze
15
+ CREDENTIALS_INTRO = "Please open your browser and go to the following url."\
16
+ "Login with the user you wish to use as target for backup".freeze
17
+ CREDENTIALS_ENV_INSTRUCTIONS = "Please set the following line as the value of "\
18
+ '"PG_DRIVE_CREDENTIALS" key in the environment hash:'.freeze
19
+
20
+ BACKUP_CMD = "pg_dump -c -C -b".freeze
21
+ PG_ENV_MAP = {
22
+ "PGPASSWORD" => "password",
23
+ "PGDATABASE" => "database",
24
+ "PGHOST" => "host",
25
+ "PGPORT" => "port",
26
+ "PGUSER" => "username",
27
+ }.freeze
28
+ GZIP_MIME_TYPE = "application/x-gzip".freeze
29
+
30
+ class << self
31
+ def perform
32
+ Uploader.call(Dump.call)
33
+ end
34
+
35
+ def setup_credentials
36
+ puts CREDENTIALS_INTRO
37
+ puts authorizer.get_authorization_url(base_url: OOB_URI)
38
+ puts "Please enter the token you receive for further instructions"
39
+ code = gets
40
+ puts CREDENTIALS_ENV_INSTRUCTIONS
41
+ puts refresh_token_from_code(code)
42
+ end
43
+
44
+ def authorizer
45
+ Google::Auth::UserAuthorizer.new(
46
+ Uploader.client_id,
47
+ Uploader::AUTH_SCOPE,
48
+ nil
49
+ )
50
+ end
51
+
52
+ def refresh_token_from_code(code)
53
+ authorizer.get_credentials_from_code(
54
+ user_id: :owner,
55
+ code: code,
56
+ base_url: OOB_URI
57
+ )
58
+ end
59
+ end
60
+ end
data/pg_drive.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "pg_drive/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pg_drive"
8
+ spec.version = PgDrive::VERSION
9
+ spec.authors = ["Gal Tsubery"]
10
+ spec.email = ["gal.tsubery@gmail.com"]
11
+
12
+ spec.summary = "Backup rails postgres to google drive"
13
+ spec.description = "This gem backs up postgres and stores the backup in google drive"
14
+ spec.homepage = "http://github.com/tsubery/pg_drive/"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`
18
+ .split("\x0")
19
+ .reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.12"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "rubocop"
28
+ spec.add_development_dependency "pry"
29
+ spec.add_dependency "google-api-client", "0.9"
30
+ spec.add_dependency "pg" # Just to make sure we are using postgres
31
+ spec.add_dependency "rails", [">4.0.0", "<6.0.0"]
32
+ # Just to make sure we are using postgres
33
+ end
metadata ADDED
@@ -0,0 +1,179 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg_drive
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gal Tsubery
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: google-api-client
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: '0.9'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: '0.9'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pg
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rails
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">"
116
+ - !ruby/object:Gem::Version
117
+ version: 4.0.0
118
+ - - "<"
119
+ - !ruby/object:Gem::Version
120
+ version: 6.0.0
121
+ type: :runtime
122
+ prerelease: false
123
+ version_requirements: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">"
126
+ - !ruby/object:Gem::Version
127
+ version: 4.0.0
128
+ - - "<"
129
+ - !ruby/object:Gem::Version
130
+ version: 6.0.0
131
+ description: This gem backs up postgres and stores the backup in google drive
132
+ email:
133
+ - gal.tsubery@gmail.com
134
+ executables: []
135
+ extensions: []
136
+ extra_rdoc_files: []
137
+ files:
138
+ - ".gitignore"
139
+ - ".rspec"
140
+ - ".rubocop.yml"
141
+ - ".ruby-gemset"
142
+ - ".ruby-version"
143
+ - ".travis.yml"
144
+ - Gemfile
145
+ - LICENSE.txt
146
+ - README.md
147
+ - Rakefile
148
+ - bin/console
149
+ - bin/setup
150
+ - lib/pg_drive.rb
151
+ - lib/pg_drive/dump.rb
152
+ - lib/pg_drive/uploader.rb
153
+ - lib/pg_drive/version.rb
154
+ - pg_drive.gemspec
155
+ homepage: http://github.com/tsubery/pg_drive/
156
+ licenses:
157
+ - MIT
158
+ metadata: {}
159
+ post_install_message:
160
+ rdoc_options: []
161
+ require_paths:
162
+ - lib
163
+ required_ruby_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ required_rubygems_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ requirements: []
174
+ rubyforge_project:
175
+ rubygems_version: 2.5.1
176
+ signing_key:
177
+ specification_version: 4
178
+ summary: Backup rails postgres to google drive
179
+ test_files: []