ops_backups 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c11d46d1083b3b1b45c330e4b74db480d82d80ebd49356a74934dd63409f2c01
4
- data.tar.gz: d41344f6c9066beac9ba54ab9e96165fec31b33335b85af0ec26beebf2fba875
3
+ metadata.gz: e6f1ef117c4c1f91b7ca59eba0f0a27d2387b2f69c7d9f5022d1306c145d19eb
4
+ data.tar.gz: 1c479131ca94764b617a99cb8ea9896596ceda64aadc7a3281acbbca86286471
5
5
  SHA512:
6
- metadata.gz: 64d6bb17c0bc7e78cf236d4a2077aa3cbb0cf520576544e8b75c16a9745c19e1b2a4886ad548c8446ac6405f1e366c17873ac540a9ec641e98a45002cf5b4571
7
- data.tar.gz: 93ba27887a32e0a361187ef94189c6a574849f65fe5542a0f09fe329c1bfb42121675258ed97a8ce9b85136a9634902028869eb9e945b7c4163307556e3b7150
6
+ metadata.gz: e8da64fbec202205ff7771d222b26b4c1513467e2001f4d4997bc7f7f18e66390743ade3a68b253e684a67fcb58233d3c58c34e824e1ab528ade2260a0b9c252
7
+ data.tar.gz: 5246ea35123d5b4aa7748362b1ee89915450d56a433cfa1f62df9089081c62d63a8551ce6361d103d761096eb5564eb053ed8fcb15d9b96a7aa45b283e816fca
data/README.md CHANGED
@@ -1,117 +1,14 @@
1
- # Ops
2
-
3
- ## Description
4
- Ops is a Ruby gem designed to enhance and to backup your PostgreSQL database.
1
+ # OpsBackups
2
+ Short description and motivation.
5
3
 
6
4
  ## Usage
7
-
8
- ### Configuration
9
-
10
- To configure the Ops gem, you need to set up the following environment variables:
11
- - `DATABASE_URL`: The URL of the PostgreSQL database to backup.
12
-
13
- Set up Active Storage for backups (e.g., Amazon S3 for production). We expect a service named `:backup_storage` to be configured.
14
-
15
- ```yaml
16
- backup_storage:
17
- service: S3
18
- access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
19
- secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
20
- region: <%= ENV['AWS_REGION'] %>
21
- bucket: backups
22
- ```
23
- ### Backup Method
24
-
25
- The `Ops::Backup` class provides a method to perform a PostgreSQL database backup:
26
-
27
- - `db_pg_backup(exclude_tables: [], tag: nil)`: This method performs a full backup of the database, optionally excluding specified tables. The backup is tagged and saved as an attachment.
28
-
29
- example:
30
-
31
- ```ruby
32
- Ops::Backup.new.db_pg_backup(exclude_tables: ["versions"], tag: "db_pg_unveresioned")
33
- ```
34
-
35
- ### Backup Retention Strategies
36
-
37
- Ops gem provides two backup retention strategies:
38
-
39
- 1. **Tiered Cleanup Policy**:
40
- - Keeps all backups from the last day.
41
- - Keeps the last backup of each day from the last week (except the last day).
42
- - Keeps the last backup of each week from the last month (except the last week).
43
- - Keeps the last backup of each month before the last month.
44
-
45
- example:
46
- ```ruby
47
- Ops::CleanupTiered.new.cleanup("db_pg_unversioned")
48
- ```
49
-
50
- This policy can be applied using the `Ops::CleanupTieredJob`.
51
-
52
- 2. **Limit Cleanup Policy**:
53
- - Keeps the last N backups, where N is specified by the `limit` parameter.
54
-
55
- example:
56
- ```ruby
57
- Ops::CleanupLimit.new.cleanup("db_pg_unversioned", limit: 14)
58
- ```
59
-
60
- This policy can be applied using the `Ops::CleanupLimitJob`.
61
-
62
- ### Using SolidQueue Schedule
63
-
64
- 1. Schedule the backup job using SolidQueue:
65
- ```ruby
66
- SolidQueue.schedule(Ops::BackupDbJob.new, cron: '0 0 * * *', args: { tag: "db_pg_full", exclude_tables: [], cleanup: "retain_last_limit" }) # This schedules the backup job to run daily at midnight
67
- ```
68
-
69
- 2. Schedule the cleanup job with a tiered policy:
70
- ```ruby
71
- SolidQueue.schedule(Ops::CleanupTieredJob.new, cron: '0 1 * * *', args: { tag: "db_pg_full" }) # This schedules the tiered cleanup job to run daily at 1 AM
72
- ```
73
-
74
- 3. Schedule the cleanup job with a limit policy:
75
- ```ruby
76
- SolidQueue.schedule(Ops::CleanupLimitJob.new, cron: '0 2 * * *', args: { tag: "db_pg_full", limit: 14 }) # This schedules the limit cleanup job to run daily at 2 AM
77
- ```
78
-
79
- ### Using `recurring.yml` from SolidQueue
80
-
81
- Alternatively, you can configure recurring tasks using `recurring.yml` in your SolidQueue configuration:
82
-
83
- 1. Create or update the `recurring.yml` file in your application's config directory:
84
- ```yaml
85
- # filepath: config/recurring.yml
86
- backup_db_job:
87
- cron: "0 0 * * *"
88
- class: "Ops::BackupDbJob"
89
- args:
90
- tag: "db_pg_full"
91
- exclude_tables: []
92
- cleanup: "retain_last_limit"
93
-
94
- cleanup_tiered_job:
95
- cron: "0 1 * * *"
96
- class: "Ops::CleanupTieredJob"
97
- args:
98
- tag: "db_pg_full"
99
-
100
- cleanup_limit_job:
101
- cron: "0 2 * * *"
102
- class: "Ops::CleanupLimitJob"
103
- args:
104
- tag: "db_pg_full"
105
- limit: 14
106
- ```
107
-
108
-
5
+ How to use my plugin.
109
6
 
110
7
  ## Installation
111
8
  Add this line to your application's Gemfile:
112
9
 
113
10
  ```ruby
114
- gem "ops"
11
+ gem "ops_backups"
115
12
  ```
116
13
 
117
14
  And then execute:
@@ -121,12 +18,11 @@ $ bundle
121
18
 
122
19
  Or install it yourself as:
123
20
  ```bash
124
- $ gem install ops
21
+ $ gem install ops_backups
125
22
  ```
126
23
 
127
24
  ## Contributing
128
-
129
- Bug reports and pull requests are welcome on GitHub at https://github.com/koenhandekyn/ops.
25
+ Contribution directions go here.
130
26
 
131
27
  ## License
132
28
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,4 @@
1
+ module OpsBackups
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -1,4 +1,4 @@
1
- module Ops
1
+ module OpsBackups
2
2
  module ApplicationHelper
3
3
  end
4
4
  end
@@ -0,0 +1,4 @@
1
+ module OpsBackups
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -1,7 +1,7 @@
1
1
  # typed: true
2
2
  # frozen_string_literal: true
3
3
 
4
- class Ops::Backups::BackupDbJob < ApplicationJob
4
+ class OpsBackups::BackupDbJob < ApplicationJob
5
5
  queue_as :operations
6
6
 
7
7
  # perform a full backup of the database
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Cleanup job for limit backup policy
4
- class Ops::Backups::CleanupLimitJob < ApplicationJob
4
+ class OpsBackups::CleanupLimitJob < ApplicationJob
5
5
  queue_as :operations
6
6
 
7
7
  # @param [String] tag
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Cleanup job for tiered backup policy
4
- class Ops::Backups::CleanupTieredJob < ApplicationJob
4
+ class OpsBackups::CleanupTieredJob < ApplicationJob
5
5
  queue_as :operations
6
6
 
7
7
  # @param [String] tag
@@ -0,0 +1,6 @@
1
+ module OpsBackups
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: "from@example.com"
4
+ layout "mailer"
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module OpsBackups
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -1,9 +1,8 @@
1
- module Ops
2
- puts "Ops module loaded"
3
-
4
- class Ops::Backups::Backup < ApplicationRecord
1
+ module OpsBackups
2
+ class Backup < ApplicationRecord
5
3
  # has_one_attached :backup_file, service: :backup_storage
6
- has_one_attached :backup_file
4
+ has_one_attached :backup_file, service: :backups
5
+ self.table_name = "ops_backups"
7
6
 
8
7
  default_scope { order(updated_at: :desc) }
9
8
 
@@ -1,13 +1,13 @@
1
1
  <!DOCTYPE html>
2
2
  <html>
3
3
  <head>
4
- <title>Ops</title>
4
+ <title>Ops backups</title>
5
5
  <%= csrf_meta_tags %>
6
6
  <%= csp_meta_tag %>
7
7
 
8
8
  <%= yield :head %>
9
9
 
10
- <%= stylesheet_link_tag "ops/application", media: "all" %>
10
+ <%= stylesheet_link_tag "ops_backups/application", media: "all" %>
11
11
  </head>
12
12
  <body>
13
13
 
data/config/routes.rb CHANGED
@@ -1,2 +1,2 @@
1
- Ops::Backups::Engine.routes.draw do
1
+ OpsBackups::Engine.routes.draw do
2
2
  end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ bin/rails generate install Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,51 @@
1
+ class OpsBackups::InstallGenerator < Rails::Generators::Base
2
+ source_root File.expand_path("templates", __dir__)
3
+
4
+ desc "Copies migrations and adds a storage service configuration to storage.yml"
5
+
6
+ def copy_migrations
7
+ rake "railties:install:migrations FROM=ops_backups"
8
+ say "Migrations copied to your application.", :green
9
+ end
10
+
11
+ def add_storage_service
12
+ storage_file = Rails.root.join("config", "storage.yml")
13
+
14
+ if File.exist?(storage_file)
15
+ service_config = <<~YAML
16
+
17
+ backups:
18
+ service: Disk
19
+ root: <%= Rails.root.join("storage") %>
20
+ # service: S3
21
+ # access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
22
+ # secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
23
+ # region: <%= ENV['AWS_REGION'] %>
24
+ # bucket: allcrux-backups
25
+ YAML
26
+
27
+ append_to_file storage_file, service_config
28
+ say "Added 'backups' service to storage.yml.", :green
29
+ else
30
+ say "config/storage.yml not found. Please create it and re-run this generator.", :red
31
+ end
32
+ end
33
+
34
+ def add_recurring_jobs
35
+ recurring_file = Rails.root.join("config", "recurring.yml")
36
+
37
+ if File.exist?(recurring_file)
38
+ recurring_config = <<~YAML.lines.map { |line| "\t#{line}" }.join
39
+ backup_db:
40
+ class: OpsBackups::BackupDbJob
41
+ args: [tag: "db_pg_backup", cleanup: "retain_tiered_cleanup_policy"]
42
+ schedule: every hour
43
+ YAML
44
+
45
+ append_to_file recurring_file, recurring_config
46
+ say "Added 'recurring' jobs to recurring.yml.", :green
47
+ else
48
+ say "config/recurring.yml not found. Please create it and re-run this generator.", :red
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,11 @@
1
+ module OpsBackups
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace OpsBackups
4
+
5
+ initializer :append_migrations do |app|
6
+ unless app.root.to_s.match? root.to_s
7
+ app.config.paths["db/migrate"].concat(config.paths["db/migrate"].expanded)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module OpsBackups
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "ops_backups/version"
2
+ require "ops_backups/engine"
3
+
4
+ module OpsBackups
5
+ # Your code goes here...
6
+ end
@@ -1,4 +1,4 @@
1
1
  # desc "Explaining what the task does"
2
- # task :ops do
2
+ # task :ops_backups do
3
3
  # # Task goes here
4
4
  # end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ops_backups
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Koen Handekyn
@@ -35,22 +35,25 @@ files:
35
35
  - MIT-LICENSE
36
36
  - README.md
37
37
  - Rakefile
38
- - app/assets/stylesheets/ops/application.css
39
- - app/controllers/ops/backups/application_controller.rb
40
- - app/helpers/ops/application_helper.rb
41
- - app/jobs/ops/backups/application_job.rb
42
- - app/jobs/ops/backups/backup_db_job.rb
43
- - app/jobs/ops/backups/cleanup_limit_job.rb
44
- - app/jobs/ops/backups/cleanup_tiered_job.rb
45
- - app/models/ops/backups/application_record.rb
46
- - app/models/ops/backups/backup.rb
47
- - app/views/layouts/ops/application.html.erb
38
+ - app/assets/stylesheets/ops_backups/application.css
39
+ - app/controllers/ops_backups/application_controller.rb
40
+ - app/helpers/ops_backups/application_helper.rb
41
+ - app/jobs/ops_backups/application_job.rb
42
+ - app/jobs/ops_backups/backup_db_job.rb
43
+ - app/jobs/ops_backups/cleanup_limit_job.rb
44
+ - app/jobs/ops_backups/cleanup_tiered_job.rb
45
+ - app/mailers/ops_backups/application_mailer.rb
46
+ - app/models/ops_backups/application_record.rb
47
+ - app/models/ops_backups/backup.rb
48
+ - app/views/layouts/ops_backups/application.html.erb
48
49
  - config/routes.rb
49
50
  - db/migrate/20241114173612_create_ops_backups.rb
50
- - lib/ops/backups/backups.rb
51
- - lib/ops/backups/engine.rb
52
- - lib/ops/backups/version.rb
53
- - lib/tasks/ops_tasks.rake
51
+ - lib/generators/ops_backups/install/USAGE
52
+ - lib/generators/ops_backups/install/install_generator.rb
53
+ - lib/ops_backups.rb
54
+ - lib/ops_backups/engine.rb
55
+ - lib/ops_backups/version.rb
56
+ - lib/tasks/ops_backups_tasks.rake
54
57
  homepage: https://github.com/koenhandekyn/ops-backups
55
58
  licenses:
56
59
  - MIT
@@ -1,6 +0,0 @@
1
- module Ops
2
- module Backups
3
- class ApplicationController < ActionController::Base
4
- end
5
- end
6
- end
@@ -1,6 +0,0 @@
1
- module Ops
2
- module Backups
3
- class ApplicationJob < ActiveJob::Base
4
- end
5
- end
6
- end
@@ -1,8 +0,0 @@
1
- module Ops
2
- module Backups
3
- class ApplicationRecord < ActiveRecord::Base
4
- self.abstract_class = true
5
- self.table_name_prefix = "ops_"
6
- end
7
- end
8
- end
@@ -1,8 +0,0 @@
1
- require "ops/backups/version"
2
- require "ops/backups/engine"
3
-
4
- module Ops
5
- module Backups
6
- # Your code goes here...
7
- end
8
- end
@@ -1,7 +0,0 @@
1
- module Ops
2
- module Backups
3
- class Engine < ::Rails::Engine
4
- isolate_namespace Ops::Backups
5
- end
6
- end
7
- end
@@ -1,5 +0,0 @@
1
- module Ops
2
- module Backups
3
- VERSION = "0.1.0"
4
- end
5
- end