postgresql-backup 0.0.6 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -1
- data/README.md +128 -1
- data/lib/configuration.rb +23 -9
- data/lib/hooks.rb +29 -0
- data/lib/tools/database.rb +17 -5
- metadata +6 -5
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
@@ -1,4 +1,12 @@
|
|
1
|
-
###
|
1
|
+
### v0.0.8 - 2023-07-07
|
2
|
+
|
3
|
+
- Add support to hooks (methods to run before/after dump and restore)
|
4
|
+
|
5
|
+
### v0.0.7 - 2022-04-19
|
6
|
+
|
7
|
+
- Add support for ruby 3 and fog-aws 3.13.0
|
8
|
+
|
9
|
+
### v0.0.6 - 2021-04-25
|
2
10
|
|
3
11
|
- Add github as the homepage of the gem
|
4
12
|
- Add changelog to the project
|
data/README.md
CHANGED
@@ -2,6 +2,16 @@
|
|
2
2
|
|
3
3
|
This gem automates PostgreSQL's backup and restore in your Rails project. It will inject two rake tasks that you can use to manage your data, either by using the local system or AWS S3 storage.
|
4
4
|
|
5
|
+
The current version supports ruby 3. If you need backward compatibiliy, use [v0.0.6](https://rubygems.org/gems/postgresql-backup/versions/0.0.6) instead.
|
6
|
+
|
7
|
+
## How it looks like?
|
8
|
+
|
9
|
+
Dump:
|
10
|
+
![](https://res.cloudinary.com/ongmungazi/image/upload/v1650388791/ruby-gem/dump.gif)
|
11
|
+
|
12
|
+
Restore:
|
13
|
+
![](https://res.cloudinary.com/ongmungazi/image/upload/v1650388791/ruby-gem/restore.gif)
|
14
|
+
|
5
15
|
## Getting started
|
6
16
|
|
7
17
|
Add the gem to your Rails project:
|
@@ -16,7 +26,13 @@ Go to the terminal and update your gems using bundler:
|
|
16
26
|
bundle install
|
17
27
|
```
|
18
28
|
|
19
|
-
|
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`.
|
20
36
|
|
21
37
|
## Configuration
|
22
38
|
|
@@ -29,6 +45,8 @@ Create a file inside the `config/initializers` folder. The name is not important
|
|
29
45
|
Here is an example with all available options you can change:
|
30
46
|
|
31
47
|
```ruby
|
48
|
+
require 'postgresql_backup'
|
49
|
+
|
32
50
|
PostgresqlBackup.configure do |config|
|
33
51
|
# This gem works with two possible repositories:
|
34
52
|
#
|
@@ -74,6 +92,20 @@ PostgresqlBackup.configure do |config|
|
|
74
92
|
# path where they are going to be stored. The remote path is the
|
75
93
|
# place to do that. The default value is `_backups/database/`
|
76
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
|
77
109
|
end
|
78
110
|
```
|
79
111
|
|
@@ -139,3 +171,98 @@ Important note: if you are trying to locally restore a backup that was created i
|
|
139
171
|
Everything will work just fine, but you may come across some strange warnings, like when you try to drop the database: it will say you are droping a production database to double check if this is your intended purpose.
|
140
172
|
|
141
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`.
|
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
|
+
|
254
|
+
## I want to contribute
|
255
|
+
|
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.
|
257
|
+
|
258
|
+
The first thing to do is to update the dependencies. If you do not have bundle installed, run `gem install bundler`. Then:
|
259
|
+
|
260
|
+
```
|
261
|
+
bundle install
|
262
|
+
```
|
263
|
+
|
264
|
+
To run the tests, we use rspec:
|
265
|
+
|
266
|
+
```
|
267
|
+
rspec
|
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
|
|
@@ -57,23 +65,23 @@ module Tools
|
|
57
65
|
attr_reader :configuration
|
58
66
|
|
59
67
|
def host
|
60
|
-
@host ||= ::ActiveRecord::Base.
|
68
|
+
@host ||= ::ActiveRecord::Base.connection_db_config.configuration_hash[:host]
|
61
69
|
end
|
62
70
|
|
63
71
|
def port
|
64
|
-
@port ||= ::ActiveRecord::Base.
|
72
|
+
@port ||= ::ActiveRecord::Base.connection_db_config.configuration_hash[:port]
|
65
73
|
end
|
66
74
|
|
67
75
|
def database
|
68
|
-
@database ||= ::ActiveRecord::Base.
|
76
|
+
@database ||= ::ActiveRecord::Base.connection_db_config.configuration_hash[:database]
|
69
77
|
end
|
70
78
|
|
71
79
|
def user
|
72
|
-
::ActiveRecord::Base.
|
80
|
+
::ActiveRecord::Base.connection_db_config.configuration_hash[:username]
|
73
81
|
end
|
74
82
|
|
75
83
|
def password
|
76
|
-
@password ||= ::ActiveRecord::Base.
|
84
|
+
@password ||= ::ActiveRecord::Base.connection_db_config.configuration_hash[:password]
|
77
85
|
end
|
78
86
|
|
79
87
|
def file_name
|
@@ -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
|
@@ -58,14 +58,14 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 3.
|
61
|
+
version: 3.13.0
|
62
62
|
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: 3.
|
68
|
+
version: 3.13.0
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: pastel
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -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
|