pgchief 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +2 -2
- data/lib/pgchief/command/database_restore.rb +58 -0
- data/lib/pgchief/command/s3_upload.rb +0 -1
- data/lib/pgchief/database/backups.rb +56 -0
- data/lib/pgchief/database.rb +4 -0
- data/lib/pgchief/prompt/backup_database.rb +1 -1
- data/lib/pgchief/prompt/database_management.rb +2 -1
- data/lib/pgchief/prompt/restore_database.rb +16 -0
- data/lib/pgchief/version.rb +1 -1
- data/lib/pgchief.rb +5 -2
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: adccab6ee77468761bf0d05d12954f7b0360547b95f5295797b911ed80f731e2
|
4
|
+
data.tar.gz: 2a4ae39f43e0da9913f252e70e0583d0e21dd45e48931e94a62c309298d287f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7dea359e077b5c473c7df21e53067954dee0c426df76b8a375a026a3976593dbdd4024c6582650a2fbe1404a34affb1030fe0065842065ad594119cd3f757ace
|
7
|
+
data.tar.gz: 3b16494cea09736f6b2c16e55d2c5054898a6f023bf06b8862585313617104dc858e98750397baf6b606126aaf2f5bb109f9f02fbf11699b281bdeefd6c41c38
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -132,7 +132,7 @@ Give "rando-username" access to database(s):
|
|
132
132
|
* [x] Display connection information
|
133
133
|
* [x] Back up database locally
|
134
134
|
* [x] Back up database to S3
|
135
|
-
* [
|
136
|
-
* [
|
135
|
+
* [x] Restore local database
|
136
|
+
* [x] Restore remote database @ S3
|
137
137
|
* [ ] Quickly back up via command line option
|
138
138
|
* [ ] Quickly restore via command line option
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "forwardable"
|
4
|
+
|
5
|
+
module Pgchief
|
6
|
+
module Command
|
7
|
+
# Command object to restore a database
|
8
|
+
class DatabaseRestore < Base
|
9
|
+
extend Forwardable
|
10
|
+
|
11
|
+
attr_reader :database, :filename
|
12
|
+
|
13
|
+
def_delegators :s3, :configured?, :client, :bucket, :path
|
14
|
+
|
15
|
+
def call
|
16
|
+
@database = params.first
|
17
|
+
@filename = params.last
|
18
|
+
raise Pgchief::Errors::DatabaseMissingError unless db_exists?
|
19
|
+
|
20
|
+
download! if configured?
|
21
|
+
restore!
|
22
|
+
|
23
|
+
"Database '#{database}' restored from #{filename}"
|
24
|
+
rescue PG::Error => e
|
25
|
+
"Error: #{e.message}"
|
26
|
+
ensure
|
27
|
+
conn.close
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def download!
|
33
|
+
client.get_object(
|
34
|
+
bucket: bucket,
|
35
|
+
key: "#{path}#{filename}",
|
36
|
+
response_target: local_location
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
40
|
+
def restore!
|
41
|
+
`pg_restore --clean --no-owner --dbname=#{Pgchief::Config.pgurl}/#{database} #{local_location}`
|
42
|
+
end
|
43
|
+
|
44
|
+
def db_exists?
|
45
|
+
query = "SELECT 1 FROM pg_database WHERE datname = '#{database}'"
|
46
|
+
conn.exec(query).any?
|
47
|
+
end
|
48
|
+
|
49
|
+
def local_location
|
50
|
+
"#{Pgchief::Config.backup_dir}/#{filename}"
|
51
|
+
end
|
52
|
+
|
53
|
+
def s3
|
54
|
+
Pgchief::Config.s3
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "forwardable"
|
4
|
+
require "pry"
|
5
|
+
|
6
|
+
module Pgchief
|
7
|
+
class Database
|
8
|
+
# Get a list of all backups for a given database
|
9
|
+
class Backups
|
10
|
+
extend Forwardable
|
11
|
+
|
12
|
+
def_delegators :s3, :bucket, :path, :client
|
13
|
+
|
14
|
+
def self.for(database, remote)
|
15
|
+
new(database, remote).for
|
16
|
+
end
|
17
|
+
|
18
|
+
attr_reader :database, :remote
|
19
|
+
|
20
|
+
def initialize(database, remote)
|
21
|
+
@database = database
|
22
|
+
@remote = remote
|
23
|
+
end
|
24
|
+
|
25
|
+
def for
|
26
|
+
remote ? remote_backups : local_backups
|
27
|
+
end
|
28
|
+
|
29
|
+
def remote_backups
|
30
|
+
@remote_backups ||= client.list_objects(
|
31
|
+
bucket: bucket,
|
32
|
+
prefix: "#{path}#{database}-"
|
33
|
+
).contents
|
34
|
+
.map(&:key)
|
35
|
+
.sort
|
36
|
+
.last(3)
|
37
|
+
.reverse
|
38
|
+
.map { |f| File.basename(f) }
|
39
|
+
end
|
40
|
+
|
41
|
+
def local_backups
|
42
|
+
Dir["#{Pgchief::Config.backup_dir}#{database}-*.dump"]
|
43
|
+
.sort_by { |f| File.mtime(f) }
|
44
|
+
.reverse
|
45
|
+
.last(3)
|
46
|
+
.map { |f| File.basename(f) }
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def s3
|
52
|
+
Pgchief::Config.s3
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/lib/pgchief/database.rb
CHANGED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Pgchief
|
4
|
+
module Prompt
|
5
|
+
# Class to prompt for which database to restore
|
6
|
+
class RestoreDatabase < Base
|
7
|
+
def call
|
8
|
+
database = prompt.select("Which database needs restoring?", Pgchief::Database.all)
|
9
|
+
local_file = prompt.select("Which backup file do you want to restore?", Pgchief::Database.backups_for(database))
|
10
|
+
result = Pgchief::Command::DatabaseRestore.call(database, local_file)
|
11
|
+
|
12
|
+
prompt.say result
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/pgchief/version.rb
CHANGED
data/lib/pgchief.rb
CHANGED
@@ -11,18 +11,20 @@ require "pgchief/config/s3"
|
|
11
11
|
require "pgchief/connection_string"
|
12
12
|
require "pgchief/version"
|
13
13
|
require "pgchief/database"
|
14
|
+
require "pgchief/database/backups"
|
14
15
|
require "pgchief/user"
|
15
16
|
|
16
17
|
require "pgchief/prompt/base"
|
17
|
-
require "pgchief/prompt/start"
|
18
18
|
require "pgchief/prompt/backup_database"
|
19
19
|
require "pgchief/prompt/create_database"
|
20
20
|
require "pgchief/prompt/create_user"
|
21
21
|
require "pgchief/prompt/database_management"
|
22
22
|
require "pgchief/prompt/drop_database"
|
23
23
|
require "pgchief/prompt/drop_user"
|
24
|
-
require "pgchief/prompt/user_management"
|
25
24
|
require "pgchief/prompt/grant_database_privileges"
|
25
|
+
require "pgchief/prompt/restore_database"
|
26
|
+
require "pgchief/prompt/start"
|
27
|
+
require "pgchief/prompt/user_management"
|
26
28
|
require "pgchief/prompt/view_database_connection_string"
|
27
29
|
|
28
30
|
require "pgchief/command"
|
@@ -33,6 +35,7 @@ require "pgchief/command/database_create"
|
|
33
35
|
require "pgchief/command/database_drop"
|
34
36
|
require "pgchief/command/database_list"
|
35
37
|
require "pgchief/command/database_privileges_grant"
|
38
|
+
require "pgchief/command/database_restore"
|
36
39
|
require "pgchief/command/retrieve_connection_string"
|
37
40
|
require "pgchief/command/s3_upload"
|
38
41
|
require "pgchief/command/store_connection_string"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pgchief
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel Oliveira
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-11-
|
11
|
+
date: 2024-11-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk-s3
|
@@ -108,6 +108,7 @@ files:
|
|
108
108
|
- lib/pgchief/command/database_drop.rb
|
109
109
|
- lib/pgchief/command/database_list.rb
|
110
110
|
- lib/pgchief/command/database_privileges_grant.rb
|
111
|
+
- lib/pgchief/command/database_restore.rb
|
111
112
|
- lib/pgchief/command/retrieve_connection_string.rb
|
112
113
|
- lib/pgchief/command/s3_upload.rb
|
113
114
|
- lib/pgchief/command/store_connection_string.rb
|
@@ -118,6 +119,7 @@ files:
|
|
118
119
|
- lib/pgchief/config/s3.rb
|
119
120
|
- lib/pgchief/connection_string.rb
|
120
121
|
- lib/pgchief/database.rb
|
122
|
+
- lib/pgchief/database/backups.rb
|
121
123
|
- lib/pgchief/prompt.rb
|
122
124
|
- lib/pgchief/prompt/backup_database.rb
|
123
125
|
- lib/pgchief/prompt/base.rb
|
@@ -127,6 +129,7 @@ files:
|
|
127
129
|
- lib/pgchief/prompt/drop_database.rb
|
128
130
|
- lib/pgchief/prompt/drop_user.rb
|
129
131
|
- lib/pgchief/prompt/grant_database_privileges.rb
|
132
|
+
- lib/pgchief/prompt/restore_database.rb
|
130
133
|
- lib/pgchief/prompt/start.rb
|
131
134
|
- lib/pgchief/prompt/user_management.rb
|
132
135
|
- lib/pgchief/prompt/view_database_connection_string.rb
|