forked from virtuozzo/backup-plugin-veeam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
immediate_virtual_server_backup.rb
36 lines (27 loc) · 1.11 KB
/
immediate_virtual_server_backup.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Backups::Plugin.hook helpers: %i[client_helper query_helper task_helper uid_helper session_helper] do
BACKUP_RESULT_KEY_CHAIN = %i[QueryResult Entities BackupJobSessions BackupJobSession Result].freeze
def call(virtual_server)
job_id = virtual_server.metadata[:veeam_related_job_ids]&.first
return error('Unable to find jobs for %s' % virtual_server.label) unless job_id
task_path = task_path(api_post("jobs/#{job_id}?action=start"))
task_poller(task_path, interval: 15).run
backup_session_uid = get_backup_session_uid(job_id)
return error('Unable to start backup session') unless backup_session_uid
session_poller(
BACKUP_RESULT_KEY_CHAIN,
build_query(:backupJobSession, uid: backup_session_uid)
).run
end
private
def get_backup_session_uid(job_id)
[
api_get(
build_query(:BackupJobSession, jobUid: identifier_to_uid(:Job, job_id), state: :Working)
).dig(:QueryResult, :Entities, :BackupJobSessions, :BackupJobSession)
]
.flatten
.compact
.max_by { |hash| Time.parse(hash[:CreationTimeUTC]) }
&.fetch(:UID)
end
end