Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DG-28970 | Add backup_requests method #73

Merged
merged 9 commits into from
Dec 18, 2024
47 changes: 47 additions & 0 deletions lib/sfrest/backup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,53 @@ def create_backup(site_id, datum = nil)
@conn.post(current_path, datum.to_json)
end

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

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

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

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

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

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

# Gets a url to download a backup
# @param [Integer] site_id Node id of site
# @param [Integer] backup_id Id of backup to delete
Expand Down
Loading