-
Notifications
You must be signed in to change notification settings - Fork 0
/
op_autobk_backup_DCM.py
executable file
·54 lines (49 loc) · 1.6 KB
/
op_autobk_backup_DCM.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
#!/usr/local/bin/python3
import sys as mSys
import pysftp as mSFTP
from lib_autobk import *
###############################
# Global Configurations and Constants
sBkSrcDCM = '/tmp/archive.tar.gz'
sBkCmdDCM = """\
cd /settings && \
/bin/tar -ch \
--exclude=24c64.bin \
--exclude=backup-eth0.ip-settings \
--exclude=backup-eth3.ip-settings \
--exclude=eth0.ip-settings \
--exclude=eth3.ip-settings \
--exclude=internal \
--exclude=kcfg.bin \
--exclude=license.txt \
--exclude=psk.txt \
--exclude=setkey.conf \
. | \
gzip -c -n > /tmp/archive.tar.gz"""
###############################
try:
# Configuration and Logging
(oINI, oLog, sAt) = LoadConfig('op-backup-DCM')
sIniUsr = oINI.get('DCM', 'Usr', fallback='AutoBk')
sIniPwd = oINI.get('DCM', 'Pwd', fallback='@ut0BkS3rv!ce')
# 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
# SFTP to DCM and run backup command
cnopts = mSFTP.CnOpts()
cnopts.hostkeys = None
oSftp = mSFTP.Connection(host=sDeviceIP, username=sIniUsr, password=sIniPwd, cnopts=cnopts)
oLog.info(sAt, 'sftp', 'connected')
oSftp.execute(sBkCmdDCM) # backup cmd
oSftp.get(sBkSrcDCM, sBkTarget, preserve_mtime=True) # download
oSftp.remove(sBkSrcDCM) # cleanup
oSftp.close()
oLog.info(sAt, 'sftp', '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)