PowerShell script exchange
Post Reply
kevin@wei.com
Influencer
Posts: 15
Liked: 4 times
Joined: Apr 04, 2012 8:49 pm
Full Name: Kevin Wood
Contact:

Script to create backup job by datastore

Post by kevin@wei.com » 2 people like this post

I wanted to share this script in case someone finds it useful. It is not pretty and may need some tweaking, but works great. Here is what it does:
1. Locate all datastores or certain datastores and uses those for the name of the backup job from a specific host. You only really need one host to query as all hosts in a cluster should see all the same volumes.
2. Creates the backup job with settings specific to a StoreOnce backup appliance. This can be altered to support any device.
3. Creates initial job at hh:mm time and increments jobs in m increments. Again, this can be adjusted.
4. Configures a post job script to run on every execution of the job.
5. Sets the number of restore points to maintain to 28. Again, can be modified.
6. Sets number of days to retain deleted vms in backups (28, but can be modified).

There are a lot of prompts listed, but currently unused. They will be implemented to prompt users for the above information instead of editing the script every time. I did find out a nice side effect to this program. I wanted to be able to run the script periodically to create new backup jobs for newly created datastores. I wanted to be sure it didn't create duplicate jobs. Well, Veeam does not allow a job to be created with the an already existing name, so you could potentially set this up as a pre/post script to create jobs every night for any new datastores created during the day.

I appreciate any feedback or tweaks to the script. I hope someone finds some use for this.

Here it is:
----------------------------------------------------------------------------------------------------

Code: Select all

#Questions:
# Why is a prescript called a prescript and a post script called a commandline?

Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
write-host "This script created by Kevin Wood at Worldcom Exhange, Inc. on 7-27-2016"
write-host "This script will automatically build backup jobs based on datastores and their names"
write-host "Jobs will start at HH:mm time and increment every X minutes"
write-host "Collecting Host information"
$datastore = Get-VBRServer -Name "sbo3-ta00-vh57.hosting.local" |Find-VBRViDatastore -Name "sbo3-ta00-3p01-clus08-ds*"
write-host "Collecting Data Stores"
$datastorename = Get-VBRServer -Name "sbo3-ta00-vh57.hosting.local" |Find-VBRViDatastore -Name "sbo3-ta00-3p01-clus08-ds*"|select name
write-host "Setting Backup Target"
$repository = Get-VBRBackupRepository -Name "Veeam_BS3_BO3_TA00_SS1_S1"
$RestorePoints = "28" # Restore Points
$RetainDaysadd = "28" # VM deleted retention period in days
$starttime = Get-Date "00:00"
write-host "Setting start time to " $starttime
$timeincrement = 5
write-host "Setting time increments to gap backups to " $timeincrement

for ($i=0; $i -lt $datastore.length; $i++) {
$d = $datastorename[$i].Name
    write-host "Creating backup job " $d
    Add-VBRViBackupJob -Name $d -Entity $datastore[$i] -BackupRepository $repository 
    $Jobname = Get-VBRJob -Name $d
    $JobOptions = $Jobname | Get-VBRJobOptions
    write-host "Setting Restore points to " $RestorePoints
    $JobOptions.BackupStorageOptions.RetainCycles = $RestorePoints
    $JobOptions.BackupStorageOptions.EnableDeletedVmDataRetention = $true
    write-host "Setting Advanced Storage Options for job " $d
    Set-VBRJobAdvancedStorageOptions -Job $Jobname -enablededuplication $FALSE -CompressionLevel 5 -StorageBlockSize KbBlockSize4096
    write-host "Setting Advanced Options for job " $d
    Set-VBRJobAdvancedOptions -Job $Jobname -EnableIntegrityChecks $True -Enabled $True -CommandLine "C:\Program Files\Veeam\Backup and Replication\Scripts\job_date_retention.bat" -Retaindays $Retaindaysadd
    write-host "Setting Advanced Vi Options for job " $d
    Set-VBRJobAdvancedViOptions -Job $Jobname -EnableChangeTracking $True -UseChangeTracking $True -ExcludeSwapFile $True 
    write-host "Setting Advanced Backup Options for job " $d
    Set-VBRJobAdvancedBackupOptions -Job $Jobname -Algorithm Incremental -TransformFullToSyntethic $True -TransformToSyntethicDays Sunday -EnableFullBackup $True -FullBackupScheduleKind Monthly -DayNumberInMonth Second -FullBackupDays Sunday
    $time = Get-Date -f HH:mm $starttime
    Get-VBRJob -Name $d | Set-VBRJobSchedule -Daily -At $time -DailyKind Everyday
    write-host "Setting backup schedule for job" $d "to" $time
    [datetime]$starttime = $starttime.AddMinutes(5)
}
kevin@wei.com
Influencer
Posts: 15
Liked: 4 times
Joined: Apr 04, 2012 8:49 pm
Full Name: Kevin Wood
Contact:

Re: Script to create backup job by datastore

Post by kevin@wei.com » 1 person likes this post

Here is an updated version of the code. The only prompt I am working on now is being able to select the proxy/proxies you want to run the jobs on. Currently this prompts you for all the info when you run it. I also want to set all the variables up so you can pre-define everything and then just run it. Not sure if there is a concensus one way or the other.

Use and enjoy.

Code: Select all

Add-PSSnapin VeeamPSSnapin
write-host "Veeam datastore job creation utility written by Kevin Wood @ Worldcom Exchange, Inc."
write-host "Version 2.0"
write-host "This script will automatically build Veeam backup jobs based on datastores and their names."
write-host "Please enter the information requested at the prompts."
write-host "Collecting Backup Information"
$hostname = Read-host -Prompt 'Please enter a hostname contained within the cluster you wish to backup'
$dsentry = Read-host -Prompt 'Please enter the first x letters of the datastores you would like to backup (or * for all)'
$repentry = Read-host -Prompt 'Please enter the name of the repository you would like backups sent to'
$proxyentry = Read-host -Prompt 'Please enter the name of the proxy you would like backup run from'
$scriptentry = Read-host -Prompt 'Please enter the pre-job script fulle path and name that need to be executed'
$RestorePoints = Read-host -Prompt 'Please enter the number of restore points you wish to keep'
$timeentry = Read-host -Prompt 'Please enter the start time for the jobs to begin.  Enter in a 24 format i.e. 13:11'
$timeincrement = Read-host -Prompt 'Please enter the time between job starts in minutes'
$starttime = Get-Date $timeentry
$dsentry = $dsentry + "*"
$datastore = Get-VBRServer -Name $hostname |Find-VBRViDatastore -Name $dsentry
$datastorename = Get-VBRServer -Name $hostname |Find-VBRViDatastore -Name $dsentry|select name
$repository = Get-VBRBackupRepository -Name $repentry
$RetainDaysadd = "28" # VM deleted retention period in days
write-host "Setting time increments to gap backups to " $timeincrement
$proxyentry = "VMware Backup Proxy"
$SourceProxy = Get-VBRViProxy

for ($i=0; $i -lt $datastore.length; $i++) {
$d = $datastorename[$i].Name
write-host "Creating backup job " $d
Add-VBRViBackupJob -Name $d -Entity $datastore[$i] -BackupRepository $repository
$Jobname = Get-VBRJob -Name $d
$JobOptions = $Jobname | Get-VBRJobOptions
write-host "Setting Restore points to " $RestorePoints
$JobOptions.BackupStorageOptions.RetainCycles = $RestorePoints
$JobOptions.BackupStorageOptions.EnableDeletedVmDataRetention = $true
$Joboptions.JobOptions.RunManually = $false
write-host "Setting Advanced Storage Options for job " $d
Set-VBRJobAdvancedStorageOptions -Job $Jobname -enablededuplication $FALSE -CompressionLevel 5 -StorageBlockSize KbBlockSize4096
write-host "Setting Advanced Options for job " $d
Set-VBRJobAdvancedOptions -Job $Jobname -EnableIntegrityChecks $True -EnablePreScript $True -PreJobScript $scriptentry -Retaindays $Retaindaysadd
write-host "Setting Advanced Vi Options for job " $d
Set-VBRJobAdvancedViOptions -Job $Jobname -EnableChangeTracking $True -UseChangeTracking $True -ExcludeSwapFile $True 
write-host "Setting Advanced Backup Options for job " $d
Set-VBRJobAdvancedBackupOptions -Job $Jobname -Algorithm Incremental -TransformFullToSyntethic $True -TransformToSyntethicDays Sunday -EnableFullBackup $True -FullBackupScheduleKind Monthly -DayNumberInMonth Second -FullBackupDays Sunday
$time = Get-Date -f HH:mm $starttime
Get-VBRJob -Name $d | Set-VBRJobSchedule -Daily -At $time -DailyKind Everyday
Set-VBRJobProxy -Job $jobname -Proxy $SourceProxy
write-host "Setting backup schedule for job" $d "to" $time
[datetime]$starttime = $starttime.AddMinutes($timeincrement)
}
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests