-
Notifications
You must be signed in to change notification settings - Fork 0
/
op_autobk_backup_CAP.py
executable file
·123 lines (109 loc) · 4.43 KB
/
op_autobk_backup_CAP.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
#!/usr/local/bin/python3
import sys as mSys
import xml.etree.ElementTree as mXml
from datetime import timedelta, datetime, date, time, timezone
from http.client import HTTPConnection
from lib_autobk import *
###############################
# Global Configurations and Constants
sBkSrcCAP = '/usr/cp/DeviceConfig/AutoBk.zip'
sBkZipCAP = 'AutoBk.zip'
sCapTimeFmt = '%a %b %d %H:%M:%S %Z %Y'
sCapRpcLogon = """\
<?xml version="1.0" encoding="UTF-8"?>
<request id="G1000" origin="gui" destination="device" command="add" category="login" time="{}" protocol-version="4.0" platform-name="CAP-1000" type="push">
<user name="{}" password="{}" authentication="embedded" />
</request>
"""
sCapRpcBackup = """\
<?xml version="1.0" encoding="UTF-8"?>
<request id="G1001" origin="gui" destination="device" command="update" category="config" time="{0}" protocol-version="4.0" platform-name="CAP-1000" sid="{1}">
<path>
<manager id="{2}" />
<farmer id="{2}" />
</path>
<farmer id="{2}" restore-config="false" targetZipFile="{3}" />
</request>
"""
sCapRpcErase = """\
<?xml version="1.0" encoding="UTF-8"?>
<request id="G1002" origin="gui" destination="device" command="remove" category="file" time="{0}" protocol-version="4.0" platform-name="CAP-1000" sid="{1}">
<path>
<manager id="{2}" />
<farmer id="{2}" />
</path>
<file dir="/usr/cp/DeviceConfig" file-name="{3}" />
</request>
"""
sCapRpcLogoff = """\
<?xml version="1.0" encoding="UTF-8"?>
<request id="G1003" origin="gui" destination="device" command="remove" category="login" time="{}" protocol-version="4.0" platform-name="CAP-1000" sid="{}" />
"""
dnRpcHdrsCAP = {
'Content-Type': 'text/xml',
'Cache-Control':'no-cache',
'Pragma': 'no-cache',
'User-Agent': 'CtrlAutoBk/2.00',
'Accept': 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2',
'Connection': 'keep-alive',
}
dnXfrHdrsCAP = {
'filetransfer-command': 'get',
'Content-Type': 'multipart-formdata',
'User-Agent': 'CtrlAutoBk/2.00',
'Accept': 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2',
'Connection': 'keep-alive',
}
###############################
# Sends an XML/RPC request message to a CAP-1000, checks for errors and returns the response XML
def CapHttpRpc(oHttpCnx, sXml):
# Send HTTP RPC request message and parse the result as XML
xData = HttpRequest(oHttpCnx, '/xmlrq', dnRpcHdrsCAP, bPost=True, sMsg=sXml)
oXresponse = mXml.fromstring(xData)
oXreason = oXresponse.find('reason')
if (oXreason is not None and oXreason.attrib.get('err-code') != 'OK'):
raise AutoBkError('Reason: {}'.format(oXreason.text))
return oXresponse
###############################
try:
# Configuration and Logging
(oINI, oLog, sAt) = LoadConfig('op-backup-CAP')
sIniUsr = oINI.get('CAP', 'Usr', fallback='Admin')
sIniPwd = oINI.get('CAP', 'Pwd', fallback='')
# Load arguments
if (len(mSys.argv) < 3): raise AutoBkError('Missing arguments')
sDeviceIP = mSys.argv[1]
sBkTarget = mSys.argv[2]
oLog.info(sAt, 'host', sDeviceIP)
oLog.info(sAt, 'path', sBkTarget)
# Perform Backup
tNow = datetime.now(tz=timezone.utc).astimezone()
sNowCap = tNow.strftime(sCapTimeFmt) # HTTP formated time for CAP RPC calls
# HTTP Connect to CAP-1000
oHttp = HTTPConnection(sDeviceIP, 8080)
# Logon to CAP to get session ID and local manager name
oXresponse = CapHttpRpc(oHttp, sCapRpcLogon.format(sNowCap, sIniUsr, sIniPwd))
oXsession = oXresponse.find('session')
sCapSID = oXsession.attrib.get('sid')
sCapManager = oXsession.attrib.get('manager-id')
oLog.info(sAt, 'session', sCombo.format(sCapManager, sCapSID))
# Request config update
CapHttpRpc(oHttp, sCapRpcBackup.format(sNowCap, sCapSID, sCapManager, sBkZipCAP))
oLog.info(sAt, 'update', 'complete')
# Request config download
dnXfrHdrsCAP['filetransfer-name'] = sBkSrcCAP
dnXfrHdrsCAP['filetransfer-sid'] = sCapSID
xData = HttpRequest(oHttp, '/filetransfer', dnXfrHdrsCAP, bPost=True)
oFile = open(sBkTarget, 'wb')
oFile.write(xData)
oFile.close()
oLog.info(sAt, 'transfer', 'complete')
# Cleanup, logoff, and disconnect HTTP session
CapHttpRpc(oHttp, sCapRpcErase.format(sNowCap, sCapSID, sCapManager, sBkZipCAP))
CapHttpRpc(oHttp, sCapRpcLogoff.format(sNowCap, sCapSID))
oHttp.close()
oLog.info(sAt, 'session', 'complete')
except Exception as oErr:
if (oLog is not None): oLog.error(sAt, 'unknown', oErr)
print(str(oErr)) # used by calling thread to get error message
mSys.exit(1)