Hopefully you can help me with this...
I have a script in PS that runs after the full backup job that did the following
- Selects & inventories the tape drive
- Marks the inserted tape as Free
- Selects latest full backup from the backup repository (finds *.vbk files and selects the latest one)
- Creates a hardlink for this backup file
- Starts the duplication of the file to tape
- Ejects the tape
- Removes the hardlink
- Sends an email report
Could anyone assist me with this?
Code: Select all
############################################################
### Powershell Script for Automating Backup Duplication #
### Version: 1 #
### Date: 17/06/14 #
############################################################
# Initial Setup
Remove-Variable -Name * -Scope script -ErrorAction SilentlyContinue
$Error.clear()
$ErrorActionPreference = "Stop"
# Time & Logging
$logger = New-Object System.Text.StringBuilder
$logger.Length = 0
function Log([string]$Message)
{
$LogTime = Get-Date -Format "dd-MM-yyyy HH:mm:ss"
#$logger.Append($LogTime)
$logger.AppendLine($LogTime + " " + $Message)
}
Log "Starting script"
# SMTP
$smtpServer = "server"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
# Email Reporting
$emailFrom = "xxx"
$emailTo = "xxxx"
$subject_success = "[SUCCESS] SRV Backup Files Replicated to Tape"
$subject_failure = "[FAILURE] SRV Backup Files NOT Replicated to Tape"
# Let's set our locations
$SRV80_b2d = "D:\Veeam\Backups\Backup Job\"
# GET Veeam Module for Commands!
try
{
Log "Attempting to add the Veeam Snapin so commands can be executed"
Add-PSSnapin VeeamPSSnapin
Log "Successfully added the Veeam Snapin"
}
catch
{
Log "Error while attempting to add the Veeam Snapin: $error"
Log "Script execution FAILED"
$smtp.Send($emailFrom, $emailTo, $subject_failure, $logger.ToString())
exit
}
##################################################################
######################## BEGIN PROCESSES #########################
##################################################################
## INVENTORY THE TAPE ##
try
{
Log "Attempting to enumerate & inventory the tape drive"
Get-VBRTapeDrive | Start-VBRTapeInventory
Log "Successfully inventory'd the tape drive"
}
catch
{
Log "Error while attempting to inventory the tape drive: $error"
Log "Script execution FAILED"
$smtp.Send($emailFrom, $emailTo, $subject_failure, $logger.ToString())
exit
}
## SELECT THE TAPE ##
$tape = Get-VBRTapeMedium | Where-Object {$_.IsOnline -like "True"}
## MARK TAPE AS FREE ##
try
{
Log "Attempting to mark $tape as free"
$tape.MarkAsFree()
Log "Successfully marked $tape as free"
}
catch
{
Log "Error while attempting to mark $tape as free: $error"
Log "Script execution FAILED"
$smtp.Send($emailFrom, $emailTo, $subject_failure, $logger.ToString())
exit
}
## LATEST BACKUP SELECTION ##
# Time to get our latest full backup file and store it into a variable, ready for duplication
# It's a bit roundabout at present because the Add-VBRTapeFilesJob cmdlet can't handle files properly
Log "Finding latest full backup of SRV80"
$linkpath = "D:\Veeam\Backups\HardLinks\"
$SRV80_b2d_files = dir $SRV80_b2d -Filter "*.vbk"
$SRV80_Latest = $SRV80_b2d_files | group directory | foreach {@($_.group | sort {[datetime]$_.creationtime} -desc)[0]}
try
{
Log "Selecting latest full backup of SRV80"
$FilePath = $SRV80_Latest.DirectoryName
$FileName = $SRV80_Latest.Name
$SRV80Link = "LatestSRV80VBK.vbk"
cmd /c mklink /H $linkpath$SRV80Link $Filepath\$Filename
}
catch
{
Log "Error while selecting latest full backup: $error"
Log "Script execution FAILED"
$smtp.Send($emailFrom, $emailTo, $subject_failure, $logger.ToString())
exit
}
# Sleep for 10 seconds while Veeam releases its hold on the above hardlink
Start-Sleep -s 10
## NOT NEEDED
#try
#{
# Log "Creating job for duplicating $SRV60_Latest")
# #Add-VBRTapeFilesJob -Name "SRV60 to Tape" -Server "This server" -Path $linkpath$Filename -MediaPool "DSG Tape Media" -Masks vbk
# Log "Successfully created job")
#}
#catch
#{
# Log "Error while creating duplication job: $error")
# Log "Script execution FAILED")
# $smtp.Send($emailFrom, $emailTo, $subject_failure, $logger.ToString())
# exit
#}
################
## START JOB! ##
################
try
{
Log "Starting job to duplicate $SRV80_Latest"
Start-VBRJob -Job "SRV80 to Tape"
Log "Successfully duplicated $SRV80_Latest to tape"
$jobDuplicated = 1
Log "Ejecting the tape"
Get-VBRTapeDrive | Eject-VBRTapeDrive
Log "Successfully ejected tape"
}
catch
{
$jobDuplicated = 0
Log "Error while duplicating: $error"
Log "Script execution FAILED"
$smtp.Send($emailFrom, $emailTo, $subject_failure, $logger.ToString())
exit
}
## NOT NEEDED
#try
#{
# Log "Removing the SRV60 to Tape Job")
# #Remove-VBRJob -Job "SRV60 to Tape" -Confirm:$false
# Log "Successfully removed job")
#}
#catch
#{
# Log "Error while removing job: $error")
# $smtp.Send($emailFrom, $emailTo, $subject_failure, $logger.ToString())
# exit
#}
## REMOVE HARD LINKS ##
try
{
Log "Removing the symbolic link to the backup file"
cmd.exe /c del $linkpath$SRV80Link
Log "Successfully removed the symbolic link to the backup file"
}
catch
{
Log "Error while removing symbolic link: $error"
$smtp.Send($emailFrom, $emailTo, $subject_failure, $logger.ToString())
exit
}
# Let's wrap it up and email our report
# SRV80 will be in a different script
# EMAIL RESULTS #
if ($error.count -le 0)
{
if ($jobDuplicated = 1)
{
Log "SRV80 backup files are on tape, yay!"
Log "Script executed successfully"
$smtp.Send($emailFrom, $emailTo, $subject_success, $logger.ToString())
}
}
else
{
Log "Script execution Error"
$smtp.Send($emailFrom, $emailTo, $subject_failure, $logger.ToString())
}