postgresql-backup 0.0.7 → 0.0.8
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/CHANGELOG.md +4 -0
- data/README.md +103 -2
- data/lib/configuration.rb +23 -9
- data/lib/hooks.rb +29 -0
- data/lib/tools/database.rb +12 -0
- metadata +4 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9d8de117460200af5ebacdfaa61c91cb8f46de9b7d91123d8d0f606e31fce16d
|
|
4
|
+
data.tar.gz: a8484224c080047669ca383459f69d5269f393198ded85b7a96c06048ae9ecd6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8ac205a5ff92a7ed2298d9c7c07af0130f8b41cb4b809d0e07788b9ae3d7b7e8f53c24690e3d2ccae2d7448287ba369ff9c21bebbe245c7dc800d3153a3b7150
|
|
7
|
+
data.tar.gz: e8d7921301d7a67ef563c5996686289bf6581d47d44a08dd99120d9a0023c4c682b88f72426dfc9ada53d1507668537ab142743b14723bc3d1b04933365f537d
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
|
@@ -26,7 +26,13 @@ Go to the terminal and update your gems using bundler:
|
|
|
26
26
|
bundle install
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
In the Rakefile of your project, add `require 'postgresql_backup'` anywhere **before** this line:
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
Rails.application.load_tasks
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Right now, your project already has two new rake tasks: `postgresql_backup:dump` and `postgresql_backup:restore`.
|
|
30
36
|
|
|
31
37
|
## Configuration
|
|
32
38
|
|
|
@@ -39,6 +45,8 @@ Create a file inside the `config/initializers` folder. The name is not important
|
|
|
39
45
|
Here is an example with all available options you can change:
|
|
40
46
|
|
|
41
47
|
```ruby
|
|
48
|
+
require 'postgresql_backup'
|
|
49
|
+
|
|
42
50
|
PostgresqlBackup.configure do |config|
|
|
43
51
|
# This gem works with two possible repositories:
|
|
44
52
|
#
|
|
@@ -84,6 +92,20 @@ PostgresqlBackup.configure do |config|
|
|
|
84
92
|
# path where they are going to be stored. The remote path is the
|
|
85
93
|
# place to do that. The default value is `_backups/database/`
|
|
86
94
|
config.remote_path = ''
|
|
95
|
+
|
|
96
|
+
# There are cases where we need to run a command before or after the database
|
|
97
|
+
# is restored or a backup is created. To accomplish this, you can set the
|
|
98
|
+
# `hooks` attribute to a class or an instance of a class that
|
|
99
|
+
# responds to the method you need.
|
|
100
|
+
#
|
|
101
|
+
# Available hook methods are:
|
|
102
|
+
#
|
|
103
|
+
# * before_restore
|
|
104
|
+
# * after_restore
|
|
105
|
+
# * before_dump
|
|
106
|
+
# * after_dump
|
|
107
|
+
#
|
|
108
|
+
config.hooks = nil
|
|
87
109
|
end
|
|
88
110
|
```
|
|
89
111
|
|
|
@@ -150,6 +172,85 @@ Everything will work just fine, but you may come across some strange warnings, l
|
|
|
150
172
|
|
|
151
173
|
To prevent this, every time the rake restores a backup file it tries to replace the environment being copied into the ar_internal_metadata table with the current Rails environment. Thus, `environment production` will become `environment development`.
|
|
152
174
|
|
|
175
|
+
## Database restore hooks
|
|
176
|
+
|
|
177
|
+
Sometimes we need to run things every time a database restore is about to happen, or maybe after the restore is completed. You may even need to run code before or after a backup is created.
|
|
178
|
+
|
|
179
|
+
For example, if you use Elasticsearch you may need to reindex it after restoring a database.
|
|
180
|
+
|
|
181
|
+
To accomplish this, you can use the `hooks` configurations:
|
|
182
|
+
|
|
183
|
+
Examples:
|
|
184
|
+
|
|
185
|
+
```ruby
|
|
186
|
+
class DatabaseBackupHooks
|
|
187
|
+
def before_restore
|
|
188
|
+
puts 'Backup is going to be restored...'
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def after_restore
|
|
192
|
+
puts 'Backup restored!'
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def before_dump
|
|
196
|
+
puts 'Database backup is about to be created...'
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def after_dump
|
|
200
|
+
puts 'Dump created!'
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
Then, you can set the `hooks` in the initializer:
|
|
206
|
+
|
|
207
|
+
```ruby
|
|
208
|
+
PostgresqlBackup.configure do |config|
|
|
209
|
+
config.hooks = DatabaseBackupHooks.new
|
|
210
|
+
end
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
It also works with classes with class methods:
|
|
214
|
+
|
|
215
|
+
```ruby
|
|
216
|
+
class DatabaseBackupHooks
|
|
217
|
+
def self.before_restore
|
|
218
|
+
puts 'Backup is going to be restored...'
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def self.after_restore
|
|
222
|
+
puts 'Backup restored!'
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
def self.before_dump
|
|
226
|
+
puts 'Database backup is about to be created...'
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
def self.after_dump
|
|
230
|
+
puts 'Dump created!'
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
```ruby
|
|
237
|
+
PostgresqlBackup.configure do |config|
|
|
238
|
+
config.hooks = DatabaseBackupHooks # Note that here we no longer instantiate the class
|
|
239
|
+
end
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
You can even create a class on the fly:
|
|
243
|
+
|
|
244
|
+
```ruby
|
|
245
|
+
PostgresqlBackup.configure do |config|
|
|
246
|
+
config.hooks = Class.new do
|
|
247
|
+
def self.after_restore
|
|
248
|
+
puts "after restore hook"
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
```
|
|
253
|
+
|
|
153
254
|
## I want to contribute
|
|
154
255
|
|
|
155
256
|
Feel free to open a pull request with the changes you want to make. Remember to update `CHANGELOG.md` with the change you are proposing, because once the PR is merged, it is important to show which changes are being made to the gem.
|
|
@@ -164,4 +265,4 @@ To run the tests, we use rspec:
|
|
|
164
265
|
|
|
165
266
|
```
|
|
166
267
|
rspec
|
|
167
|
-
```
|
|
268
|
+
```
|
data/lib/configuration.rb
CHANGED
|
@@ -1,25 +1,39 @@
|
|
|
1
|
+
require_relative 'hooks'
|
|
2
|
+
|
|
1
3
|
class Configuration
|
|
2
|
-
attr_accessor :
|
|
3
|
-
:
|
|
4
|
+
attr_accessor :aws_access_key_id,
|
|
5
|
+
:aws_secret_access_key,
|
|
6
|
+
:backup_folder,
|
|
7
|
+
:bucket,
|
|
8
|
+
:file_suffix,
|
|
9
|
+
:region,
|
|
10
|
+
:remote_path,
|
|
11
|
+
:repository
|
|
12
|
+
|
|
13
|
+
attr_reader :hooks
|
|
4
14
|
|
|
5
15
|
def initialize(
|
|
6
|
-
repository: 'file system',
|
|
7
|
-
backup_folder: 'db/backups',
|
|
8
|
-
file_suffix: '',
|
|
9
16
|
aws_access_key_id: '',
|
|
10
17
|
aws_secret_access_key: '',
|
|
18
|
+
backup_folder: 'db/backups',
|
|
11
19
|
bucket: '',
|
|
20
|
+
file_suffix: '',
|
|
12
21
|
region: '',
|
|
13
|
-
remote_path: '_backups/database/'
|
|
22
|
+
remote_path: '_backups/database/',
|
|
23
|
+
repository: 'file system'
|
|
14
24
|
)
|
|
15
|
-
@repository = repository
|
|
16
|
-
@backup_folder = backup_folder
|
|
17
|
-
@file_suffix = file_suffix
|
|
18
25
|
@aws_access_key_id = aws_access_key_id
|
|
19
26
|
@aws_secret_access_key = aws_secret_access_key
|
|
27
|
+
@backup_folder = backup_folder
|
|
20
28
|
@bucket = bucket
|
|
29
|
+
@file_suffix = file_suffix
|
|
21
30
|
@region = region
|
|
22
31
|
@remote_path = remote_path
|
|
32
|
+
@repository = repository
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def hooks=(hooks)
|
|
36
|
+
@hooks = Hooks.new(hooks)
|
|
23
37
|
end
|
|
24
38
|
|
|
25
39
|
def s3?
|
data/lib/hooks.rb
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
class Hooks
|
|
2
|
+
def initialize(hooks)
|
|
3
|
+
@hooks = hooks
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
def before_restore
|
|
7
|
+
return unless @hooks.respond_to?(:before_restore)
|
|
8
|
+
|
|
9
|
+
@hooks.before_restore
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def after_restore
|
|
13
|
+
return unless @hooks.respond_to?(:after_restore)
|
|
14
|
+
|
|
15
|
+
@hooks.after_restore
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def before_dump
|
|
19
|
+
return unless @hooks.respond_to?(:before_dump)
|
|
20
|
+
|
|
21
|
+
@hooks.before_dump
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def after_dump
|
|
25
|
+
return unless @hooks.respond_to?(:after_dump)
|
|
26
|
+
|
|
27
|
+
@hooks.after_dump
|
|
28
|
+
end
|
|
29
|
+
end
|
data/lib/tools/database.rb
CHANGED
|
@@ -12,11 +12,15 @@ module Tools
|
|
|
12
12
|
#
|
|
13
13
|
# Return the full path of the backup file created in the disk.
|
|
14
14
|
def dump(debug: false)
|
|
15
|
+
hooks.before_dump
|
|
16
|
+
|
|
15
17
|
file_path = File.join(backup_folder, "#{file_name}#{file_suffix}.sql")
|
|
16
18
|
|
|
17
19
|
cmd = "PGPASSWORD='#{password}' pg_dump -F p -v -O -U '#{user}' -h '#{host}' -d '#{database}' -f '#{file_path}' -p '#{port}' "
|
|
18
20
|
debug ? system(cmd) : system(cmd, err: File::NULL)
|
|
19
21
|
|
|
22
|
+
hooks.after_dump
|
|
23
|
+
|
|
20
24
|
file_path
|
|
21
25
|
end
|
|
22
26
|
|
|
@@ -35,11 +39,15 @@ module Tools
|
|
|
35
39
|
# If you need to make the command more verbose, pass
|
|
36
40
|
# `debug: true` in the arguments of the function.
|
|
37
41
|
def restore(file_name, debug: false)
|
|
42
|
+
hooks.before_restore
|
|
43
|
+
|
|
38
44
|
file_path = File.join(backup_folder, file_name)
|
|
39
45
|
output_redirection = debug ? '': ' > /dev/null'
|
|
40
46
|
cmd = "PGPASSWORD='#{password}' psql -U '#{user}' -h '#{host}' -d '#{database}' -f '#{file_path}' -p '#{port}' #{output_redirection}"
|
|
41
47
|
system(cmd)
|
|
42
48
|
|
|
49
|
+
hooks.after_restore
|
|
50
|
+
|
|
43
51
|
file_path
|
|
44
52
|
end
|
|
45
53
|
|
|
@@ -92,5 +100,9 @@ module Tools
|
|
|
92
100
|
end
|
|
93
101
|
end
|
|
94
102
|
end
|
|
103
|
+
|
|
104
|
+
def hooks
|
|
105
|
+
@hooks ||= configuration.hooks
|
|
106
|
+
end
|
|
95
107
|
end
|
|
96
108
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: postgresql-backup
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.8
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Artur Caliendo Prado
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2023-07-13 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bump
|
|
@@ -120,6 +120,7 @@ files:
|
|
|
120
120
|
- README.md
|
|
121
121
|
- lib/Rakefile
|
|
122
122
|
- lib/configuration.rb
|
|
123
|
+
- lib/hooks.rb
|
|
123
124
|
- lib/postgresql_backup.rb
|
|
124
125
|
- lib/railtie.rb
|
|
125
126
|
- lib/tasks/backup.rake
|
|
@@ -146,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
146
147
|
- !ruby/object:Gem::Version
|
|
147
148
|
version: '0'
|
|
148
149
|
requirements: []
|
|
149
|
-
rubygems_version: 3.
|
|
150
|
+
rubygems_version: 3.2.3
|
|
150
151
|
signing_key:
|
|
151
152
|
specification_version: 4
|
|
152
153
|
summary: Automate PostgreSQL's backup and restore
|