Skip to content

Instantly share code, notes, and snippets.

@nikop
Last active December 15, 2015 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikop/5177218 to your computer and use it in GitHub Desktop.
Save nikop/5177218 to your computer and use it in GitHub Desktop.
Powershell script for SnapRaid to create ShadowCopies before syncing and using shadow copies for sync.
$path = "C:\SnapRaid\"
$exepath = $path + "snapraid.exe"
$config = $path +"snapraid.conf"
$config_shadow = $path + "snapraid_shadow2.conf"
$data = Get-Content $config
$file = New-Object System.IO.StreamWriter $config_shadow
$now = Get-Date
$logfile = $path + "SnapRAID-$(get-date -format yyMMdd-HHmmss).log"
$log = New-Object System.IO.StreamWriter $logfile
function writeLog($txt)
{
$log.WriteLine($txt)
}
writeLog("Start")
Set-Location $path
function GetShadowCopy($Ltr)
{
$Ltr = $Ltr.Trim("\\")
$Disk = Get-WmiObject -Class Win32_Volume | Where { $_.DriveLetter -eq $Ltr }
$LastSnap = (Get-WMIObject win32_shadowcopy) | Where { $_.VolumeName -eq $Disk.DeviceId -and $_.ClientAccessible -eq $true } | Sort InstallDate | Select-Object -Last 1
return $LastSnap
}
function CreateShadowCopy($Ltr)
{
if ((Get-WmiObject Win32_ShadowCopy -List).Create($Ltr,"ClientAccessible"))
{
return GetShadowCopy($Ltr)
}
throw "ShadowCopy Failed"
}
# Search Disks in config file
foreach ( $l in $data )
{
if ( $l.StartsWith("disk") )
{
$tmp = $l.Split(" ")
writeLog("Cheking shadow copy for: "+$tmp[2])
$sc = GetShadowCopy($tmp[2])
if ($sc)
{
$Dt = [system.management.managementdatetimeconverter]::todatetime(($sc).InstallDate)
if ($now.Subtract($Dt).TotalHours -gt 12)
{
writeLog("Updating shadow copy for: " + $tmp[2])
$sc = CreateShadowCopy($tmp[2])
}
else
{
writeLog("Skipping, less than 12 hours old")
}
}
else
{
writeLog("Creating shadow copy for: " + $tmp[2])
$sc = CreateShadowCopy($tmp[2])
}
$file.WriteLine("disk "+ $tmp[1] + " " + $sc.DeviceObject + "\")
}
else
{
$file.WriteLine($l)
}
}
writeLog("Launching snapraid...")
$file.Close()
$log.Close()
& $exepath sync -c "$config_shadow" | out-file $logfile
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment