-
Notifications
You must be signed in to change notification settings - Fork 0
/
init-veeam.cypher
246 lines (232 loc) · 16.7 KB
/
init-veeam.cypher
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
235
236
237
238
239
240
241
242
243
244
245
246
// SECTION Create (:Veeamserver) nodes
WITH "base-veeam-api-url/backupservers?format=Entity" as url
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
unwind value.BackupServers as backupserver
MERGE (vs:Veeamserver {id:backupserver.UID}) SET vs.name=backupserver.Name, vs.description=backupserver.Description,vs.version=vs.Version
WITH backupserver,vs
UNWIND backupserver.Links as link
WITH vs,link where link.Type='BackupServerReference'
SET vs.apiurl=split(link.Href,'/backupServers/')[0]
return vs;
// SECTION Create (:Veeamjob)-[:JOB_MANAGEDBY_SERVER]->(:Veeamserver) nodes and relationships to Veeamservers
WITH "base-veeam-api-url/jobs?format=Entity" as url
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
unwind value.Jobs as job
MERGE (vj:Veeamjob {id:job.UID}) set vj.name=job.Name set vj.type=job.JobType,vj.scheduled=job.ScheduleConfigured
WITH job,vj
UNWIND job.Links as joblink
WITH * where joblink.Type='BackupServerReference'
MATCH (vs:Veeamserver {name:joblink.Name})
MERGE (vj)-[:JOB_MANAGEDBY_SERVER]->(vs)
return vj,vs;
// SECTION Create (:Veeamprotectedvm) nodes via the VeeamAPI lookupSvc
// TO avoid duplicates (multiple veeam servers and multiple vcenters, we use a combination of the vm-xxxxx plus the name as the unique id for a VM)
WITH "base-veeam-api-url/lookupSvc" as url
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
unwind value.Links as link
WITH link where link.Type='HierarchyItemList' and link.Href ends with '=Vm'
CALL apoc.load.jsonParams(link.Href,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
WITH *,"[^A-Za-z\\d-. _]{1,63}" as regex1
unwind value.HierarchyItems as vmobject
WITH *,trim(apoc.text.regreplace(vmobject.ObjectName,regex1,'')) as cleanedname,split(vmobject.ObjectRef,'.')[1] as vmid
MERGE (vvm:Veeamprotectedvm {id:vmid,name:cleanedname}) SET vvm.creation='VeeamAPI lookupSvc function'
FOREACH (ignoreMe in CASE WHEN not vmobject.ObjectRef in coalesce(vvm.vobjid,[]) then [1] ELSE [] END | SET vvm.vobjid=coalesce(vvm.vobjid,[]) + vmobject.ObjectRef)
RETURN vvm;
// SECTION discover the restorepoints (in the last 4 hours)
MATCH (vsr:Veeamserver) where toLower(vsr.apiurl)=toLower('base-veeam-api-url') SET vsr.pendingupdate=timestamp(),vsr.initstamp=timestamp()-1440000
WITH vsr,timestamp() AS howsoonisnow
WITH apoc.date.format(vsr.initstamp,'ms',"yyyy-MM-dd'T'HH:mm:ss'Z'") as backupdate
MATCH (vvm:Veeamprotectedvm) where not (vvm)--(:Veeambackup)--(:Veeamjob {type:'Backup'})
UNWIND vvm.vobjid as vobjid
WITH vvm,backupdate,"base-veeam-api-url/query?type=VmRestorePoint&format=Entities&filter=HierarchyObjRef==%22"+vobjid+"%22;CreationTime%3E"+backupdate as url
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
UNWIND value.Entities as vmrestorepoints
UNWIND vmrestorepoints.VmRestorePoints as vmrestorepoint
UNWIND vmrestorepoint.VmRestorePoints as restorepoint
MERGE (vb:Veeambackup {name:restorepoint.Name}) SET vb.type=restorepoint.PointType,vb.algorithm=restorepoint.Algorithm,vb.creationtime=restorepoint.CreationTimeUTC
MERGE (vb)-[:BACKUP_OF]->(vvm)
WITH vb,restorepoint where not (vb)-[:PART_OF_JOB]->(:Veeamjob)
UNWIND restorepoint.Links as link
WITH vb,link.Href as url where link.Type='RestorePointReference'
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
UNWIND value.Links as link
WITH vb,link.Href as url where link.Type='BackupReference'
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
UNWIND value.Links as link
OPTIONAL MATCH (vbs:Veeamserver {name:link.Name}) where vbs.id ends with last(split(link.Href,'/'))
FOREACH (ignoreMe in CASE WHEN exists(vbs.name) and vbs.name <> '' then [1] ELSE [] END | MERGE (vb)-[:BACKUP_PERFORMED_ON]->(vbs))
WITH vb,link where link.Type='Backup'
MATCH (vj:Veeamjob {name:link.Name}) WHERE (vj)--(:Veeamserver)--(vb)
MERGE (vb)-[:PART_OF_JOB]->(vj)
return link,vj.name,vb.name;
// SECTION discover the restorepoints (between 4 hours and 1 day)
MATCH (vsr:Veeamserver) where toLower(vsr.apiurl)=toLower('base-veeam-api-url')
WITH vsr,vsr.initstamp as lastscan
SET vsr.initstamp=timestamp()-86400000
WITH apoc.date.format(vsr.initstamp,'ms',"yyyy-MM-dd'T'HH:mm:ss'Z'") as backupdate,apoc.date.format(lastscan,'ms',"yyyy-MM-dd'T'HH:mm:ss'Z'") as lastbackupscan
MATCH (vvm:Veeamprotectedvm) where not (vvm)--(:Veeambackup)--(:Veeamjob {type:'Backup'})
UNWIND vvm.vobjid as vobjid
WITH vvm,backupdate,"base-veeam-api-url/query?type=VmRestorePoint&format=Entities&filter=HierarchyObjRef==%22"+vobjid+"%22;CreationTime%3E"+backupdate+";CreationTime%3C"+lastbackupscan as url
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
UNWIND value.Entities as vmrestorepoints
UNWIND vmrestorepoints.VmRestorePoints as vmrestorepoint
UNWIND vmrestorepoint.VmRestorePoints as restorepoint
MERGE (vb:Veeambackup {name:restorepoint.Name}) SET vb.type=restorepoint.PointType,vb.algorithm=restorepoint.Algorithm,vb.creationtime=restorepoint.CreationTimeUTC
MERGE (vb)-[:BACKUP_OF]->(vvm)
WITH vb,restorepoint where not (vb)-[:PART_OF_JOB]->(:Veeamjob)
UNWIND restorepoint.Links as link
WITH vb,link.Href as url where link.Type='RestorePointReference'
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
UNWIND value.Links as link
WITH vb,link.Href as url where link.Type='BackupReference'
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
UNWIND value.Links as link
OPTIONAL MATCH (vbs:Veeamserver {name:link.Name}) where vbs.id ends with last(split(link.Href,'/'))
FOREACH (ignoreMe in CASE WHEN exists(vbs.name) and vbs.name <> '' then [1] ELSE [] END | MERGE (vb)-[:BACKUP_PERFORMED_ON]->(vbs))
WITH vb,link where link.Type='Backup'
MATCH (vj:Veeamjob {name:link.Name}) WHERE (vj)--(:Veeamserver)--(vb)
MERGE (vb)-[:PART_OF_JOB]->(vj)
return link,vj.name,vb.name;
// SECTION discover the restorepoints (between 4 hours and 1 day)
MATCH (vsr:Veeamserver) where toLower(vsr.apiurl)=toLower('base-veeam-api-url')
WITH vsr,vsr.initstamp as lastscan
SET vsr.initstamp=timestamp()-86400000
WITH apoc.date.format(vsr.initstamp,'ms',"yyyy-MM-dd'T'HH:mm:ss'Z'") as backupdate,apoc.date.format(lastscan,'ms',"yyyy-MM-dd'T'HH:mm:ss'Z'") as lastbackupscan
MATCH (vvm:Veeamprotectedvm) where not (vvm)--(:Veeambackup)--(:Veeamjob {type:'Backup'})
UNWIND vvm.vobjid as vobjid
WITH vvm,backupdate,"base-veeam-api-url/query?type=VmRestorePoint&format=Entities&filter=HierarchyObjRef==%22"+vobjid+"%22;CreationTime%3E"+backupdate+";CreationTime%3C"+lastbackupscan as url
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
UNWIND value.Entities as vmrestorepoints
UNWIND vmrestorepoints.VmRestorePoints as vmrestorepoint
UNWIND vmrestorepoint.VmRestorePoints as restorepoint
MERGE (vb:Veeambackup {name:restorepoint.Name}) SET vb.type=restorepoint.PointType,vb.algorithm=restorepoint.Algorithm,vb.creationtime=restorepoint.CreationTimeUTC
MERGE (vb)-[:BACKUP_OF]->(vvm)
WITH vb,restorepoint where not (vb)-[:PART_OF_JOB]->(:Veeamjob)
UNWIND restorepoint.Links as link
WITH vb,link.Href as url where link.Type='RestorePointReference'
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
UNWIND value.Links as link
WITH vb,link.Href as url where link.Type='BackupReference'
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
UNWIND value.Links as link
OPTIONAL MATCH (vbs:Veeamserver {name:link.Name}) where vbs.id ends with last(split(link.Href,'/'))
FOREACH (ignoreMe in CASE WHEN exists(vbs.name) and vbs.name <> '' then [1] ELSE [] END | MERGE (vb)-[:BACKUP_PERFORMED_ON]->(vbs))
WITH vb,link where link.Type='Backup'
MATCH (vj:Veeamjob {name:link.Name}) WHERE (vj)--(:Veeamserver)--(vb)
MERGE (vb)-[:PART_OF_JOB]->(vj)
return link,vj.name,vb.name;
// SECTION discover the restorepoints (between 1 day and 2 days)
MATCH (vsr:Veeamserver) where toLower(vsr.apiurl)=toLower('base-veeam-api-url')
WITH vsr,vsr.initstamp as lastscan
SET vsr.initstamp=timestamp()-172800000
WITH apoc.date.format(vsr.initstamp,'ms',"yyyy-MM-dd'T'HH:mm:ss'Z'") as backupdate,apoc.date.format(lastscan,'ms',"yyyy-MM-dd'T'HH:mm:ss'Z'") as lastbackupscan
MATCH (vvm:Veeamprotectedvm) where not (vvm)--(:Veeambackup)--(:Veeamjob {type:'Backup'})
UNWIND vvm.vobjid as vobjid
WITH vvm,backupdate,"base-veeam-api-url/query?type=VmRestorePoint&format=Entities&filter=HierarchyObjRef==%22"+vobjid+"%22;CreationTime%3E"+backupdate+";CreationTime%3C"+lastbackupscan as url
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
UNWIND value.Entities as vmrestorepoints
UNWIND vmrestorepoints.VmRestorePoints as vmrestorepoint
UNWIND vmrestorepoint.VmRestorePoints as restorepoint
MERGE (vb:Veeambackup {name:restorepoint.Name}) SET vb.type=restorepoint.PointType,vb.algorithm=restorepoint.Algorithm,vb.creationtime=restorepoint.CreationTimeUTC
MERGE (vb)-[:BACKUP_OF]->(vvm)
WITH vb,restorepoint where not (vb)-[:PART_OF_JOB]->(:Veeamjob)
UNWIND restorepoint.Links as link
WITH vb,link.Href as url where link.Type='RestorePointReference'
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
UNWIND value.Links as link
WITH vb,link.Href as url where link.Type='BackupReference'
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
UNWIND value.Links as link
OPTIONAL MATCH (vbs:Veeamserver {name:link.Name}) where vbs.id ends with last(split(link.Href,'/'))
FOREACH (ignoreMe in CASE WHEN exists(vbs.name) and vbs.name <> '' then [1] ELSE [] END | MERGE (vb)-[:BACKUP_PERFORMED_ON]->(vbs))
WITH vb,link where link.Type='Backup'
MATCH (vj:Veeamjob {name:link.Name}) WHERE (vj)--(:Veeamserver)--(vb)
MERGE (vb)-[:PART_OF_JOB]->(vj)
return link,vj.name,vb.name;
// SECTION discover the restorepoints (between 2 day and 4 days)
MATCH (vsr:Veeamserver) where toLower(vsr.apiurl)=toLower('base-veeam-api-url')
WITH vsr,vsr.initstamp as lastscan
SET vsr.initstamp=timestamp()-345600000
WITH apoc.date.format(vsr.initstamp,'ms',"yyyy-MM-dd'T'HH:mm:ss'Z'") as backupdate,apoc.date.format(lastscan,'ms',"yyyy-MM-dd'T'HH:mm:ss'Z'") as lastbackupscan
MATCH (vvm:Veeamprotectedvm) where not (vvm)--(:Veeambackup)--(:Veeamjob {type:'Backup'})
UNWIND vvm.vobjid as vobjid
WITH vvm,backupdate,"base-veeam-api-url/query?type=VmRestorePoint&format=Entities&filter=HierarchyObjRef==%22"+vobjid+"%22;CreationTime%3E"+backupdate+";CreationTime%3C"+lastbackupscan as url
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
UNWIND value.Entities as vmrestorepoints
UNWIND vmrestorepoints.VmRestorePoints as vmrestorepoint
UNWIND vmrestorepoint.VmRestorePoints as restorepoint
MERGE (vb:Veeambackup {name:restorepoint.Name}) SET vb.type=restorepoint.PointType,vb.algorithm=restorepoint.Algorithm,vb.creationtime=restorepoint.CreationTimeUTC
MERGE (vb)-[:BACKUP_OF]->(vvm)
WITH vb,restorepoint where not (vb)-[:PART_OF_JOB]->(:Veeamjob)
UNWIND restorepoint.Links as link
WITH vb,link.Href as url where link.Type='RestorePointReference'
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
UNWIND value.Links as link
WITH vb,link.Href as url where link.Type='BackupReference'
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
UNWIND value.Links as link
OPTIONAL MATCH (vbs:Veeamserver {name:link.Name}) where vbs.id ends with last(split(link.Href,'/'))
FOREACH (ignoreMe in CASE WHEN exists(vbs.name) and vbs.name <> '' then [1] ELSE [] END | MERGE (vb)-[:BACKUP_PERFORMED_ON]->(vbs))
WITH vb,link where link.Type='Backup'
MATCH (vj:Veeamjob {name:link.Name}) WHERE (vj)--(:Veeamserver)--(vb)
MERGE (vb)-[:PART_OF_JOB]->(vj)
return link,vj.name,vb.name;
// SECTION discover the restorepoints (between 4 day and 7 days)
MATCH (vsr:Veeamserver) where toLower(vsr.apiurl)=toLower('base-veeam-api-url')
WITH vsr,vsr.initstamp as lastscan
SET vsr.initstamp=timestamp()-604800000
WITH apoc.date.format(vsr.initstamp,'ms',"yyyy-MM-dd'T'HH:mm:ss'Z'") as backupdate,apoc.date.format(lastscan,'ms',"yyyy-MM-dd'T'HH:mm:ss'Z'") as lastbackupscan
MATCH (vvm:Veeamprotectedvm) where not (vvm)--(:Veeambackup)--(:Veeamjob {type:'Backup'})
UNWIND vvm.vobjid as vobjid
WITH vvm,backupdate,"base-veeam-api-url/query?type=VmRestorePoint&format=Entities&filter=HierarchyObjRef==%22"+vobjid+"%22;CreationTime%3E"+backupdate+";CreationTime%3C"+lastbackupscan as url
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
UNWIND value.Entities as vmrestorepoints
UNWIND vmrestorepoints.VmRestorePoints as vmrestorepoint
UNWIND vmrestorepoint.VmRestorePoints as restorepoint
MERGE (vb:Veeambackup {name:restorepoint.Name}) SET vb.type=restorepoint.PointType,vb.algorithm=restorepoint.Algorithm,vb.creationtime=restorepoint.CreationTimeUTC
MERGE (vb)-[:BACKUP_OF]->(vvm)
WITH vb,restorepoint where not (vb)-[:PART_OF_JOB]->(:Veeamjob)
UNWIND restorepoint.Links as link
WITH vb,link.Href as url where link.Type='RestorePointReference'
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
UNWIND value.Links as link
WITH vb,link.Href as url where link.Type='BackupReference'
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
UNWIND value.Links as link
OPTIONAL MATCH (vbs:Veeamserver {name:link.Name}) where vbs.id ends with last(split(link.Href,'/'))
FOREACH (ignoreMe in CASE WHEN exists(vbs.name) and vbs.name <> '' then [1] ELSE [] END | MERGE (vb)-[:BACKUP_PERFORMED_ON]->(vbs))
WITH vb,link where link.Type='Backup'
MATCH (vj:Veeamjob {name:link.Name}) WHERE (vj)--(:Veeamserver)--(vb)
MERGE (vb)-[:PART_OF_JOB]->(vj)
return link,vj.name,vb.name;
// SECTION discover the restorepoints (between 7 day and 14 days)
MATCH (vsr:Veeamserver) where toLower(vsr.apiurl)=toLower('base-veeam-api-url')
WITH vsr,vsr.initstamp as lastscan
SET vsr.initstamp=timestamp()-1209600000
WITH apoc.date.format(vsr.initstamp,'ms',"yyyy-MM-dd'T'HH:mm:ss'Z'") as backupdate,apoc.date.format(lastscan,'ms',"yyyy-MM-dd'T'HH:mm:ss'Z'") as lastbackupscan
MATCH (vvm:Veeamprotectedvm) where not (vvm)--(:Veeambackup)--(:Veeamjob {type:'Backup'})
UNWIND vvm.vobjid as vobjid
WITH vvm,backupdate,"base-veeam-api-url/query?type=VmRestorePoint&format=Entities&filter=HierarchyObjRef==%22"+vobjid+"%22;CreationTime%3E"+backupdate+";CreationTime%3C"+lastbackupscan as url
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
UNWIND value.Entities as vmrestorepoints
UNWIND vmrestorepoints.VmRestorePoints as vmrestorepoint
UNWIND vmrestorepoint.VmRestorePoints as restorepoint
MERGE (vb:Veeambackup {name:restorepoint.Name}) SET vb.type=restorepoint.PointType,vb.algorithm=restorepoint.Algorithm,vb.creationtime=restorepoint.CreationTimeUTC
MERGE (vb)-[:BACKUP_OF]->(vvm)
WITH vb,restorepoint where not (vb)-[:PART_OF_JOB]->(:Veeamjob)
UNWIND restorepoint.Links as link
WITH vb,link.Href as url where link.Type='RestorePointReference'
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
UNWIND value.Links as link
WITH vb,link.Href as url where link.Type='BackupReference'
CALL apoc.load.jsonParams(url,{Accept:"application/json",`X-RestSvcSessionId`:"veeam-restsvc-sessionid"},null) yield value
UNWIND value.Links as link
OPTIONAL MATCH (vbs:Veeamserver {name:link.Name}) where vbs.id ends with last(split(link.Href,'/'))
FOREACH (ignoreMe in CASE WHEN exists(vbs.name) and vbs.name <> '' then [1] ELSE [] END | MERGE (vb)-[:BACKUP_PERFORMED_ON]->(vbs))
WITH vb,link where link.Type='Backup'
MATCH (vj:Veeamjob {name:link.Name}) WHERE (vj)--(:Veeamserver)--(vb)
MERGE (vb)-[:PART_OF_JOB]->(vj)
return link,vj.name,vb.name;
// move the .pendingupdate property to .lastupdate
MATCH (vsr:Veeamserver) where toLower(vsr.apiurl)=toLower('base-veeam-api-url')
FOREACH (ignoreMe in CASE WHEN exists(vsr.pendingupdate) then [1] ELSE [] END | SET vsr.lastupdate=vsr.pendingupdate REMOVE vsr.pendingupdate);