Module: Rails::Baklite
- Includes:
- ActiveSupport::Configurable
- Defined in:
- lib/rails/baklite.rb,
lib/rails/baklite/railtie.rb,
lib/rails/baklite/version.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
"1.1.2"
Class Method Summary collapse
- ._backup(sdb, backupfile, name = 'main') ⇒ Object
- .backup ⇒ Object
- .restore(backupfile, sdb, name = 'main') ⇒ Object
- .upload(backupfile) ⇒ Object
Class Method Details
._backup(sdb, backupfile, name = 'main') ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rails/baklite.rb', line 31 def self._backup(sdb, backupfile, name = 'main') ddb = SQLite3::Database.new(backupfile) b = SQLite3::Backup.new(ddb, name, sdb, name) p [b.remaining, b.pagecount] begin p b.step(1) p [b.remaining, b.pagecount] end while b.remaining > 0 b.finish ddb = SQLite3::Database.new(backupfile) b = SQLite3::Backup.new(ddb, name, sdb, name) b.step(-1) b.finish end |
.backup ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rails/baklite.rb', line 17 def self.backup db_config = ActiveRecord::Base.connection_db_config database = db_config.configuration_hash raise Error, 'Only SQLite3 databases are supported' if database[:adapter] != 'sqlite3' backupfile = "#{Rails.root}/tmp/database.bak" _backup(ActiveRecord::Base.connection.raw_connection, backupfile) upload(backupfile) backupfile end |
.restore(backupfile, sdb, name = 'main') ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/rails/baklite.rb', line 48 def self.restore(backupfile, sdb, name = 'main') ddb = SQLite3::Database.new(backupfile, readonly: true) b = SQLite3::Backup.new(sdb, name, ddb, name) begin b.step(1) end while b.remaining > 0 b.finish end |
.upload(backupfile) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rails/baklite.rb', line 58 def self.upload(backupfile) raise Error, 'No token provided' if Rails::Baklite.token.empty? name = Rails::Baklite.config.name || Rails.application.class.module_parent_name addr = Rails.env.test? ? 'http://localhost:3000' : 'https://baklite.tortitas.eu' uri = URI.parse("#{addr}/databases/snapshots") File.open(backupfile) do |file| req = Net::HTTP::Post.new(uri.path) req['Authorization'] = "Bearer #{Rails::Baklite.token}" req.set_form [['file', file], ['name', name]], 'multipart/form-data' res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http| http.request(req) end raise Error, "Failed to upload the backup: #{res.code} #{res.body}" if res.code != '201' end end |