backy_rb 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. checksums.yaml +4 -4
  2. data/.backyrc.example +17 -0
  3. data/CHANGELOG.md +3 -0
  4. data/Gemfile +4 -0
  5. data/Gemfile.lock +134 -1
  6. data/README.md +69 -11
  7. data/{backy.gemspec → backy_rb.gemspec} +5 -2
  8. data/bin/backy +4 -0
  9. data/dummy/psql/Gemfile +0 -34
  10. data/lib/backy/cli.rb +88 -0
  11. data/lib/backy/configuration.rb +65 -2
  12. data/lib/backy/db.rb +1 -0
  13. data/lib/backy/list.rb +1 -1
  14. data/lib/backy/logger.rb +27 -0
  15. data/lib/backy/pg_dump.rb +60 -6
  16. data/lib/backy/pg_restore.rb +73 -7
  17. data/lib/backy/version.rb +1 -1
  18. data/lib/{backy.rb → backy_rb.rb} +15 -0
  19. metadata +53 -61
  20. data/dummy/psql/.gitattributes +0 -7
  21. data/dummy/psql/.gitignore +0 -22
  22. data/dummy/psql/.ruby-version +0 -1
  23. data/dummy/psql/Gemfile.lock +0 -207
  24. data/dummy/psql/README.md +0 -24
  25. data/dummy/psql/Rakefile +0 -6
  26. data/dummy/psql/app/assets/config/manifest.js +0 -2
  27. data/dummy/psql/app/assets/images/.keep +0 -0
  28. data/dummy/psql/app/assets/stylesheets/application.css +0 -15
  29. data/dummy/psql/app/controllers/application_controller.rb +0 -2
  30. data/dummy/psql/app/controllers/concerns/.keep +0 -0
  31. data/dummy/psql/app/controllers/posts_controller.rb +0 -59
  32. data/dummy/psql/app/helpers/application_helper.rb +0 -2
  33. data/dummy/psql/app/helpers/posts_helper.rb +0 -2
  34. data/dummy/psql/app/models/application_record.rb +0 -3
  35. data/dummy/psql/app/models/concerns/.keep +0 -0
  36. data/dummy/psql/app/models/post.rb +0 -2
  37. data/dummy/psql/app/views/layouts/application.html.erb +0 -15
  38. data/dummy/psql/app/views/posts/_form.html.erb +0 -27
  39. data/dummy/psql/app/views/posts/_post.html.erb +0 -12
  40. data/dummy/psql/app/views/posts/edit.html.erb +0 -10
  41. data/dummy/psql/app/views/posts/index.html.erb +0 -14
  42. data/dummy/psql/app/views/posts/new.html.erb +0 -9
  43. data/dummy/psql/app/views/posts/show.html.erb +0 -10
  44. data/dummy/psql/bin/bundle +0 -109
  45. data/dummy/psql/bin/rails +0 -4
  46. data/dummy/psql/bin/rake +0 -4
  47. data/dummy/psql/bin/setup +0 -33
  48. data/dummy/psql/config/application.rb +0 -43
  49. data/dummy/psql/config/boot.rb +0 -3
  50. data/dummy/psql/config/credentials.yml.enc +0 -1
  51. data/dummy/psql/config/database.yml +0 -87
  52. data/dummy/psql/config/environment.rb +0 -5
  53. data/dummy/psql/config/environments/development.rb +0 -62
  54. data/dummy/psql/config/environments/production.rb +0 -75
  55. data/dummy/psql/config/environments/test.rb +0 -50
  56. data/dummy/psql/config/locales/en.yml +0 -33
  57. data/dummy/psql/config/master.key +0 -1
  58. data/dummy/psql/config/puma.rb +0 -43
  59. data/dummy/psql/config/routes.rb +0 -7
  60. data/dummy/psql/config.ru +0 -6
  61. data/dummy/psql/db/migrate/20230330203226_create_posts.rb +0 -10
  62. data/dummy/psql/db/schema.rb +0 -23
  63. data/dummy/psql/db/seeds.rb +0 -8
  64. data/dummy/psql/log/.keep +0 -0
  65. data/dummy/psql/public/404.html +0 -67
  66. data/dummy/psql/public/422.html +0 -67
  67. data/dummy/psql/public/500.html +0 -66
  68. data/dummy/psql/public/apple-touch-icon-precomposed.png +0 -0
  69. data/dummy/psql/public/apple-touch-icon.png +0 -0
  70. data/dummy/psql/public/favicon.ico +0 -0
  71. data/dummy/psql/public/robots.txt +0 -1
  72. data/dummy/psql/tmp/.keep +0 -0
  73. data/dummy/psql/tmp/pids/.keep +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 79bb6103213004ae32e66537026b14efb957a6de480b11cc9fe0614eec32ed81
4
- data.tar.gz: ef8e5b3e93dfd4da6aead26891631118b6b000fd3ca50f035e5f5975d8033949
3
+ metadata.gz: 976aef5ff1edf205faaa2494ddbf2e7a52ee40437b85f2b4a7e9b5cc46cdfef1
4
+ data.tar.gz: b8774cee38066ddcc000d916e204573a08c07cef91f1d5e809521e990e3a299e
5
5
  SHA512:
6
- metadata.gz: ce262c9c1f830f87a304f3ee2fcee5d34aa68d0180a1397f1bb1c768322f43adefebc1dbf99baab58b4e78a6e047b89838dec865c22eb41593fb448a3a2c1719
7
- data.tar.gz: 3910848351967b47731abaabdab28251c5be76d63fe21b8e8c64066cf56dc40d3def935d3266f3a8d71de9901c965b2260c1241b688f80fbc36c56859c59b506
6
+ metadata.gz: d786dd5a26d31bc9a7d17c68bd1dff4f796ce50860c900e302cedb55442b4493a62f00ef5103cd0c019052fa16a06f90b2c9d05e97c597c74dc145210537c357
7
+ data.tar.gz: df6656391c48a16172ef5dcaded48df92228f690c0a137c752fc879012542317ceb62a97f71cb721f0d21241ffb9838f2830815a2659525067cae1d6a31f899a
data/.backyrc.example ADDED
@@ -0,0 +1,17 @@
1
+ # ~/.backyrc
2
+ defaults:
3
+ use_parallel: true
4
+ s3:
5
+ access_key_id: "YOUR_AWS_ACCESS_KEY_ID"
6
+ secret_access_key: "YOUR_AWS_SECRET_ACCESS_KEY"
7
+ region: "eu-central-1"
8
+ bucket: "your-s3-bucket-name"
9
+ database:
10
+ adapter: "postgresql"
11
+ host: "localhost"
12
+ port: 5432
13
+ username: "your-db-username"
14
+ password: "your-db-password"
15
+ database_name: "your-database-name"
16
+ local:
17
+ backup_path: "/path/to/your/local/backup/directory"
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.4] - 2023-12-28
4
+ - Adds CLI
5
+
3
6
  ## [0.1.0] - 2023-03-30
4
7
 
5
8
  - Initial release
data/Gemfile CHANGED
@@ -14,3 +14,7 @@ gem "standard", "~> 1.3"
14
14
  gem "aws-sdk-s3", "~> 1.117"
15
15
 
16
16
  gem "pg"
17
+
18
+ gem "thor", "~> 1.2"
19
+
20
+ gem "puma"
data/Gemfile.lock CHANGED
@@ -9,11 +9,66 @@ PATH
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
+ actioncable (7.0.5)
13
+ actionpack (= 7.0.5)
14
+ activesupport (= 7.0.5)
15
+ nio4r (~> 2.0)
16
+ websocket-driver (>= 0.6.1)
17
+ actionmailbox (7.0.5)
18
+ actionpack (= 7.0.5)
19
+ activejob (= 7.0.5)
20
+ activerecord (= 7.0.5)
21
+ activestorage (= 7.0.5)
22
+ activesupport (= 7.0.5)
23
+ mail (>= 2.7.1)
24
+ net-imap
25
+ net-pop
26
+ net-smtp
27
+ actionmailer (7.0.5)
28
+ actionpack (= 7.0.5)
29
+ actionview (= 7.0.5)
30
+ activejob (= 7.0.5)
31
+ activesupport (= 7.0.5)
32
+ mail (~> 2.5, >= 2.5.4)
33
+ net-imap
34
+ net-pop
35
+ net-smtp
36
+ rails-dom-testing (~> 2.0)
37
+ actionpack (7.0.5)
38
+ actionview (= 7.0.5)
39
+ activesupport (= 7.0.5)
40
+ rack (~> 2.0, >= 2.2.4)
41
+ rack-test (>= 0.6.3)
42
+ rails-dom-testing (~> 2.0)
43
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
44
+ actiontext (7.0.5)
45
+ actionpack (= 7.0.5)
46
+ activerecord (= 7.0.5)
47
+ activestorage (= 7.0.5)
48
+ activesupport (= 7.0.5)
49
+ globalid (>= 0.6.0)
50
+ nokogiri (>= 1.8.5)
51
+ actionview (7.0.5)
52
+ activesupport (= 7.0.5)
53
+ builder (~> 3.1)
54
+ erubi (~> 1.4)
55
+ rails-dom-testing (~> 2.0)
56
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
57
+ activejob (7.0.5)
58
+ activesupport (= 7.0.5)
59
+ globalid (>= 0.3.6)
12
60
  activemodel (7.0.5)
13
61
  activesupport (= 7.0.5)
14
62
  activerecord (7.0.5)
15
63
  activemodel (= 7.0.5)
16
64
  activesupport (= 7.0.5)
65
+ activestorage (7.0.5)
66
+ actionpack (= 7.0.5)
67
+ activejob (= 7.0.5)
68
+ activerecord (= 7.0.5)
69
+ activesupport (= 7.0.5)
70
+ marcel (~> 1.0)
71
+ mini_mime (>= 1.1.0)
17
72
  activesupport (7.0.5)
18
73
  concurrent-ruby (~> 1.0, >= 1.0.2)
19
74
  i18n (>= 1.6, < 2)
@@ -37,18 +92,84 @@ GEM
37
92
  aws-sigv4 (1.5.2)
38
93
  aws-eventstream (~> 1, >= 1.0.2)
39
94
  brakeman (5.4.1)
95
+ builder (3.2.4)
40
96
  concurrent-ruby (1.2.2)
97
+ crass (1.0.6)
98
+ date (3.3.3)
41
99
  diff-lcs (1.5.0)
42
- i18n (1.14.0)
100
+ erubi (1.12.0)
101
+ faker (3.2.0)
102
+ i18n (>= 1.8.11, < 2)
103
+ globalid (1.1.0)
104
+ activesupport (>= 5.0)
105
+ i18n (1.14.1)
43
106
  concurrent-ruby (~> 1.0)
44
107
  jmespath (1.6.2)
45
108
  json (2.6.3)
46
109
  language_server-protocol (3.17.0.3)
110
+ loofah (2.21.3)
111
+ crass (~> 1.0.2)
112
+ nokogiri (>= 1.12.0)
113
+ mail (2.8.1)
114
+ mini_mime (>= 0.1.1)
115
+ net-imap
116
+ net-pop
117
+ net-smtp
118
+ marcel (1.0.2)
119
+ method_source (1.0.0)
120
+ mini_mime (1.1.2)
47
121
  minitest (5.18.0)
122
+ net-imap (0.3.6)
123
+ date
124
+ net-protocol
125
+ net-pop (0.1.2)
126
+ net-protocol
127
+ net-protocol (0.2.1)
128
+ timeout
129
+ net-smtp (0.3.3)
130
+ net-protocol
131
+ nio4r (2.5.9)
132
+ nokogiri (1.15.2-arm64-darwin)
133
+ racc (~> 1.4)
134
+ nokogiri (1.15.2-x86_64-linux)
135
+ racc (~> 1.4)
48
136
  parallel (1.22.1)
49
137
  parser (3.2.1.1)
50
138
  ast (~> 2.4.1)
51
139
  pg (1.5.3)
140
+ puma (6.3.0)
141
+ nio4r (~> 2.0)
142
+ racc (1.7.1)
143
+ rack (2.2.7)
144
+ rack-test (2.1.0)
145
+ rack (>= 1.3)
146
+ rails (7.0.5)
147
+ actioncable (= 7.0.5)
148
+ actionmailbox (= 7.0.5)
149
+ actionmailer (= 7.0.5)
150
+ actionpack (= 7.0.5)
151
+ actiontext (= 7.0.5)
152
+ actionview (= 7.0.5)
153
+ activejob (= 7.0.5)
154
+ activemodel (= 7.0.5)
155
+ activerecord (= 7.0.5)
156
+ activestorage (= 7.0.5)
157
+ activesupport (= 7.0.5)
158
+ bundler (>= 1.15.0)
159
+ railties (= 7.0.5)
160
+ rails-dom-testing (2.0.3)
161
+ activesupport (>= 4.2.0)
162
+ nokogiri (>= 1.6)
163
+ rails-html-sanitizer (1.6.0)
164
+ loofah (~> 2.21)
165
+ nokogiri (~> 1.14)
166
+ railties (7.0.5)
167
+ actionpack (= 7.0.5)
168
+ activesupport (= 7.0.5)
169
+ method_source
170
+ rake (>= 12.2)
171
+ thor (~> 1.0)
172
+ zeitwerk (~> 2.5)
52
173
  rainbow (3.1.1)
53
174
  rake (13.0.6)
54
175
  regexp_parser (2.7.0)
@@ -93,10 +214,17 @@ GEM
93
214
  language_server-protocol (~> 3.17.0.2)
94
215
  rubocop (~> 1.48.1)
95
216
  rubocop-performance (~> 1.16.0)
217
+ thor (1.2.2)
96
218
  timecop (0.9.6)
219
+ timeout (0.3.2)
97
220
  tzinfo (2.0.6)
98
221
  concurrent-ruby (~> 1.0)
99
222
  unicode-display_width (2.4.2)
223
+ websocket-driver (0.7.5)
224
+ websocket-extensions (>= 0.1.0)
225
+ websocket-extensions (0.1.5)
226
+ zeitwerk (2.6.8)
227
+ zlib (3.0.0)
100
228
 
101
229
  PLATFORMS
102
230
  arm64-darwin-22
@@ -106,7 +234,10 @@ DEPENDENCIES
106
234
  aws-sdk-s3 (~> 1.117)
107
235
  backy_rb!
108
236
  brakeman (~> 5.4)
237
+ faker (>= 3.1)
109
238
  pg
239
+ puma
240
+ rails (>= 4.0)
110
241
  rake (~> 13.0)
111
242
  rspec (~> 3.0)
112
243
  rubocop (~> 1.44)
@@ -114,7 +245,9 @@ DEPENDENCIES
114
245
  rubocop-rake (~> 0.6.0)
115
246
  rubocop-rspec (~> 2.18)
116
247
  standard (~> 1.3)
248
+ thor (~> 1.2)
117
249
  timecop (~> 0.9.6)
250
+ zlib (~> 3.0)
118
251
 
119
252
  BUNDLED WITH
120
253
  2.4.8
data/README.md CHANGED
@@ -4,26 +4,84 @@
4
4
 
5
5
  Introducing "Backy" - the ultimate database backup gem for Ruby on Rails applications! Safeguard your valuable data with ease, speed, and reliability. Developed with the Rails community in mind, Backy provides a comprehensive solution for handling your database backups, ensuring your information is safe and sound.
6
6
 
7
- ## Installation Rails
7
+ ## Features
8
8
 
9
- Add gem to gemfile
9
+ - Database backup and restore.
10
+ - Integration with AWS S3 for storing backups.
11
+ - Support for both standalone usage and as part of a Rails application.
12
+ - Automatic configuration inference when used within a Rails application.
13
+ - Logging with colored output.
14
+ - Command Line Interface (CLI) for easy management.
15
+
16
+ ## Installation Rails
10
17
 
11
- gem 'backy'
12
18
 
13
- Create initializer `config/initializers/backy.rb`
19
+ Add this line to your application's Gemfile:
14
20
 
15
21
  ```ruby
16
- Backy.configure do |config|
17
- config.s3_access_key = ""
18
- config.s3_secret = ""
19
- config.s3_bucket = ""
20
- config.s3_region = ""
21
- end
22
+ gem 'backy'
22
23
  ```
23
24
 
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install backy_rb
32
+
33
+
24
34
  ## Usage
25
35
 
26
- TODO
36
+ ### Standalone
37
+
38
+ To use Backy in a standalone environment, you can utilize the provided CLI.
39
+
40
+ $ backy [command] [options]
41
+
42
+ Available commands include:
43
+
44
+ * `download`: Download a file from S3
45
+ * `dump`: Dump the database
46
+ * `help [COMMAND]`: Describe available commands or one specific command
47
+ * `list`: List all dumps
48
+ * `push`: Push dump to S3 and delete local file
49
+ * `restore`: Restore a database from a dump
50
+ * `upload`: Upload a specific file to S3
51
+
52
+ ### In a Rails Application
53
+
54
+ Backy seamlessly integrates with Rails applications. When included in a Rails app, Backy can automatically infer configurations from the Rails environment.
55
+
56
+ #### Rake Tasks
57
+ Backy provides Rake tasks for easy integration:
58
+
59
+ ```
60
+ bin/rails backy:dump
61
+ bin/rails backy:restore
62
+ ```
63
+
64
+ ## Configuration
65
+
66
+ Backy can be configured through a .backyrc YAML file. Place this file in your home directory or the root of your Rails application.
67
+
68
+ Example `.backyrc`:
69
+
70
+ ```yaml
71
+ defaults:
72
+ use_parallel: true
73
+ s3:
74
+ access_key_id: YOUR_ACCESS_KEY
75
+ secret_access_key: YOUR_SECRET_KEY
76
+ region: YOUR_REGION
77
+ bucket: YOUR_BUCKET
78
+ database:
79
+ host: DB_HOST
80
+ port: DB_PORT
81
+ username: DB_USERNAME
82
+ password: DB_PASSWORD
83
+ database_name: DB_NAME
84
+ ```
27
85
 
28
86
  ## Development
29
87
 
@@ -24,8 +24,8 @@ Gem::Specification.new do |spec|
24
24
  (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
25
25
  end
26
26
  end
27
- spec.bindir = "exe"
28
- spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
27
+
28
+ spec.executables = ["backy"]
29
29
  spec.require_paths = ["lib"]
30
30
 
31
31
  spec.add_development_dependency "rspec", "~> 3.0"
@@ -36,6 +36,9 @@ Gem::Specification.new do |spec|
36
36
  spec.add_development_dependency "rubocop-performance", "~> 1.15"
37
37
  spec.add_development_dependency "rubocop-rake", "~> 0.6.0"
38
38
  spec.add_development_dependency "timecop", "~> 0.9.6"
39
+ spec.add_development_dependency "zlib", "~> 3.0"
40
+ spec.add_development_dependency "rails", ">= 4.0"
41
+ spec.add_development_dependency "faker", ">= 3.1"
39
42
 
40
43
  spec.add_dependency "activerecord", ">= 4.0"
41
44
  spec.add_dependency "activesupport", ">= 4.0"
data/bin/backy ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative "../lib/backy_rb"
3
+
4
+ Backy::CLI.start(ARGV)
data/dummy/psql/Gemfile CHANGED
@@ -1,34 +0,0 @@
1
- source "https://rubygems.org"
2
- git_source(:github) { |repo| "https://github.com/#{repo}.git" }
3
-
4
- ruby "3.2.0"
5
-
6
- # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
7
- gem "rails", "~> 7.0.4", ">= 7.0.4.2"
8
-
9
- # The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
10
- gem "sprockets-rails"
11
-
12
- # Use postgresql as the database for Active Record
13
- gem "pg", "~> 1.1"
14
-
15
- # Use the Puma web server [https://github.com/puma/puma]
16
- gem "puma", "~> 5.0"
17
-
18
- # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
19
- gem "tzinfo-data", platforms: %i[mingw mswin x64_mingw jruby]
20
-
21
- # Backy <3
22
- gem "backy", path: "../../"
23
-
24
- group :development, :test do
25
- # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
26
- gem "debug", platforms: %i[mri mingw x64_mingw]
27
- end
28
-
29
- group :development do
30
- # Speed up commands on slow machines / big apps [https://github.com/rails/spring]
31
- # gem "spring"
32
- end
33
-
34
- gem "faker", "~> 3.1"
data/lib/backy/cli.rb CHANGED
@@ -0,0 +1,88 @@
1
+ require "thor"
2
+ require "yaml"
3
+
4
+ module Backy
5
+ class CLI < Thor
6
+ def initialize(*args)
7
+ Logger.log("Initializing Backy CLI...")
8
+ super
9
+ Backy.setup
10
+ end
11
+
12
+ desc "dump", "Dump the database"
13
+ def dump
14
+ Backy::PgDump.new.call
15
+ Logger.success("Database dumped successfully.")
16
+ end
17
+
18
+ desc "push", "Push dump to S3 and delete local file"
19
+ def push
20
+ file_name = Backy::PgDump.new.call
21
+ save_to_s3(file_name)
22
+ File.delete(file_name) if File.exist?(file_name)
23
+ Logger.sucess("Dump pushed to S3 and local file deleted.")
24
+ end
25
+
26
+ desc "upload", "Upload a specific file to S3"
27
+ method_option :file, aliases: "-f", desc: "File to upload"
28
+ def upload
29
+ file_name = options[:file]
30
+ validate_file!(file_name)
31
+ save_to_s3(file_name)
32
+ Logger.success("File uploaded to S3.")
33
+ end
34
+
35
+ desc "download", "Download a file from S3"
36
+ method_option :file, aliases: "-f", desc: "File to download"
37
+ def download
38
+ file_name = options[:file]
39
+ validate_file!(file_name, should_exist: false)
40
+ load_from_s3_if_missing(file_name)
41
+ Logger.success("File downloaded from S3.")
42
+ end
43
+
44
+ desc "list", "List all dumps"
45
+ def list
46
+ list = Backy::List.new.call
47
+ list.each { |item| say "#{item.local? ? "local" : " "} #{item.remote? ? "remote" : " "} : #{item.dump_file}", :yellow }
48
+ say_instructions_if_any(list)
49
+ end
50
+
51
+ desc "restore", "Restore a database from a dump"
52
+ method_option :file, aliases: "-f", desc: "File to restore from"
53
+ def restore
54
+ file_name = find_file_for_restore(options[:file])
55
+ pg_restore(load_from_s3_if_missing(file_name)) if file_name
56
+ Logger.success("Database restored from #{file_name}.")
57
+ end
58
+
59
+ private
60
+
61
+ def validate_file!(file_name, should_exist: true)
62
+ if file_name.nil? || (should_exist ? !File.exist?(file_name) : File.exist?(file_name))
63
+ Logger.error("Invalid file specified.")
64
+ exit 1
65
+ end
66
+ end
67
+
68
+ def find_file_for_restore(template_file_name)
69
+ file_name = Backy::List.new.call.reverse.find { |item| item.dump_file.starts_with?(template_file_name) }&.dump_file if template_file_name.present?
70
+ Logger.info "Please set the file to restore from." if file_name.nil?
71
+ file_name
72
+ end
73
+
74
+ def say_instructions_if_any(list)
75
+ return unless list.any?
76
+ Logger.info("\nTo restore a backup, run:")
77
+ Logger.log(" backy restore --file FILE_NAME")
78
+ end
79
+
80
+ def load_from_s3_if_missing(file_name)
81
+ Backy::S3Load.new(file_name: file_name).call
82
+ end
83
+
84
+ def save_to_s3(file_name)
85
+ Backy::S3Save.new(file_name: file_name).call
86
+ end
87
+ end
88
+ end
@@ -12,9 +12,19 @@ module Backy
12
12
  :s3_bucket,
13
13
  :app_name,
14
14
  :environment,
15
+ :use_parallel,
15
16
  :log_file
16
17
  )
17
18
 
19
+ def load
20
+ local_config_file = File.join(Dir.pwd, '.backyrc')
21
+ global_config_file = File.join(Dir.home, '.backyrc')
22
+
23
+ config_file = File.exist?(local_config_file) ? local_config_file : global_config_file
24
+ Logger.log("Loading configuration from #{config_file}...") if File.exist?(config_file)
25
+ load_from_file(config_file) if File.exist?(config_file)
26
+ end
27
+
18
28
  def pg_host
19
29
  @pg_host ||= ENV["PG_HOST"]
20
30
  end
@@ -51,8 +61,29 @@ module Backy
51
61
  @s3_bucket ||= ENV["S3_BUCKET"]
52
62
  end
53
63
 
64
+ def use_parallel
65
+ @use_parallel ||= ENV["BACKY_USE_PARALLEL"] == "true"
66
+ end
67
+
68
+ def use_parallel?
69
+ use_parallel && pigz_installed && multicore
70
+ end
71
+
72
+ # Detect if pigz binary is available
73
+ # If it is, use it to speed up the dump
74
+ # pigz is a parallel gzip implementation
75
+ # https://zlib.net/pigz/
76
+ def pigz_installed
77
+ @pigz_installed ||= system("which pigz > /dev/null 2>&1")
78
+ end
79
+
80
+ # Determine if the system is multicore
81
+ def multicore
82
+ @multicore ||= Etc.nprocessors > 1
83
+ end
84
+
54
85
  def app_name
55
- @app_name ||= "backy"
86
+ @app_name ||= ENV["APP_NAME"].presence || "backy"
56
87
  end
57
88
 
58
89
  def environment
@@ -60,7 +91,39 @@ module Backy
60
91
  end
61
92
 
62
93
  def log_file
63
- @log_file ||= "log/backy.log"
94
+ @log_file ||= default_log_file
95
+ end
96
+
97
+ private
98
+
99
+ def default_log_file
100
+ if Gem.win_platform?
101
+ # Windows default path
102
+ File.join(Dir.home, "AppData", "Local", "#{app_name}", "log", "#{app_name}.log")
103
+ else
104
+ # Unix-like systems default path
105
+ File.join(Dir.home, ".local", "share", "#{app_name}", "log", "#{app_name}.log")
106
+ end
107
+ end
108
+
109
+ def load_from_file(file_path)
110
+ configuration = YAML.load_file(file_path)
111
+
112
+ @s3_access_key = configuration.dig("defaults", "s3", "access_key_id")
113
+ @s3_secret = configuration.dig("defaults", "s3", "secret_access_key")
114
+ @s3_region = configuration.dig("defaults", "s3", "region")
115
+ @s3_bucket = configuration.dig("defaults", "s3", "bucket")
116
+
117
+ @pg_host = configuration.dig("defaults", "database", "host")
118
+ @pg_port = configuration.dig("defaults", "database", "port")
119
+ @pg_username = configuration.dig("defaults", "database", "username")
120
+ @pg_password = configuration.dig("defaults", "database", "password")
121
+ @pg_database = configuration.dig("defaults", "database", "database_name")
122
+
123
+ @app_name = configuration.dig("defaults", "app_name") || "backy"
124
+ @environment = configuration.dig("defaults", "environment") || "development"
125
+ @log_file = configuration.dig("defaults", "log", "file") || default_log_file
126
+ @use_parallel = configuration.dig("defaults", "use_parallel") || false
64
127
  end
65
128
  end
66
129
  end
data/lib/backy/db.rb CHANGED
@@ -11,6 +11,7 @@ module Backy
11
11
  def_delegator "Backy.configuration", :pg_database, :database
12
12
  def_delegator "Backy.configuration", :pg_username, :username
13
13
  def_delegator "Backy.configuration", :pg_password, :password
14
+ def_delegator "Backy.configuration", :use_parallel?, :use_parallel?
14
15
 
15
16
  def pg_password_env
16
17
  password.present? ? "PGPASSWORD='#{password}' " : ""
data/lib/backy/list.rb CHANGED
@@ -5,7 +5,7 @@ module Backy
5
5
  remotes = Set.new(S3List.new.call)
6
6
 
7
7
  (locals + remotes).sort.map do |dump_file|
8
- OpenStruct.new(local?: dump_file.in?(locals), remote?: dump_file.in?(remotes), dump_file: dump_file)
8
+ OpenStruct.new(local?: locals.include?(dump_file), remote?: remotes.include?(dump_file), dump_file: dump_file)
9
9
  end
10
10
  end
11
11
 
@@ -0,0 +1,27 @@
1
+ require 'thor'
2
+
3
+ module Backy
4
+ class Logger
5
+ # Logs a message with the specified color using Thor's shell
6
+ def self.log(message, color = nil)
7
+ thor_shell = Thor::Base.shell.new
8
+ thor_shell.say("[#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}] #{message}", color)
9
+ end
10
+
11
+ def self.success(message)
12
+ log(message, :green)
13
+ end
14
+
15
+ def self.info(message)
16
+ log(message, :blue)
17
+ end
18
+
19
+ def self.warn(message)
20
+ log(message, :yellow)
21
+ end
22
+
23
+ def self.error(message)
24
+ log(message, :red)
25
+ end
26
+ end
27
+ end