-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathcreatebinarybackup.py
executable file
·135 lines (113 loc) · 4.24 KB
/
createbinarybackup.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# createbinarybackup.py
# February 2018
#
# Usage:
# python createbinarybackup.py [-q] [-d]
#
#
# Copyright © 2018, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the License);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Import Python modules
from sharedfunctions import callrestapi, getapplicationproperties
import argparse
import json
import sys
import os
# get python version
version=int(str(sys.version_info[0]))
# get cli location from properties, check that cli is there if not ERROR and stop
clicommand=getclicommand()
debug=False
defaultBackupScheduleName="DEFAULT_BACKUP_SCHEDULE"
newScheduleName="BINARY_BACKUP_SCHEDULE"
newScheduleDesc="JobRequest to execute a binary backup"
jobDefinitionURIStem="/jobDefinitions/definitions/"
newScheduleContentType="application/vnd.sas.backup.request+json" # For a single-tenant deployment
#newScheduleContentType="application/vnd.sas.backup.deployment.request+json" # For a multi-tenant deployment
# Define exception handler so that we only output trace info from errors when in debug mode
def exception_handler(exception_type, exception, traceback, debug_hook=sys.excepthook):
if debug:
debug_hook(exception_type, exception, traceback)
else:
print (exception_type.__name__, exception)
sys.excepthook = exception_handler
parser = argparse.ArgumentParser()
#parser.add_argument("-t","--principaltype", help="Enter the type of principal to test: user or group.",required='True',choices=['user','group'])
parser.add_argument("-q","--quiet", action='store_true')
parser.add_argument("-d","--debug", action='store_true')
args = parser.parse_args()
#principaltype=args.principaltype
quiet=args.quiet
debug=args.debug
# STEP 1 of 4: Get the jobDefinition of the existing DEFAULT_BACKUP_SCHEDULE
endpoint='/jobDefinitions/definitions?limit=20&filter=in(name,"'+defaultBackupScheduleName+'")'
method='get'
accept='application/json'
jobDefinition_json=callrestapi(endpoint,method,accept)
if debug:
print('jobDefinition_json:')
print(jobDefinition_json)
jobDefinitions=jobDefinition_json['items']
id_found=False
jobDefinitionId=''
for jobDefinition in jobDefinitions:
if jobDefinition['name']:
if(jobDefinition['name']==defaultBackupScheduleName):
jobDefinitionId=jobDefinition['id']
print('Id: '+jobDefinitionId)
id_found=True
if not id_found:
raise Exception('Unable to determine Id for '+defaultBackupScheduleName+'.')
# STEP 2 of 4: Create a jobExecution request
endpoint='/jobExecution/jobRequests'
method='post'
accept='application/vnd.sas.job.execution.job.request+json'
content='application/vnd.sas.job.execution.job.request+json'
inputdata={
"name": newScheduleName,
"description": newScheduleDesc,
"jobDefinitionUri": jobDefinitionURIStem+jobDefinitionId,
"arguments": {
"contentType": newScheduleContentType,
"backupType": "binary"
}
}
jobExecutionRequest_json=callrestapi(endpoint,method,accept,content,inputdata)
if debug:
print('jobExecutionRequest_json:')
print(jobExecutionRequest_json)
# STEP 3 of 4: Get the href to submit the job from the create jobExecution response
links=jobExecutionRequest_json['links']
href_found=False
submitJobHref=''
for link in links:
if link['rel']:
if(link['rel']=="submitJob"):
submitJobHref=link['href']
print('Href: '+submitJobHref)
href_found=True
if not href_found:
raise Exception('Unable to find the href for the submitJob link.')
# STEP 4 of 4: Submit the jobExecution request
endpoint=submitJobHref
method='post'
accept='application/vnd.sas.job.execution.job+json'
submitJob_json=callrestapi(endpoint,method,accept)
#if debug:
print('submitJob_json:')
print(submitJob_json)