archivault 0.2.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eaf47c3cfc367a25c1ea685a22229738230844f458187e7094bbb1892a80c67d
4
- data.tar.gz: ce10a4fa42f6c2e9b2a52938f11884e82828351ccfedb540bce4402b747334a3
3
+ metadata.gz: a4ec8f7f88e54372cf70eb0e07e637c34561ddc888ec4ef4ccb53275c64d3993
4
+ data.tar.gz: 8b89b5f12ae91effaad05cf5add3ce8a0e58bd43cce1ac0d7482505f751ac464
5
5
  SHA512:
6
- metadata.gz: ca08c17fb2a4f21a0bc2e200fe9ea931b60b5057856b38e2725fe57b2de37f424a35f2cb181052f4dda613a1f45547fd10fc08ba04ce27d3135a02c5dd404109
7
- data.tar.gz: c1ab9e98d5b9ca7072d18cfe352f32d2074f6401c28b5c199cab83a9d9f076a8e186878ce63466ae823aaa87cd79142a75fcb464f358054349978403e1227e3e
6
+ metadata.gz: 03cf209ecb7fbc7f1bb6af70ecc290d9ca302aa63bd6c4d07c07ad0605d0cccf42d44680a5ca3beb3ae80e63299aa965177b708ca1bc22015bdb041f319b0706
7
+ data.tar.gz: c3232861b94738f32449782c035df357d4ef2ab94859c848821b7ec8af450daf490a93e2a0543a79978d3c8e1628c570a48ec6421ce7fbd0848e265b5f558d6f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ - Add an unguessable random suffix to backup file names, so a PutObject-only S3 key cannot target (overwrite) existing backups
4
+ - Pass decoded PostgreSQL URL passwords to pg_dump through PGPASSWORD instead of command-line arguments, and prevent password leaks in invalid URL errors
5
+ - Allow Execute to forward environment variables and system options
6
+
7
+ ## [0.2.1] - 2026-09-03
8
+
9
+ - Allow custom database backup filename prefixes
10
+
3
11
  ## [0.2.0] - 2026-04-10
4
12
 
5
13
  - Add PostgresqlBackup
data/README.md CHANGED
@@ -32,8 +32,9 @@ s3_setup = {
32
32
  bucket: "bucket"
33
33
  }
34
34
  ping_url = "https://example.com/ping" # optional parameter
35
+ filename = "production" # optional prefix; timestamp is appended
35
36
 
36
- Archivault::SqliteBackup.new(database_path:, gpg_passphrase:, s3_setup:, ping_url:).call
37
+ Archivault::SqliteBackup.new(database_path:, gpg_passphrase:, s3_setup:, ping_url:, filename:).call
37
38
  ```
38
39
 
39
40
  ### PostgreSQL database backup
@@ -48,8 +49,9 @@ s3_setup = {
48
49
  bucket: "bucket"
49
50
  }
50
51
  ping_url = "https://example.com/ping" # optional parameter
52
+ filename = "production" # optional prefix; timestamp is appended
51
53
 
52
- Archivault::PostgresqlBackup.new(database_url:, gpg_passphrase:, s3_setup:, ping_url:).call
54
+ Archivault::PostgresqlBackup.new(database_url:, gpg_passphrase:, s3_setup:, ping_url:, filename:).call
53
55
  ```
54
56
 
55
57
  ### Logs backup
@@ -86,7 +88,8 @@ Archivault::LogsBackup.new(log_path_or_paths:, gpg_passphrase:, s3_setup:, ping_
86
88
  # bucket: bucket
87
89
  params = {
88
90
  database_path: Rails.root.join("storage/production.sqlite3"),
89
- ping_url: "https://example.com/ping" # optional parameter
91
+ ping_url: "https://example.com/ping", # optional parameter
92
+ filename: "production" # optional prefix; timestamp is appended
90
93
  }.merge(Rails.application.credentials.archivault)
91
94
  Archivault::SqliteBackup.new(**params).call
92
95
  ```
@@ -104,7 +107,8 @@ Archivault::SqliteBackup.new(**params).call
104
107
  # bucket: bucket
105
108
  params = {
106
109
  database_url: ENV["DATABASE_URL"],
107
- ping_url: "https://example.com/ping" # optional parameter
110
+ ping_url: "https://example.com/ping", # optional parameter
111
+ filename: "production" # optional prefix; timestamp is appended
108
112
  }.merge(Rails.application.credentials.archivault)
109
113
  Archivault::PostgresqlBackup.new(**params).call
110
114
  ```
@@ -4,8 +4,8 @@ require "English"
4
4
 
5
5
  module Archivault
6
6
  class Execute
7
- def call(command, *)
8
- status = system(command, *)
7
+ def call(command, *, env: {}, **)
8
+ status = system(env, command, *, **)
9
9
  raise ExecuteError, "#{command} could not be executed" if status.nil?
10
10
  raise ExecuteError, "#{command} failed with exit code #{$CHILD_STATUS&.exitstatus}" unless status
11
11
 
@@ -16,9 +16,8 @@ module Archivault
16
16
  @s3_setup = s3_setup
17
17
  @ping_url = ping_url
18
18
 
19
- @timestamp = Timestamp.new.timestamp
20
19
  @tmp_path = Tmp.new.path
21
- @tar_path = "#{@tmp_path}/logs-#{@timestamp}.tgz"
20
+ @tar_path = "#{@tmp_path}/#{Timestamp.new.backup_name("logs")}.tgz"
22
21
  @gpg_path = "#{@tar_path}.gpg"
23
22
  end
24
23
 
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # To restore a database from a dump file, use:
4
- # pg_restore --no-owner -d DATABASE_URL database.dump
4
+ # pg_restore --clean --if-exists --no-owner --no-acl --role=db-role -U postgres -d db-name /tmp/db.dump
5
5
  module Archivault
6
6
  class PgDump
7
7
  def initialize(database_url:, backup_path:)
@@ -12,8 +12,23 @@ module Archivault
12
12
  @backup_path = backup_path.to_s
13
13
  end
14
14
 
15
+ # The password goes to pg_dump through PGPASSWORD, not through the command line:
16
+ # arguments of a running process are readable by every local user (ps, /proc/<pid>/cmdline).
15
17
  def call
16
- Execute.new.call("pg_dump", "-Fc", "-f", @backup_path, @database_url)
18
+ uri = parse_database_url
19
+ password = URI.decode_uri_component(uri.password) if uri.password && !uri.password.empty?
20
+ uri.password = nil
21
+ env = password ? { "PGPASSWORD" => password } : {}
22
+ Execute.new.call("pg_dump", "-Fc", "-f", @backup_path, uri.to_s, env: env)
23
+ end
24
+
25
+ private
26
+
27
+ def parse_database_url
28
+ URI.parse(@database_url)
29
+ rescue URI::InvalidURIError
30
+ # URI::InvalidURIError quotes the whole input, password included - re-raise without it.
31
+ raise "Invalid database URL: percent-encode special characters in the password", cause: nil
17
32
  end
18
33
  end
19
34
  end
@@ -10,16 +10,17 @@ module Archivault
10
10
  # - access_key_id: AWS access key ID
11
11
  # - secret_access_key: AWS secret access key
12
12
  # - bucket: S3 bucket name
13
- def initialize(database_url:, gpg_passphrase:, s3_setup:, ping_url: nil)
13
+ # - filename: Filename prefix for the backup (defaults to database)
14
+ def initialize(database_url:, gpg_passphrase:, s3_setup:, ping_url: nil, filename: nil)
14
15
  @database_url = database_url.to_s
15
16
  @gpg_passphrase = gpg_passphrase.to_s
16
17
  @s3_setup = s3_setup
17
18
  @ping_url = ping_url
18
19
 
19
- @timestamp = Timestamp.new.timestamp
20
20
  @tmp_path = Tmp.new.path
21
- @backup_path = "#{@tmp_path}/database-#{@timestamp}.dump"
22
- @tar_path = "#{@tmp_path}/database-#{@timestamp}.tgz"
21
+ backup_filename = Timestamp.new.backup_name(filename || "database")
22
+ @backup_path = "#{@tmp_path}/#{backup_filename}.dump"
23
+ @tar_path = "#{@tmp_path}/#{backup_filename}.tgz"
23
24
  @gpg_path = "#{@tar_path}.gpg"
24
25
  end
25
26
 
@@ -10,16 +10,17 @@ module Archivault
10
10
  # - access_key_id: AWS access key ID
11
11
  # - secret_access_key: AWS secret access key
12
12
  # - bucket: S3 bucket name
13
- def initialize(database_path:, gpg_passphrase:, s3_setup:, ping_url: nil)
13
+ # - filename: Filename prefix for the backup (defaults to database)
14
+ def initialize(database_path:, gpg_passphrase:, s3_setup:, ping_url: nil, filename: nil)
14
15
  @database_path = database_path.to_s
15
16
  @gpg_passphrase = gpg_passphrase.to_s
16
17
  @s3_setup = s3_setup
17
18
  @ping_url = ping_url
18
19
 
19
- @timestamp = Timestamp.new.timestamp
20
20
  @tmp_path = Tmp.new.path
21
- @backup_path = "#{@tmp_path}/database-#{@timestamp}.sqlite3"
22
- @tar_path = "#{@tmp_path}/database-#{@timestamp}.tgz"
21
+ backup_filename = Timestamp.new.backup_name(filename || "database")
22
+ @backup_path = "#{@tmp_path}/#{backup_filename}.sqlite3"
23
+ @tar_path = "#{@tmp_path}/#{backup_filename}.tgz"
23
24
  @gpg_path = "#{@tar_path}.gpg"
24
25
  end
25
26
 
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "securerandom"
4
+
3
5
  module Archivault
4
6
  class Timestamp
5
7
  attr_reader :timestamp
@@ -7,5 +9,11 @@ module Archivault
7
9
  def initialize
8
10
  @timestamp = Time.now.strftime("%Y-%m-%d-%H-%M")
9
11
  end
12
+
13
+ # The backup S3 key may only PutObject, which can also overwrite an existing object.
14
+ # It can't list the bucket, so an unguessable suffix keeps it from targeting existing backups.
15
+ def backup_name(prefix)
16
+ "#{prefix}-#{timestamp}-#{SecureRandom.hex(8)}"
17
+ end
10
18
  end
11
19
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Archivault
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: archivault
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Macuk