noop-backup 0.1.0
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 +7 -0
- data/CHANGELOG.md +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +94 -0
- data/Rakefile +8 -0
- data/exe/nbu +6 -0
- data/lib/noop_backup/cli.rb +13 -0
- data/lib/noop_backup/commands/backup.rb +94 -0
- data/lib/noop_backup/configuration.rb +54 -0
- data/lib/noop_backup/notifiers/slack.rb +29 -0
- data/lib/noop_backup/notifiers/stdout.rb +7 -0
- data/lib/noop_backup/version.rb +3 -0
- data/lib/noop_backup.rb +28 -0
- metadata +87 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: d3039d98fa3edb9366c1fe2e41f0f1ddfa999c0ef088d265ac256de69a835b7b
|
|
4
|
+
data.tar.gz: 352a209a61c3b8dec730308dba8ca43813e3444d9664db7941ef54b08caf3f00
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 5e6912b20074c3a938aebb51b4c55a03b6c0c4134b8b9b4c384d32031eacb73f5a7c9952a7079fbcbb8f2b37745a7450dbe2076f458509794e84dbc973006c47
|
|
7
|
+
data.tar.gz: 7fc6e19dbc757daf152b9c534274406c2005e25fd489ce5c0dd958185c22619e9842979d8ad35b9a420ef8a038801879061b85c4a9bff4a576363ed5cf2384ee
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Georgi Mitrev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# NoopBackup
|
|
2
|
+
|
|
3
|
+
Ruby gem for backing up PostgreSQL databases with minimum effort.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
If using bundler, add the gem to your `Gemfile`:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
bundle add noop-backup
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
gem install noop-backup
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The gem requires `pg_dump` to be installed on the machine that is running it.
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### Automatic
|
|
24
|
+
|
|
25
|
+
#### Rails 8 with Solid Queue
|
|
26
|
+
|
|
27
|
+
(soon) NoopBackup will run automatically using the Solid Queue scheduler.
|
|
28
|
+
|
|
29
|
+
#### Sidekiq, Good Job, whenever
|
|
30
|
+
|
|
31
|
+
(soon)
|
|
32
|
+
|
|
33
|
+
### Manually
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
bundle exec nbu backup
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
This command will dump the database and stream it to S3 without writing anything to disk. Use any
|
|
40
|
+
scheduler or even cron to run it periodically.
|
|
41
|
+
|
|
42
|
+
## Configuration
|
|
43
|
+
|
|
44
|
+
The gem needs AWS credentials and a bucket name:
|
|
45
|
+
|
|
46
|
+
```
|
|
47
|
+
AWS_REGION=region
|
|
48
|
+
AWS_ACCESS_KEY_ID=your-key
|
|
49
|
+
AWS_SECRET_ACCESS_KEY=your-secret-key
|
|
50
|
+
NBU_BUCKET=s3-bucket
|
|
51
|
+
NBU_MIN_SIZE=2048
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
If your app already has the AWS SDK set up, only the bucket needs to be configured.
|
|
55
|
+
|
|
56
|
+
Alternatively, the gem can be configured with an initializer:
|
|
57
|
+
|
|
58
|
+
```rb
|
|
59
|
+
# config/initializers/noop-backup.rb
|
|
60
|
+
|
|
61
|
+
NoopBackup.configure do |config|
|
|
62
|
+
config.bucket = 'bucket-name'
|
|
63
|
+
config.region = 'eu-central-1'
|
|
64
|
+
config.prefix = Rails.env
|
|
65
|
+
config.min_size = 2048
|
|
66
|
+
|
|
67
|
+
config.notifier :slack do |slack|
|
|
68
|
+
slack.webhook_url = 'https://hooks.slack.com/services/whatever'
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Development
|
|
74
|
+
|
|
75
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
76
|
+
|
|
77
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
78
|
+
|
|
79
|
+
## Contributing
|
|
80
|
+
|
|
81
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/gmitrev/noop-backup.
|
|
82
|
+
|
|
83
|
+
## Wishlist
|
|
84
|
+
|
|
85
|
+
- [ ] S3-compatible backends
|
|
86
|
+
- [ ] file backends
|
|
87
|
+
- [ ] email notifier
|
|
88
|
+
- [ ] SMS notifier
|
|
89
|
+
- [ ] restore command
|
|
90
|
+
- [ ] test command
|
|
91
|
+
|
|
92
|
+
## License
|
|
93
|
+
|
|
94
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/exe/nbu
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
require "aws-sdk-s3"
|
|
2
|
+
require "open3"
|
|
3
|
+
|
|
4
|
+
module NoopBackup::Commands
|
|
5
|
+
class Backup
|
|
6
|
+
def self.execute
|
|
7
|
+
new.execute
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def initialize
|
|
11
|
+
@key = [config.prefix, Time.now.utc.strftime("%Y%m%dT%H%M%SZ")].join("/")
|
|
12
|
+
@bytes = 0
|
|
13
|
+
@started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def execute
|
|
17
|
+
commands = [
|
|
18
|
+
[config.pg_env, "pg_dump", "--format=custom", "--no-owner"]
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
commands << ["pv", "-btra"] if system("which", "pv", out: File::NULL, err: File::NULL)
|
|
22
|
+
|
|
23
|
+
Open3.pipeline_r(*commands) do |last_stdout, wait_threads|
|
|
24
|
+
upload(last_stdout)
|
|
25
|
+
|
|
26
|
+
raise "pipeline failed" unless wait_threads.all? { |t| t.value.success? }
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - @started
|
|
30
|
+
|
|
31
|
+
if config.min_size && @bytes.to_i < config.min_size.to_i
|
|
32
|
+
raise "backup too small: #{human_size(@bytes)} < min_size (#{human_size(config.min_size)})"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
config.notify(success_message(duration))
|
|
36
|
+
rescue => e
|
|
37
|
+
remove_partial_upload
|
|
38
|
+
|
|
39
|
+
config.notify("❌ Backup failed: #{e.message}")
|
|
40
|
+
|
|
41
|
+
exit 1
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
def config
|
|
47
|
+
@config ||= NoopBackup.configuration
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def s3_client
|
|
51
|
+
@_s3_client ||= Aws::S3::Client.new(region: config.region)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Prefer Aws::S3::TransferManager for streaming uploads if available.
|
|
55
|
+
# Aws::S3::Resource.upload_stream is deprecated in newer versions
|
|
56
|
+
def upload(stdout)
|
|
57
|
+
if defined?(Aws::S3::TransferManager)
|
|
58
|
+
manager = Aws::S3::TransferManager.new(client: s3_client)
|
|
59
|
+
|
|
60
|
+
manager.upload_stream(bucket: config.bucket, key: @key, part_size: 8 * 1024 * 1024, thread_count: 2) do |s3_stream|
|
|
61
|
+
@bytes = IO.copy_stream(stdout, s3_stream)
|
|
62
|
+
end
|
|
63
|
+
else
|
|
64
|
+
object = Aws::S3::Resource.new(region: config.region).bucket(config.bucket).object(@key)
|
|
65
|
+
|
|
66
|
+
object.upload_stream(part_size: 8 * 1024 * 1024, thread_count: 2) do |s3_stream|
|
|
67
|
+
@bytes = IO.copy_stream(stdout, s3_stream)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def human_size(bytes)
|
|
73
|
+
units = %w[B KB MB GB TB]
|
|
74
|
+
size = bytes.to_f
|
|
75
|
+
i = 0
|
|
76
|
+
while size >= 1024 && i < units.size - 1
|
|
77
|
+
size /= 1024
|
|
78
|
+
i += 1
|
|
79
|
+
end
|
|
80
|
+
format("%.1f %s", size, units[i])
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def success_message(duration)
|
|
84
|
+
"✅ #{config.pg_env["PGDATABASE"]} backed up successfully — " \
|
|
85
|
+
"#{human_size(@bytes)} in #{duration.round(1)}s → #{config.bucket}/#{@key}"
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def remove_partial_upload
|
|
89
|
+
s3_client.delete_object(bucket: config.bucket, key: @key)
|
|
90
|
+
rescue => e
|
|
91
|
+
warn "Failed to clean up partial upload #{@key}: #{e.message}"
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
module NoopBackup
|
|
2
|
+
class Configuration
|
|
3
|
+
attr_accessor :bucket,
|
|
4
|
+
:region,
|
|
5
|
+
:prefix,
|
|
6
|
+
:pg_host,
|
|
7
|
+
:pg_port,
|
|
8
|
+
:pg_user,
|
|
9
|
+
:pg_password,
|
|
10
|
+
:pg_database,
|
|
11
|
+
:min_size
|
|
12
|
+
|
|
13
|
+
def initialize
|
|
14
|
+
@bucket = ENV["NBU_BUCKET"]
|
|
15
|
+
@region = ENV["AWS_REGION"] || "auto"
|
|
16
|
+
@prefix = "backups"
|
|
17
|
+
@notifiers = [NoopBackup::Notifiers::Stdout.new]
|
|
18
|
+
@min_size = ENV.fetch("NBU_MIN_SIZE", 1024).to_i
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def notifier(type)
|
|
22
|
+
case type
|
|
23
|
+
when :slack
|
|
24
|
+
notifier = NoopBackup::Notifiers::Slack.new
|
|
25
|
+
|
|
26
|
+
yield notifier
|
|
27
|
+
|
|
28
|
+
@notifiers << notifier
|
|
29
|
+
else raise "Unknown notifier: #{type}"
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def notify(message)
|
|
34
|
+
@notifiers.each do |notifier|
|
|
35
|
+
notifier.notify(message)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def pg_env
|
|
40
|
+
{
|
|
41
|
+
"PGHOST" => pg_host || db_config[:host]&.to_s || "localhost",
|
|
42
|
+
"PGPORT" => pg_port || db_config[:port]&.to_s,
|
|
43
|
+
"PGUSER" => pg_user || db_config[:username]&.to_s,
|
|
44
|
+
"PGPASSWORD" => pg_password || db_config[:password]&.to_s,
|
|
45
|
+
"PGDATABASE" => pg_database || db_config[:database].to_s
|
|
46
|
+
}.compact
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def db_config
|
|
50
|
+
@db_config ||= defined?(::ActiveRecord) ?
|
|
51
|
+
::ActiveRecord::Base.connection_db_config.configuration_hash : {}
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require "net/http"
|
|
2
|
+
require "uri"
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module NoopBackup::Notifiers
|
|
6
|
+
class Slack
|
|
7
|
+
attr_accessor :webhook_url
|
|
8
|
+
|
|
9
|
+
def notify(text)
|
|
10
|
+
uri = URI(webhook_url)
|
|
11
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
12
|
+
http.use_ssl = true
|
|
13
|
+
|
|
14
|
+
request = Net::HTTP::Post.new(uri.path, "Content-Type" => "application/json")
|
|
15
|
+
request.body = {text: text}.to_json
|
|
16
|
+
|
|
17
|
+
response = http.request(request)
|
|
18
|
+
|
|
19
|
+
success = response.is_a?(Net::HTTPSuccess)
|
|
20
|
+
|
|
21
|
+
warn "Slack notify failed: #{response.code} #{response.body}" unless success
|
|
22
|
+
|
|
23
|
+
success
|
|
24
|
+
rescue => e
|
|
25
|
+
# Never fail a backup because of a notification error
|
|
26
|
+
warn "Slack notify error: #{e.message}"
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
data/lib/noop_backup.rb
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
require_relative "noop_backup/version"
|
|
2
|
+
require_relative "noop_backup/configuration"
|
|
3
|
+
require_relative "noop_backup/commands/backup"
|
|
4
|
+
require_relative "noop_backup/notifiers/slack"
|
|
5
|
+
require_relative "noop_backup/notifiers/stdout"
|
|
6
|
+
|
|
7
|
+
module NoopBackup
|
|
8
|
+
class Error < StandardError; end
|
|
9
|
+
|
|
10
|
+
class << self
|
|
11
|
+
def configuration
|
|
12
|
+
@configuration ||= Configuration.new
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
alias_method :config, :configuration
|
|
16
|
+
|
|
17
|
+
def configure
|
|
18
|
+
yield(configuration)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Attempt to boot host app. Booting it should trigger the configuration process.
|
|
22
|
+
def prepare!
|
|
23
|
+
env_file = File.expand_path("config/environment.rb")
|
|
24
|
+
|
|
25
|
+
require env_file if File.exist?(env_file)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: noop-backup
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Georgi Mitrev
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: thor
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: aws-sdk-s3
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '1'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '1'
|
|
40
|
+
description: The simplest way to add recurring database backups to your project with
|
|
41
|
+
minimal setup required.
|
|
42
|
+
email:
|
|
43
|
+
- gvmitrev@gmail.com
|
|
44
|
+
executables:
|
|
45
|
+
- nbu
|
|
46
|
+
extensions: []
|
|
47
|
+
extra_rdoc_files: []
|
|
48
|
+
files:
|
|
49
|
+
- CHANGELOG.md
|
|
50
|
+
- LICENSE.txt
|
|
51
|
+
- README.md
|
|
52
|
+
- Rakefile
|
|
53
|
+
- exe/nbu
|
|
54
|
+
- lib/noop_backup.rb
|
|
55
|
+
- lib/noop_backup/cli.rb
|
|
56
|
+
- lib/noop_backup/commands/backup.rb
|
|
57
|
+
- lib/noop_backup/configuration.rb
|
|
58
|
+
- lib/noop_backup/notifiers/slack.rb
|
|
59
|
+
- lib/noop_backup/notifiers/stdout.rb
|
|
60
|
+
- lib/noop_backup/version.rb
|
|
61
|
+
homepage: https://github.com/gmitrev/noop-backup
|
|
62
|
+
licenses:
|
|
63
|
+
- MIT
|
|
64
|
+
metadata:
|
|
65
|
+
homepage_uri: https://github.com/gmitrev/noop-backup
|
|
66
|
+
source_code_uri: https://github.com/gmitrev/noop-backup/tree/main
|
|
67
|
+
changelog_uri: https://github.com/gmitrev/noop-backup/blob/main/CHANGELOG.md
|
|
68
|
+
rubygems_mfa_required: 'true'
|
|
69
|
+
allowed_push_host: https://rubygems.org
|
|
70
|
+
rdoc_options: []
|
|
71
|
+
require_paths:
|
|
72
|
+
- lib
|
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
74
|
+
requirements:
|
|
75
|
+
- - ">="
|
|
76
|
+
- !ruby/object:Gem::Version
|
|
77
|
+
version: 3.1.0
|
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '0'
|
|
83
|
+
requirements: []
|
|
84
|
+
rubygems_version: 4.0.10
|
|
85
|
+
specification_version: 4
|
|
86
|
+
summary: Hassle-free database backups
|
|
87
|
+
test_files: []
|