-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduled_restore.py
executable file
·182 lines (148 loc) · 8.26 KB
/
scheduled_restore.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/local/bin/python3
##########################################################################
## Script Name : scheduled_restore.py ##
## Created : 2017-12-27 16:55 ##
## Author : John McDevitt ##
## Function : Find active dataflows that have RESTORE in their ##
## : name. Find destination via comment in policy. ##
## : restore the latest backup of the dataflows. ##
## : ##
## Usage : ##
## Update Log : 2018-01-02 13:41 initial functional version ##
## : ##
##########################################################################
##########################################################################
## Function definitions ##
##########################################################################
debug_val = 1
def debug(msg,debug_level=1):
"""Print debugging messages"""
if debug_level >= debug_val:
print("DEBUG" + str(debug_level) + ": " + str(msg))
##########################################################################
## Imports ##
##########################################################################
import requests
import json
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
##########################################################################
## Main ##
##########################################################################
f = open('hdid_connection_info.json','r')
connection = json.load(f)
f.close
base_url = 'https://' + connection['master'] + '/HDID/v1.0.0/'
url_body={'username': connection['credentials']['username'],'password':connection['credentials']['password'] ,'space': connection['credentials']['space']}
headers= {'Content-Type': 'application/json'}
##########################################################################
## Login to HDID ##
##########################################################################
debug_level=0
url = base_url + 'master/UIController/services/User/actions/login/invoke'
response = requests.post(url,data=url_body, verify=False)
debug(str(response.status_code),debug_level)
if response.status_code > 200:
debug(response.text,debug_level)
cookie=response.cookies.get_dict()
debug(str(cookie),debug_level)
##########################################################################
## Get all of the active dataflows that have restore in their name ##
##########################################################################
debug_level=0
url = base_url + 'master/DataFlowHandler/objects/ActiveDataFlows/?query=(data.name~"restore")&order-by=data.name+ASC'
debug(url,debug_level)
response = requests.get(url,cookies=cookie, verify=False)
debug(str(response.status_code),debug_level)
if response.status_code > 200:
debug(response.text,debug_level)
json_dataflows=json.loads(response.text)
for dataflow in json_dataflows['dataflow']:
debug(str(dataflow),debug_level)
##########################################################################
## Get Policy name and the repository that's labeled for restore ##
##########################################################################
for connection in dataflow['data']['connections']:
if connection['label'] == "RESTORE":
repository_number = connection['destination']
policy_id = connection['routedPolicies'][0]
for node in dataflow['data']['nodes']:
if node['id'] == repository_number:
debug(node['nodeId'] + " is the node identified as restore source",debug_level)
repository_guid = node['nodeId']
debug_level=0
debug("getting policy name and restore repo name",debug_level)
debug("policy " + policy_id,debug_level)
debug("repo " + repository_guid,debug_level)
debug_level=0
url = base_url + 'master/PolicyHandler/objects/Policies/' + policy_id
debug(url,debug_level)
response = requests.get(url,cookies=cookie, verify=False)
debug(str(response.status_code),debug_level)
if response.status_code > 200:
debug(response.text,debug_level)
json_policy=json.loads(response.text)
debug(json.dumps(json_policy,indent=4),debug_level)
restore_target = json_policy['description'].replace('RESTORE TO ','')
debug('Restore target is ' + restore_target,debug_level)
debug_level=0
url = base_url + 'master/NodeManager/objects/Nodes/?query=(name="' + restore_target + '")'
debug(url,debug_level)
response = requests.get(url,cookies=cookie, verify=False)
debug(str(response.status_code),debug_level)
if response.status_code > 200:
debug(response.text,debug_level)
json_target=json.loads(response.text)
debug(json_target['node'][0]['id'],debug_level)
debug_level=0
url = base_url + 'master/NodeManager/objects/Nodes/' + repository_guid
debug(url,debug_level)
response = requests.get(url,cookies=cookie, verify=False)
debug(str(response.status_code),debug_level)
if response.status_code > 200:
debug(response.text,debug_level)
json_node=json.loads(response.text)
##########################################################################
## use the policy name and the repository node ID to query for restore ##
##########################################################################
debug_level=0
url = base_url + 'master/MetaDataAggregator/objects/FullRecoverableData/?query=(policyName~"' + json_policy['name'] + '")+AND+(storageNodeId~"' + json_node['name'] + '")&count=1&offset=0&order-by=captureDate+DESC'
debug(url,debug_level)
response = requests.get(url,cookies=cookie, verify=False)
debug(str(response.status_code),debug_level)
if response.status_code > 200:
debug(response.text,debug_level)
json_response=json.loads(response.text)
##########################################################################
## perform the restore to the designated target with most recent backup ##
##########################################################################
debug_level=0
url = base_url + 'master/RestoreHandler/services/Restores/actions/restore/invoke'
filesystem_parameters = {}
filesystem_parameters['destination']={}
filesystem_parameters['destination']['destinationNodeId']=json_target['node'][0]['id']
filesystem_parameters['collisionPolicy']='eRH_CP_OVERWRITERENAME'
filesystem_parameters['recoverableData']=[]
filesystem_parameters['recoverableData'].append({})
filesystem_parameters['recoverableData'][0]['storageLocationName']=json_response['fullRecoverableData'][0]['storageLocationType']
filesystem_parameters['recoverableData'][0]['storageNodeId']=json_response['fullRecoverableData'][0]['storageNodeId']
filesystem_parameters['recoverableData'][0]['includeItems']=[]
filesystem_parameters['recoverableData'][0]['includeItems'].append(json_response['fullRecoverableData'][0]['snapshotId'])
url_data = {'fileSystemParameters' : filesystem_parameters, 'type':'eRH_DT_FILESYSTEM'}
debug(json.dumps(url_data,indent=4),debug_level)
debug_level=0
response = requests.put(url,json=url_data,cookies=cookie,verify=False)
debug(str(response.status_code),debug_level)
if response.status_code > 202:
debug(response.text,debug_level)
jobid = response.json()
debug(jobid,debug_level)
debug_level=0
url = base_url + 'master/JobStatusHandler/objects/Jobs/?query=(parentId="' + jobid['id'] + '")'
response = requests.get(url,cookies=cookie, verify=False)
debug(str(response.status_code),debug_level)
if response.status_code > 200:
debug(response.text,debug_level)
job = response.json()
debug(job,debug_level)
print('Restore Job ' + str(job['job'][0]['id']) + ' has status ' + str(job['job'][0]['status']) + ' as of ' + str(job['job'][0]['timeCompleted']))