sqlite_backups 0.1.0 → 0.2.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: 5befa74d4c48692314c28f8ca62ba631a87faaff37c3ee83ee792267127bad8b
4
- data.tar.gz: 04e12d29cc5c7285487fd63374d598d5d2e687c113f2dd560a556701d4a80d40
3
+ metadata.gz: a3496932c9ee0f9e5a993e464fe56ac638b856731709154ef5ba5fd46b2c9523
4
+ data.tar.gz: a106d4690c2825d8c0333bc88f35b6eb8b187c2e5ba845a5d8311c2ebc44b205
5
5
  SHA512:
6
- metadata.gz: 6f610817967e118afb908977f1d14c06cacf0400ed9cb202792914181326430f6a8f219f4f8eb74990d04430c3d09d29035ca66ca9c03ce1972564dbbcd0a9b1
7
- data.tar.gz: 6bd671346cc80c2a21a160d32e74b2d09a20a2b91645416edbeea89490979edd1b72e9177d36631b006d39d999e1054c43078e551b18f77b6c8d861851a5f207
6
+ metadata.gz: 287eb0b1e1953cbe7c053e5bb95c012ec1721f0a4707e9c88689d98cc35d5381eb15195a74e305c3f0dc76cb2339afab02322071b7d9ee4aa37ee82f57d1c307
7
+ data.tar.gz: a253b37d81bbbc79bcdc4ee47ce4c8d0ea7d5c147b423fee9247b186db9fcc30592149d184f5c7540383b3eb1fa074fb8374386d1e21591d29b12155665806f7
@@ -4,7 +4,7 @@ module Backups
4
4
 
5
5
  def show
6
6
  files = Backup.where(database: params[:name]).map do
7
- { date: it.created_at, key: it.file.key }
7
+ { date: it.created_at, key: it.file.key, size: it.file.byte_size }
8
8
  end
9
9
 
10
10
  render json: { name: params[:name], files: }
@@ -10,19 +10,58 @@ module Backups
10
10
  raise StandardError, "No backups found" if backups["files"].blank?
11
11
  raise StandardError, "File not found" unless service.exist?(key)
12
12
 
13
- File.open(path, "wb") do |file|
14
- file.write(ActiveSupport::Gzip.decompress(service.download(key)))
15
- end
13
+ download
14
+ decompress
15
+
16
+ File.delete(path) if File.exist?(path)
17
+ FileUtils.mv(download_path, path)
18
+ File.delete("#{download_path}.gz") if File.exist?("#{download_path}.gz")
19
+ File.delete(download_path) if File.exist?(download_path)
20
+
21
+ puts "Restore complete!"
16
22
  end
17
23
 
18
24
  private
19
25
 
20
26
  attr_reader :name
21
27
 
28
+ def download
29
+ return if File.exist?("#{download_path}.gz") || File.exist?(download_path)
30
+
31
+ downloaded_total = 0
32
+ File.open("#{download_path}.gz", "wb") do |file|
33
+ service.download(key) do |chunk|
34
+ file.write(chunk)
35
+ downloaded_total += chunk.bytesize
36
+ print "\rDownloading: #{((downloaded_total / size.to_f) * 100.0).to_i}%"
37
+ end
38
+ end
39
+ puts
40
+ end
41
+
42
+ def decompress
43
+ return if File.exist?(download_path)
44
+
45
+ puts "Decompressing..."
46
+ File.open("#{download_path}.gz", "rb") do |file|
47
+ File.open(download_path, "wb") do |out|
48
+ Zlib::GzipReader.wrap(file) do |gz|
49
+ out.write(gz.read)
50
+ end
51
+ end
52
+ end
53
+
54
+ File.delete("#{download_path}.gz")
55
+ end
56
+
22
57
  def path
23
58
  Backups.databases(env_name: Rails.env)[name.to_s]
24
59
  end
25
60
 
61
+ def download_path
62
+ Rails.root.join("tmp/#{key}")
63
+ end
64
+
26
65
  def service
27
66
  ActiveStorage::Blob.services.fetch(Backups.storage_service)
28
67
  end
@@ -32,14 +71,22 @@ module Backups
32
71
  end
33
72
 
34
73
  def key
35
- @key ||=
74
+ backup_file[:key]
75
+ end
76
+
77
+ def size
78
+ backup_file[:size]
79
+ end
80
+
81
+ def backup_file
82
+ @backup_file ||=
36
83
  if backups["files"].count == 1
37
- backups["files"].first["key"]
84
+ backups["files"].first
38
85
  else
39
86
  CLI::UI.ask("Pick a backup to restore", options:).then do |date|
40
- backups["files"].find { it["date"].to_s == date }["key"]
87
+ backups["files"].find { it["date"].to_s == date }
41
88
  end
42
- end
89
+ end.to_h.with_indifferent_access
43
90
  end
44
91
 
45
92
  def options
@@ -1,3 +1,3 @@
1
1
  module Backups
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqlite_backups
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Pezza