Skip to content

Commit fcc1cd8

Browse files
tcsimersonrkennedy
authored andcommitted
Address review comments & add Python version
1 parent 2d83610 commit fcc1cd8

2 files changed

Lines changed: 278 additions & 37 deletions

File tree

recipes/powershell/ClientBackup.ps1

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Requirements and comments for running this script
3333

3434

3535
param (
36-
[string]$p = $(throw "Please speicfy the policy name using -p parameter."),
36+
[string]$p = $(throw "Please specify the policy name using -p parameter."),
3737
[string]$k = $(throw "Please specify the password using -k parameter."),
3838
[switch]$v = $false,
3939
[switch]$t = $false
@@ -106,6 +106,8 @@ $port = 1556
106106
$basepath = "https://" + $nbmaster + ":" + $port + "/netbackup"
107107
$content_type = "application/vnd.netbackup+json;version=4.0"
108108
$days2lookback = 30
109+
$fullname = "FULL"
110+
$incrname = "INCR"
109111
if ( $verbose ) {
110112
Write-Host "Base URI = $basepath"
111113
Write-Host "Looking back $days2lookback days for previous backups"
@@ -144,11 +146,11 @@ $content = (ConvertFrom-Json -InputObject $response)
144146
# Determining backup frequency for full backup and getting
145147
# the full and incr schedule names
146148
for ( $i=0; $i -lt $content.data.attributes.policy.schedules.count; $i++ ) {
147-
if ( $content.data.attributes.policy.schedules[$i].schedulename -eq "FULL" ) {
149+
if ( $content.data.attributes.policy.schedules[$i].schedulename -eq $fullname ) {
148150
$fullfrequency = $content.data.attributes.policy.schedules[$i].frequencyseconds
149151
$fullschedule = $content.data.attributes.policy.schedules[$i].schedulename
150152
}
151-
if ( $content.data.attributes.policy.schedules[$i].schedulename -like "INCR" ) {
153+
if ( $content.data.attributes.policy.schedules[$i].schedulename -like $incrname ) {
152154
$incrfrequency = $content.data.attributes.policy.schedules[$i].frequencyseconds
153155
$incrschedule = $content.data.attributes.policy.schedules[$i].schedulename
154156
}
@@ -180,20 +182,16 @@ $headers = @{
180182
# Getting current date
181183
$a = Get-Date
182184
$currentDate=$a.ToUniversalTime()
183-
$backupTimeEnd = (Get-Date -format s -date $currentDate) + "Z"
184-
185185
# Set starting date to 30 days from current date
186186
$lookbackDate = (Get-Date).AddDays(-$days2lookback)
187187
$backupTimeStart = (Get-Date -format s -date $lookbackDate) + "Z"
188188

189189
$query_params = @{
190190
"page[limit]" = 50 # This changes the default page size to 50
191191
# The following filter variable adds a filter to only show for this client in past 30 days
192-
#"filter" = "clientName eq '$clientname' and backupTime ge $backupTimeStart and backupTime le $backupTimeEnd"
193-
"filter" = "clientName eq '$clientname'"
192+
"filter" = "clientName eq '$clientname' and backupTime ge $backupTimeStart"
194193
}
195194
if ( $verbose ) {
196-
Write-Host "backupTimeEnd = $backupTimeEnd"
197195
Write-Host "backupTimeStart = $backupTimeStart"
198196
}
199197

@@ -209,67 +207,64 @@ if ($response.StatusCode -ne 200)
209207
throw "Unable to get the list of Netbackup images!"
210208
}
211209

212-
#Write-Host "response=$response"
213-
#Write-Host "Response JSON"
214-
#$response | ConvertFrom-Json | ConvertTo-Json
215-
216210
# Convert the JSON output into PowerShell object format
217211
$content = (ConvertFrom-Json -InputObject $response)
218212

219213
# Converting the JSON data for the image attributes into an array for looping through
220214
$imageinfo = %{$content.data.attributes}
221-
#Write-Host "Image info JSON"
222-
#$imageinfo | ConvertTo-Json
223-
#Get-Member -InputObject $imageinfo
224215

225216
# Setting values to validation variables
226217
$schedulerun = "none"
227-
$fulltime = "no"
228-
$incrtime = "no"
218+
$fulltime = 0
219+
$incrtime = 0
229220

230221
# Looping through all the images found for this client looking for most recent
231-
# full and incr backup image. Image data is listed in date descending order, i.e.,
232-
# newest to oldest. We just need to capture the first instance of either backup type
222+
# full and incr backup image. We just need to capture the first instance of either backup type
233223
# Handling 3 scenarios of returned images counts:
234224
# 1 image doesn't create an array of objects so need to process
235225
# 2 or more images create array of objects to process in a loop
236226
if ( $content.meta.pagination.count -eq 1 ) {
237-
if ( $imageinfo.scheduleName -eq "FULL" ) {
238-
$fulltime= [datetime]::Parse($imageinfo.backuptime)
239-
} else {
240-
$incrtime= [datetime]::Parse($imageinfo.backuptime)
227+
if ( $imageinfo.scheduleName -eq $fullname ) {
228+
$fulltime = (Get-Date $imageinfo.backuptime)
229+
} elseif ( $imageinfo.scheduleName -eq $incrname ) {
230+
$incrtime= (Get-Date $imageinfo.backuptime)
241231
}
242232
} else {
243233
for ( $i=0; $i -lt $content.meta.pagination.count; $i++ ) {
244-
# Can skip if we've identified that we don't need to run any backups
245-
if ( $fulltime -ne "no" -AND $incrtime -ne "no" ) {
246-
continue
247-
} elseif ( $imageinfo[$i].scheduleName -eq "FULL" ) {
248-
$fulltime = [datetime]::Parse($imageinfo[$i].backuptime)
249-
} elseif ( $imageinfo[$i].scheduleName -eq "INCR" ) {
250-
$incrtime = [datetime]::Parse($imageinfo[$i].backuptime)
234+
# Depending upon the schedule name, what to perform
235+
if ( $imageinfo[$i].schedulename -eq $fullname ) {
236+
$a = Get-Date $imageinfo[$i].backuptime
237+
if ( $a -gt $fulltime ) {
238+
$fulltime=$a
239+
}
240+
} elseif ( $imageinfo[$i].schedulename -eq $incrname ) {
241+
$a = Get-Date $imageinfo[$i].backuptime
242+
if ( $a -gt $incrtime ) {
243+
$incrtime=$a
244+
}
251245
}
252246
}
253247
}
254248

249+
255250
# Define the full and incr window by subtracting the schedule frequency from
256251
# the current time.
257252
$fullwindow=$currentDate.AddSeconds(-$fullfrequency)
258253
$incrwindow=$currentDate.AddSeconds(-$incrfrequency)
259254

260255
# Now, run through the logic to determine what kind of backup to run
261-
if ( $fulltime -eq "no" ) {
256+
if ( $fulltime -eq 0 ) {
262257
# No recent backup images found for this client, run full backup
263-
$schedulerun = "FULL"
258+
$schedulerun = $fullname
264259
} elseif ( $fullwindow -ge $fulltime ) {
265260
# Found a FULL backup older than current full window
266-
$schedulerun = "FULL"
267-
} elseif ( $fulltime -ne "no" -AND $incrtime -eq "no" ) {
261+
$schedulerun = $fullname
262+
} elseif ( $fulltime -ne 0 -AND $incrtime -eq 0 ) {
268263
# Full backup found but less than window and no incremental
269-
$schedulerun = "INCR"
264+
$schedulerun = $incrname
270265
} elseif ( $incrwindow -ge $incrtime ) {
271266
# Full backup less than window and incremental older than window
272-
$schedulerun = "INCR"
267+
$schedulerun = $incrname
273268
} else {
274269
$schedulerun = "none"
275270
}
@@ -340,4 +335,4 @@ if ($response.StatusCode -ne 202)
340335

341336
if ( $verbose ) {
342337
Write-Host "Backup $schedulerun successfully started"
343-
}
338+
}

0 commit comments

Comments
 (0)