-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbackedUpFileList.py
234 lines (203 loc) · 11.1 KB
/
backedUpFileList.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env python
"""backed up files list for python"""
# version 2023.04.01
# import pyhesity wrapper module
from pyhesity import *
from datetime import datetime
import codecs
import argparse
try:
from urllib.parse import quote_plus
except Exception:
from urllib import quote_plus
# command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--vip', type=str, required=True) # cluster to connect to
parser.add_argument('-u', '--username', type=str, default='helios') # username
parser.add_argument('-d', '--domain', type=str, default='local') # domain - defaults to local
parser.add_argument('-i', '--useApiKey', action='store_true') # use API key authentication
parser.add_argument('-pwd', '--password', type=str, default=None) # optional password
parser.add_argument('-s', '--sourceserver', type=str, action='append') # name of source server
parser.add_argument('-j', '--jobname', type=str, required=True) # narrow search by job name
parser.add_argument('-l', '--showversions', action='store_true') # show available snapshots
parser.add_argument('-k', '--listfiles', action='store_true') # show fils in snapshots
parser.add_argument('-t', '--start', type=str, default=None) # show snapshots after date
parser.add_argument('-e', '--end', type=str, default=None) # show snapshots before date
parser.add_argument('-r', '--runid', type=int, default=None) # choose specific job run id
parser.add_argument('-f', '--filedate', type=str, default=None) # show snapshots after date
parser.add_argument('-p', '--startpath', type=str, default='/') # show files under this path
parser.add_argument('-n', '--noindex', action='store_true') # do not use librarian
parser.add_argument('-x', '--forceindex', action='store_true') # do not use librarian
parser.add_argument('-ss', '--showstats', action='store_true') # show file last modified date and size
parser.add_argument('-nt', '--newerthan', type=int, default=0) # show files newer than X days
args = parser.parse_args()
vip = args.vip
username = args.username
domain = args.domain
password = args.password
useApiKey = args.useApiKey
sourceservers = args.sourceserver
jobname = args.jobname
showversions = args.showversions
start = args.start
end = args.end
runid = args.runid
filedate = args.filedate
listfiles = args.listfiles
startpath = args.startpath
noindex = args.noindex
showstats = args.showstats
newerthan = args.newerthan
forceIndex = args.forceindex
def listdir(dirPath, instance, f, volumeInfoCookie=None, volumeName=None, cookie=None):
global fileCount
thisDirPath = quote_plus(dirPath).replace('%2F%2F', '%2F')
if cookie is not None:
if volumeName is not None:
dirList = api('get', '/vm/directoryList?%s%s&statFileEntries=%s&dirPath=%s&volumeInfoCookie=%s&volumeName=%s&cookie=%s' % (instance, useLibrarian, statfile, thisDirPath, volumeInfoCookie, volumeName, cookie))
else:
dirList = api('get', '/vm/directoryList?%s%s&statFileEntries=%s&dirPath=%s&cookie=%s' % (instance, useLibrarian, statfile, thisDirPath, cookie))
else:
if volumeName is not None:
dirList = api('get', '/vm/directoryList?%s%ss&statFileEntries=%s&dirPath=%s&volumeInfoCookie=%s&volumeName=%s' % (instance, useLibrarian, statfile, thisDirPath, volumeInfoCookie, volumeName))
else:
dirList = api('get', '/vm/directoryList?%s%s&statFileEntries=%s&dirPath=%s' % (instance, useLibrarian, statfile, thisDirPath))
if dirList and 'entries' in dirList:
for entry in sorted(dirList['entries'], key=lambda e: e['name']):
if entry['type'] == 'kDirectory':
listdir('%s/%s' % (dirPath, entry['name']), instance, f, volumeInfoCookie, volumeName)
else:
if statfile is True:
filesize = entry['fstatInfo']['size']
mtime = usecsToDate(entry['fstatInfo']['mtimeUsecs'])
mtimeusecs = entry['fstatInfo']['mtimeUsecs']
if newerthan == 0 or mtimeusecs > timeAgo(newerthan, 'days'):
fileCount += 1
print('%s (%s) [%s bytes]' % (entry['fullPath'], mtime, filesize))
f.write('%s (%s) [%s bytes]\n' % (entry['fullPath'], mtime, filesize))
else:
fileCount += 1
print('%s' % entry['fullPath'])
f.write('%s\n' % entry['fullPath'])
if dirList and 'cookie' in dirList:
listdir('%s' % dirPath, instance, f, volumeInfoCookie, volumeName, dirList['cookie'])
def showFiles(doc, version):
global useLibrarian
global fileCount
filecount = 0
if 'numEntriesIndexed' not in version or version['numEntriesIndexed'] == 0:
useLibrarian = '' # False
else:
if 'indexingStatus' not in version or version['indexingStatus'] != 2:
useLibrarian = '' # False
else:
useLibrarian = '&useLibrarian=true'
if forceIndex and 'indexingStatus' in version and version['indexingStatus'] == 2:
useLibrarian = '&useLibrarian=true'
if noindex:
useLibrarian = '' # False
instance = ("attemptNum=%s&clusterId=%s&clusterIncarnationId=%s&entityId=%s&jobId=%s&jobInstanceId=%s&jobStartTimeUsecs=%s&jobUidObjectId=%s" %
(version['instanceId']['attemptNum'],
doc['objectId']['jobUid']['clusterId'],
doc['objectId']['jobUid']['clusterIncarnationId'],
doc['objectId']['entity']['id'],
doc['objectId']['jobId'],
version['instanceId']['jobInstanceId'],
version['instanceId']['jobStartTimeUsecs'],
doc['objectId']['jobUid']['objectId']))
fileDateString = datetime.strptime(usecsToDate(version['instanceId']['jobStartTimeUsecs']), '%Y-%m-%d %H:%M:%S').strftime('%Y-%m-%d_%H-%M-%S')
f = codecs.open('backedUpFiles-%s-%s-%s.txt' % (sourceserver, version['instanceId']['jobInstanceId'], fileDateString), 'w', 'utf-8')
volumeTypes = [1, 6]
backupType = doc['backupType']
if backupType in volumeTypes:
volumeList = api('get', '/vm/volumeInfo?%s&statFileEntries=%s' % (instance, statfile))
if 'volumeInfos' in volumeList:
volumeInfoCookie = volumeList['volumeInfoCookie']
for volume in sorted(volumeList['volumeInfos'], key=lambda v: v['name']):
volumeName = quote_plus(volume['name'])
listdir(startpath, instance, f, volumeInfoCookie, volumeName)
else:
listdir(startpath, instance, f)
print('\n%s files found' % fileCount)
f.close()
if sourceservers is None or len(sourceservers) == 0:
print('--sourceserver is required')
exit()
if showstats is True or newerthan > 0:
statfile = True
else:
statfile = False
useLibrarian = '&useLibrarian=true' # True
fileCount = 0
# authenticate
apiauth(vip=vip, username=username, domain=domain, password=password, useApiKey=useApiKey)
for sourceserver in sourceservers:
print('\n============================\n %s\n============================\n' % sourceserver)
search = api('get', '/searchvms?entityTypes=kView&entityTypes=kAcropolis&entityTypes=kAWS&entityTypes=kAWSNative&entityTypes=kAWSSnapshotManager&entityTypes=kAzure&entityTypes=kAzureNative&entityTypes=kFlashBlade&entityTypes=kGCP&entityTypes=kGenericNas&entityTypes=kHyperV&entityTypes=kHyperVVSS&entityTypes=kIsilon&entityTypes=kKVM&entityTypes=kNetapp&entityTypes=kPhysical&entityTypes=kVMware&vmName=%s' % sourceserver)
if 'vms' not in search:
print('no backups found for %s' % sourceserver)
continue
searchResults = [vm for vm in search['vms'] if vm['vmDocument']['objectName'].lower() == sourceserver.lower()]
if len(searchResults) == 0:
print('no backups found for %s' % sourceserver)
continue
altJobName = 'old name: %s' % jobname.lower()
altJobName2 = '%s (old name' % jobname.lower()
searchResults = [vm for vm in searchResults if vm['vmDocument']['jobName'].lower() == jobname.lower() or altJobName in vm['vmDocument']['jobName'].lower() or altJobName2 in vm['vmDocument']['jobName'].lower()]
if len(searchResults) == 0:
print('%s not protected by %s' % (sourceserver, jobname))
continue
searchResults = [r for r in searchResults if 'versions' in r['vmDocument'] and len(r['vmDocument']['versions']) > 0]
if len(searchResults) == 0:
print('No backups available for %s in %s' % (sourceserver, jobname))
continue
allVersions = []
for searchResult in searchResults:
for version in searchResult['vmDocument']['versions']:
version['doc'] = searchResult['vmDocument']
allVersions.append(version)
allVersions = sorted(allVersions, key=lambda r: r['snapshotTimestampUsecs'], reverse=True)
if showversions or start is not None or end is not None or listfiles:
if start is not None:
startusecs = dateToUsecs(start)
allVersions = [v for v in allVersions if startusecs <= v['snapshotTimestampUsecs']]
if end is not None:
endusecs = dateToUsecs(end)
allVersions = [v for v in allVersions if endusecs >= v['snapshotTimestampUsecs']]
if listfiles:
for version in allVersions:
print("\n==============================")
print(" runId: %s" % version['instanceId']['jobInstanceId'])
print(" runDate: %s" % usecsToDate(version['instanceId']['jobStartTimeUsecs']))
print("==============================\n")
showFiles(version['doc'], version)
else:
print('%10s %s' % ('runId', 'runDate'))
print('%10s %s' % ('-----', '-------'))
for version in allVersions:
print('%10d %s' % (version['instanceId']['jobInstanceId'], usecsToDate(version['instanceId']['jobStartTimeUsecs'])))
continue
# select version
if runid is not None:
# select version with matching runId
versions = [v for v in allVersions if runid == v['instanceId']['jobInstanceId']]
if len(versions) == 0:
print('Run ID not found')
continue
else:
version = versions[0]
showFiles(version['doc'], version)
elif filedate is not None:
# select version just after requested date
filedateusecs = dateToUsecs(filedate)
versions = [v for v in allVersions if filedateusecs <= v['snapshotTimestampUsecs']]
if versions:
version = versions[-1]
showFiles(version['doc'], version)
else:
print('No backups from the specified date')
continue
else:
# just use latest version
version = allVersions[0]
showFiles(version['doc'], version)