Class: SFRest::Backup

Inherits:
Object
  • Object
show all
Defined in:
lib/sfrest/backup.rb

Overview

Backup a site or restore onto that site

Instance Method Summary collapse

Constructor Details

#initialize(conn) ⇒ Backup

Returns a new instance of Backup.

Parameters:



7
8
9
# File 'lib/sfrest/backup.rb', line 7

def initialize(conn)
  @conn = conn
end

Instance Method Details

#backup_url(site_id, backup_id, lifetime = 60) ⇒ Object

Gets a url to download a backup

Parameters:

  • site_id (Integer)

    Node id of site

  • backup_id (Integer)

    Id of backup to delete

  • lifetime (Integer) (defaults to: 60)

    TTL of the url



91
92
93
# File 'lib/sfrest/backup.rb', line 91

def backup_url(site_id, backup_id, lifetime = 60)
  @conn.get("/api/v1/sites/#{site_id}/backups/#{backup_id}/url?lifetime=#{lifetime}")
end

#create_backup(site_id, datum = nil) ⇒ Object

Backs up a site.

Parameters:

  • site_id (Integer)
  • datum (Hash) (defaults to: nil)

    Options to the backup

Options Hash (datum):

  • 'label' (String)
  • 'callback_url' (Url)
  • 'callback_method' (String)

    GET|POST

  • 'caller_data' (Json)

    json encoded string



35
36
37
38
# File 'lib/sfrest/backup.rb', line 35

def create_backup(site_id, datum = nil)
  current_path = "/api/v1/sites/#{site_id}/backup"
  @conn.post(current_path, datum.to_json)
end

#delete_backup(site_id, backup_id) ⇒ Object

Deletes a site backup.

Parameters:

  • site_id (Integer)

    Node id of site

  • backup_id (Integer)

    Id of backup to delete



23
24
25
26
# File 'lib/sfrest/backup.rb', line 23

def delete_backup(site_id, backup_id)
  current_path = "/api/v1/sites/#{site_id}/backups/#{backup_id}"
  @conn.delete(current_path)
end

#execute_backup(options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sfrest/backup.rb', line 40

def execute_backup(options = {})
  api = options[:api]
  rconn = options[:rconn]
  acsf = options[:acsf]
  site_nid = options[:site_nid]
  name = options[:name]
  components = options[:components]
  user = options[:user]

  case api
  when 'rest'
    execute_rest_backup(rconn, site_nid, name, components)
  when 'drush'
    execute_drush_backup(acsf, site_nid, name, components, user)
  else
    raise "Unsupported API: #{api}"
  end
end

#execute_drush_backup(acsf, site_nid, name, components, user) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/sfrest/backup.rb', line 67

def execute_drush_backup(acsf, site_nid, name, components, user)
  drush_components = components.is_a?(Array) ? components.join(',') : components
  drush_cmd = "sf-backup #{site_nid} \"#{name}\" --components=\"#{drush_components}\" --user=#{user} --format=json"
  drush_cmd_update = acsf.drush drush_cmd
  result = acsf.factory_ssh.exec!(drush_cmd_update).strip
  JSON.parse(result)
end

#execute_rest_backup(rconn, site_nid, name, components) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/sfrest/backup.rb', line 59

def execute_rest_backup(rconn, site_nid, name, components)
  payload = {
    'label' => name,
    'components' => components
  }.to_json
  rconn.post "/api/v1/sites/#{site_nid}/backup", payload
end

#expiration_getHash

Gets the current the backup expiration and automatic deletion setting.

Returns:

  • (Hash)


106
107
108
# File 'lib/sfrest/backup.rb', line 106

def expiration_get
  @conn.get('/api/v1/backup-expiration/')
end

#expiration_set(days) ⇒ Object

Configures the backup expiration and automatic deletion setting.

Parameters:

  • days (Integer)

    Number of days after which backups are deleted.



97
98
99
100
101
102
# File 'lib/sfrest/backup.rb', line 97

def expiration_set(days)
  payload = {
    'expiration_days' => days
  }
  @conn.put('/api/v1/backup-expiration/', payload.to_json)
end

#get_backups(site_id, datum = nil) ⇒ Hash

cool stuff goes here

Parameters:

  • site_id (Integer)

    the node id of the site node

Returns:

  • (Hash)


14
15
16
17
18
# File 'lib/sfrest/backup.rb', line 14

def get_backups(site_id, datum = nil)
  current_path = "/api/v1/sites/#{site_id}/backups"
  pb = SFRest::Pathbuilder.new
  @conn.get URI.parse(pb.build_url_query(current_path, datum)).to_s
end

#parse_components(components_json) ⇒ Object



81
82
83
84
85
# File 'lib/sfrest/backup.rb', line 81

def parse_components(components_json)
  JSON.parse(components_json)
rescue JSON::ParserError
  components_json # Keep the original string if it's not valid JSON
end

#parse_response(response) ⇒ Object



75
76
77
78
79
# File 'lib/sfrest/backup.rb', line 75

def parse_response(response)
  raise "Unexpected response type: #{response.class}" unless response.is_a?(Hash)

  [response['task_id'], response['message']]
end