2009
11.16


I’ve written a Powershell script to execute the diskshadow script I mentioned earlier, and implemented a couple of ideas I had.

This script will create full backups of Hyper-V guests while they are running.

First, this script will check to see if the destination folder has any VHDs that were backed up more than 10 days ago (since this runs every two weeks it will remove the oldest of the two backups that are stored). It then starts the diskshadow script that mounts a shadow copy of the drive storing the virtuals to another drive (the Z: in my script below). Finally, it copies the VHDs from the Z: to a folder on another drive (the destination in this case is the D:) and labels the folder by date.

Here is the main Powershell script: vhdfull.ps1

#Set Date Variables
$folderdate = get-date -uformat "%m-%d-%y"
$Now = Get-Date
$DelDate = $Now.AddDays(-10)

#Start Transcript
start-transcript -path c:vhdscripttmp.txt

#Look at destination folder and remove older backups
Get-ChildItem D:VHDBackups |Where {$_.CreationTime -le "$DelDate"}|remove-item -recurse -Verbose
remove-item c:\vsbackup\tmp2.txt

#Run diskshadow, and save log for pushing into string builder for e-mail alert
diskshadow /s c:\vhdscriptvhdshadow1.txt >> c:\vsbackuptmp2.txt

#Copy the files from the disk shadow copy.
Copy-Item Z:virtuals D:VHDBackups$folderdate -recurse -Verbose

#End Transcript
stop-transcript

#Remove Disk Shadow
diskshadow /s c:vhdscriptvhdshadow2.txt


#Create string builder to format logs
$strbuild = new-object System.Text.StringBuilder

#Add line breaks to both logs, and glue them together
foreach ($line in get-content "C:vhdscripttmp2.txt")
{
$strbuild.Append($line).Append("<br/>")
}
foreach ($line in get-content "C:vhdscripttmp.txt")
{
$strbuild.Append($line).Append("<br/>")
}

#Send it.
$message = new-object System.Net.Mail.MailMessage("VHDBackup@localhost", "support@domain.com")
$message.Subject = " - VHDBackup"
$message.IsBodyHtml = $True
$message.Body = "<font size=2 face=Arial>"+$strbuild
$smtp = new-object Net.Mail.SmtpClient("<IP OF SMTP SERVER HERE>")
$smtp.Send($message)

Here is the first diskshadow script: vhdshadow1.txt

set context persistent
add volume e: alias VHDs
set verbose on
create
expose %VHDs% z:

… and the second: vhdshadow2.txt

delete shadows all



No Comment.

Add Your Comment