module CloudantBackup::Backup

Attributes

cloudant_host[RW]
date_pattern[RW]
host[RW]
password[RW]
source[RW]
target[RW]
user[RW]

Public Class Methods

new() click to toggle source
# File lib/cloudant_backup/backup.rb, line 9
def initialize
end

Public Instance Methods

backup() click to toggle source
# File lib/cloudant_backup/backup.rb, line 12
def backup
  create_target_db
  replicate
end
create_target_db() click to toggle source
# File lib/cloudant_backup/backup.rb, line 17
def create_target_db
  make_request(:put, target_db)
rescue RestClient::PreconditionFailed
  # When the db existed already, we're fine with that
end
host=(host) click to toggle source
# File lib/cloudant_backup/backup.rb, line 31
def host=(host)
  parts = host.split("://")
  if parts.length ==2
    @protocol, @host = host.split("://")
  else
    @protocol = "http"
    @host = host
  end
end
replicate() click to toggle source
# File lib/cloudant_backup/backup.rb, line 23
def replicate
  data = {
    source: source_db_url,
    target: target_db_url
  }.to_json
  make_request(:post, "_replicate", data)
end

Private Instance Methods

date_string() click to toggle source
# File lib/cloudant_backup/backup.rb, line 43
def date_string
  Date.today.strftime(@date_pattern || '%Y_%m_%d')
end
db_url(name) click to toggle source
# File lib/cloudant_backup/backup.rb, line 69
def db_url(name)
  if user
    "#{protocol}://#{user}:#{password}@#{host}/#{name}"
  else
    "#{protocol}://#{host}/#{name}"
  end
end
make_request(method, url, data = nil) click to toggle source
# File lib/cloudant_backup/backup.rb, line 81
def make_request(method, url, data = nil)
  options = {
    content_type: :json,
    accept:       :json
  }
  RestClient.send(*[method, db_url(url), data, options].compact)
end
protocol() click to toggle source
# File lib/cloudant_backup/backup.rb, line 77
def protocol
  @protocol || "https"
end
source_db_url() click to toggle source
# File lib/cloudant_backup/backup.rb, line 65
def source_db_url
  db_url(@source)
end
target_db() click to toggle source
# File lib/cloudant_backup/backup.rb, line 57
def target_db
  @target || "#{@source}-backup_#{date_string}"
end
target_db_url() click to toggle source
# File lib/cloudant_backup/backup.rb, line 61
def target_db_url
  db_url(target_db)
end
validate_options!() click to toggle source
# File lib/cloudant_backup/backup.rb, line 53
def validate_options!
  raise "Need to set source" unless @source
end