forked from extremenetworks/ExtremeScripting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDailyBackup.py
More file actions
140 lines (128 loc) · 5.07 KB
/
Copy pathDailyBackup.py
File metadata and controls
140 lines (128 loc) · 5.07 KB
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
#!/usr/bin/env python
'''
#############################################################################
#
# Script : Daily Automatic Backup Script
# Revision : 1.2
# EXOS Version(s) : 31.5.1.6 (EXOS supports Python)
# Last Updated : 19-May-2022
#
# Purpose:
# Run automated back up on all scripts, including configuration, policies, and scripts. Benefits
# include simple rollback and configuration history.
# The first time this script runs, it installs itself into UPM to be called once a day.
#
# MODIFY the user configurations below for your environment
#
'''
import time
import os
###############################################################################
# U S E R C O N F I G U R A T I O N
###############################################################################
# Preconfigured TFTP Server. Please Set to your TFTP Server
tftp = '10.1.1.5'
# Preconfigured Virtual Router. Please Configure
vrtr = 'vr-Mgmt'
# Time of day when backup occurs
backupTime = '22:00'
###############################################################################
#Set all or none of these global variables.
serial = None
yeardirectory = None
modirectory = None
daydirectory = None
systemTime = None
def exosCmd(cmd):
#print cmd
result = exsh.clicmd(cmd, True)
#print result
return result
def uploadFiles(fileSuffix, uploadPrefix):
serialSystem = serial + '_' + systemTime + '_'
for line in exosCmd('ls *' + fileSuffix).splitlines():
line.strip()
if line.endswith(fileSuffix):
fileName = line.split()[-1]
if len(fileName) > 32:
exosCmd('create log message "Length Error.File name for ' + fileName + ' exceeded 32 byte max length."')
exosCmd('create log message "' + fileName + ' truncated to ' + fileName[:32] + '"')
fileName = fileName[:32]
destFileName = '{yyyy}-{mm}-{dd}_{prefix}{file}{sourceFile}'.format(yyyy=yeardirectory,
mm=modirectory,
dd=daydirectory,
prefix=uploadPrefix,
file=serialSystem,
sourceFile=fileName)
cmd = 'tftp put {ipaddress} vr {virtualRouter} {sourceFile} {dest}'.format(ipaddress=tftp,
virtualRouter=vrtr,
sourceFile=fileName,
dest=destFileName)
print exosCmd(cmd)
exosCmd('create log message "File ' + fileName + ' exported to ' + tftp + ' Server as ' + destFileName + '"')
def upmConfig():
result = None
upmName='dailybackup'
exosCmd('enable cli scripting permanent')
for line in exosCmd('show upm profile').splitlines():
if line.startswith(upmName):
break
else:
# Creating the Backup UPM
# because we cannot interact with the shell the way TCL can,
# we need to create a separate script that will create the upm profile
xsfName = __file__.replace('.py','.xsf')
xsf = open(xsfName,'w')
xsf.write('create upm profile ' + upmName + '\n')
xsf.write('load script ' + os.path.basename(__file__) + '\n')
xsf.write('.\n\n')
xsf.close()
exosCmd('load script ' + os.path.basename(xsfName))
os.remove(xsfName)
# Here we configure the UPM to have a liberal execution time.
exosCmd('configure upm profile ' + upmName + ' maximum execution-time 1000')
exosCmd('create log target upm ' + upmName)
exosCmd('create log filter autologfilter')
exosCmd('configure log target upm ' + upmName + ' filter autologfilter')
exosCmd('enable log target upm ' + upmName)
exosCmd('enable upm profile ' + upmName)
exosCmd('create log message "UPM ' + upmName + ' successfully created"')
result = 1
# Check UPM timer exists
for line in exosCmd('show upm timers').splitlines():
if line.startswith(upmName):
break
else:
exosCmd('create upm timer ' + upmName)
exosCmd('config upm timer ' + upmName + ' profile ' + upmName)
backupHourMin = backupTime.split(':')
cmd = 'config upm timer {name} at {mm} {dd} {yy} {hh} {min} 0 every 86400'.format(name=upmName,
mm=modirectory,
dd=daydirectory,
yy=yeardirectory,
hh=backupHourMin[0],
min=backupHourMin[1])
exosCmd(cmd)
result = 1
return result
#Setting time if None values are set.
if None in (serial,yeardirectory,modirectory,daydirectory,systemTime):
yeardirectory = time.strftime('%Y')
modirectory = time.strftime('%m')
daydirectory = time.strftime('%d')
systemTime = time.strftime('%H%M%S')
for line in exosCmd('show version').splitlines():
if line.startswith('Switch'):
tokens = line.split()
serial = tokens[3]
break
if upmConfig() == None:
# upload policy files
uploadFiles('.pol','p')
# upload TCL scripts
uploadFiles('.xsf','s')
# upload Python scripts
uploadFiles('.py','y')
# upload Config files
uploadFiles('.cfg','c')
exosCmd('create log message "Automated Backup Ran"')