nxt_heroku_database_backup 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +4 -4
- data/README.md +6 -0
- data/lib/nxt_heroku_database_backup/encrypter.rb +23 -0
- data/lib/nxt_heroku_database_backup/manager.rb +39 -33
- data/lib/nxt_heroku_database_backup/uploader/s3.rb +42 -0
- data/lib/nxt_heroku_database_backup/version.rb +1 -1
- data/lib/nxt_heroku_database_backup.rb +2 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 71e7bbf4fd7c3264955b48fb5b9a06310d77cfa23773a8e130995dd8fe3c4d81
|
4
|
+
data.tar.gz: fd9ff6113b374abad0fdb04e47873245483a6977d7e4290db17c2484979c1f84
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: feca013361b0dc1e08bdfdf9a99d86082030603dcac935b8acfa0b470459d2269e14943f1140ca587bf78d0ae1fbf990a5b88423456d91fe45fc2fa82ecb22cb
|
7
|
+
data.tar.gz: 43bbc8ff8499aeb744501fad509b8238108eb5dce0d794e75fb7d8029f53e62fabe4a85a181bb4ac362de746cb7aa7743fa4c0543bc215319f98da2a1463581e
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
nxt_heroku_database_backup (0.1.
|
4
|
+
nxt_heroku_database_backup (0.1.1)
|
5
5
|
aws-sdk-s3
|
6
6
|
nxt_init
|
7
7
|
|
@@ -15,8 +15,8 @@ GEM
|
|
15
15
|
tzinfo (~> 1.1)
|
16
16
|
zeitwerk (~> 2.1, >= 2.1.8)
|
17
17
|
aws-eventstream (1.0.3)
|
18
|
-
aws-partitions (1.
|
19
|
-
aws-sdk-core (3.
|
18
|
+
aws-partitions (1.219.0)
|
19
|
+
aws-sdk-core (3.68.0)
|
20
20
|
aws-eventstream (~> 1.0, >= 1.0.2)
|
21
21
|
aws-partitions (~> 1.0)
|
22
22
|
aws-sigv4 (~> 1.1)
|
@@ -38,7 +38,7 @@ GEM
|
|
38
38
|
concurrent-ruby (~> 1.0)
|
39
39
|
jmespath (1.4.0)
|
40
40
|
method_source (0.9.2)
|
41
|
-
minitest (5.12.
|
41
|
+
minitest (5.12.2)
|
42
42
|
nxt_init (0.1.5)
|
43
43
|
activesupport
|
44
44
|
pry (0.12.2)
|
data/README.md
CHANGED
@@ -2,6 +2,12 @@
|
|
2
2
|
|
3
3
|
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/nxt_heroku_database_backup`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
4
|
|
5
|
+
## TODO
|
6
|
+
|
7
|
+
- Gzip files before uploading
|
8
|
+
- Encrypt files before upload
|
9
|
+
- Add other cloud adaptors
|
10
|
+
|
5
11
|
## Installation
|
6
12
|
|
7
13
|
Add this line to your application's Gemfile:
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module NxtHerokuDatabaseBackup
|
2
|
+
class Encrypter
|
3
|
+
include NxtInit
|
4
|
+
|
5
|
+
attr_init :pass_phrase, :salt, :data
|
6
|
+
|
7
|
+
def call
|
8
|
+
encrypted = encrypter.update(data)
|
9
|
+
encrypted << encrypter.final
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def encrypter
|
15
|
+
@encrypter ||= begin
|
16
|
+
encrypter = OpenSSL::Cipher.new 'AES-128-CBC'
|
17
|
+
encrypter.encrypt
|
18
|
+
encrypter.pkcs5_keyivgen pass_phrase, salt
|
19
|
+
encrypter
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -1,36 +1,60 @@
|
|
1
1
|
require 'open-uri'
|
2
2
|
require 'aws-sdk-s3'
|
3
|
+
require 'openssl'
|
3
4
|
|
4
5
|
module NxtHerokuDatabaseBackup
|
5
6
|
class Manager
|
6
7
|
include NxtInit
|
7
8
|
|
8
|
-
attr_init
|
9
|
+
attr_init :pass_phrase,
|
10
|
+
:salt,
|
11
|
+
success_callback: -> { -> { true } },
|
9
12
|
environment: -> { default_environment },
|
10
13
|
backup_name: -> { default_backup_name },
|
11
|
-
backup_tmp_path: -> { default_backup_tmp_path }
|
14
|
+
backup_tmp_path: -> { default_backup_tmp_path },
|
15
|
+
uploader: -> { Uploader::S3.new(bucket_name: upload_destination, file_path: encrypted_backup_tmp_path) }
|
12
16
|
|
13
17
|
|
14
18
|
def backup_latest_dump
|
15
|
-
|
16
|
-
|
17
|
-
|
19
|
+
error = nil
|
20
|
+
download_backup
|
21
|
+
encrypt_backup
|
22
|
+
upload_backup
|
18
23
|
execute_success_callback
|
24
|
+
rescue StandardError => err
|
25
|
+
error = err
|
19
26
|
ensure
|
20
|
-
|
27
|
+
[backup_tmp_path, encrypted_backup_tmp_path].each do |path|
|
28
|
+
File.delete(path) if File.exist?(path)
|
29
|
+
end
|
30
|
+
|
31
|
+
raise error if error
|
21
32
|
end
|
22
33
|
|
23
34
|
private
|
24
35
|
|
25
|
-
def
|
26
|
-
|
36
|
+
def upload_backup
|
37
|
+
uploader.call
|
27
38
|
end
|
28
39
|
|
29
|
-
def
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
40
|
+
def encrypt_backup
|
41
|
+
NxtHerokuDatabaseBackup::Encrypter.new(
|
42
|
+
pass_phrase: pass_phrase,
|
43
|
+
salt: salt,
|
44
|
+
data: File.read(backup_tmp_path)
|
45
|
+
).call
|
46
|
+
end
|
47
|
+
|
48
|
+
def download_backup
|
49
|
+
IO.copy_stream(open(database_backup_url), backup_tmp_path)
|
50
|
+
end
|
51
|
+
|
52
|
+
def encrypted_backup_tmp_path
|
53
|
+
"#{backup_tmp_path}.gpg"
|
54
|
+
end
|
55
|
+
|
56
|
+
def execute_success_callback
|
57
|
+
success_callback.call
|
34
58
|
end
|
35
59
|
|
36
60
|
def default_backup_name
|
@@ -55,30 +79,12 @@ module NxtHerokuDatabaseBackup
|
|
55
79
|
@heroku_app_name ||= ENV.fetch('HEROKU_APP_NAME')
|
56
80
|
end
|
57
81
|
|
58
|
-
def
|
59
|
-
|
60
|
-
rescue Aws::S3::Errors::NotFound
|
61
|
-
s3.create_bucket(bucket: bucket_name)
|
62
|
-
end
|
63
|
-
|
64
|
-
def bucket_name
|
65
|
-
@bucket_name ||= "#{heroku_app_name}-db-backups"
|
82
|
+
def upload_destination
|
83
|
+
@upload_destination ||= "#{heroku_app_name}-db-backups"
|
66
84
|
end
|
67
85
|
|
68
86
|
def default_environment
|
69
87
|
Object.const_defined?('HerokuEnv') && HerokuEnv.env || 'development'
|
70
88
|
end
|
71
|
-
|
72
|
-
def s3
|
73
|
-
@s3 ||= Aws::S3::Client.new(**aws_credentials)
|
74
|
-
end
|
75
|
-
|
76
|
-
def aws_credentials
|
77
|
-
{
|
78
|
-
access_key_id: ENV.fetch('DATABASE_BACKUP_AWS_ACCESS_KEY_ID'),
|
79
|
-
secret_access_key: ENV.fetch('DATABASE_BACKUP_AWS_SECRET_ACCESS_KEY'),
|
80
|
-
region: ENV.fetch('DATABASE_BACKUP_AWS_REGION')
|
81
|
-
}
|
82
|
-
end
|
83
89
|
end
|
84
90
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module NxtHerokuDatabaseBackup
|
2
|
+
module Uploader
|
3
|
+
class S3
|
4
|
+
include NxtInit
|
5
|
+
|
6
|
+
attr_init :bucket_name, :file_path
|
7
|
+
|
8
|
+
def call
|
9
|
+
aws_object.upload_file(file_path)
|
10
|
+
end
|
11
|
+
|
12
|
+
def aws_object
|
13
|
+
@aws_object ||= begin
|
14
|
+
ensure_bucket_exists
|
15
|
+
Aws::S3::Resource.new(aws_credentials).bucket(bucket_name).object(backup_name)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def ensure_bucket_exists
|
20
|
+
s3.head_bucket(bucket: bucket_name)
|
21
|
+
rescue Aws::S3::Errors::NotFound
|
22
|
+
s3.create_bucket(bucket: bucket_name)
|
23
|
+
end
|
24
|
+
|
25
|
+
def bucket_name
|
26
|
+
@bucket_name ||= "#{heroku_app_name}-db-backups"
|
27
|
+
end
|
28
|
+
|
29
|
+
def client
|
30
|
+
@client ||= Aws::S3::Client.new(**aws_credentials)
|
31
|
+
end
|
32
|
+
|
33
|
+
def aws_credentials
|
34
|
+
{
|
35
|
+
access_key_id: ENV.fetch('DATABASE_BACKUP_AWS_ACCESS_KEY_ID'),
|
36
|
+
secret_access_key: ENV.fetch('DATABASE_BACKUP_AWS_SECRET_ACCESS_KEY'),
|
37
|
+
region: ENV.fetch('DATABASE_BACKUP_AWS_REGION')
|
38
|
+
}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nxt_heroku_database_backup
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andreas Robecke
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-09-
|
11
|
+
date: 2019-09-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk-s3
|
@@ -127,7 +127,9 @@ files:
|
|
127
127
|
- bin/console
|
128
128
|
- bin/setup
|
129
129
|
- lib/nxt_heroku_database_backup.rb
|
130
|
+
- lib/nxt_heroku_database_backup/encrypter.rb
|
130
131
|
- lib/nxt_heroku_database_backup/manager.rb
|
132
|
+
- lib/nxt_heroku_database_backup/uploader/s3.rb
|
131
133
|
- lib/nxt_heroku_database_backup/version.rb
|
132
134
|
- nxt_heroku_database_backup.gemspec
|
133
135
|
homepage: https://www.getsafe.de
|