|
# Powershell script # Backup check V1.0 by Duckplaza.com
# Enter the days to check the files for. Files for the last $DaysToCheck will be compressed $DaysToCheck = 1
# Enter the days to remove the files for. These are the days before the last day of $DaysToCheck! # Set $RemoveFiles to 1 to remove old files, otherwise leave as 0. $RemoveFiles = 0 $DaysToRemove = 0
# Enable or disable (1 or 0) the checks $TrnCheck = 0 $BakCheck = 1
# Directories to check, separated by comma's $TrnDirsToCheck = "D:\SQLBackup" ###### Backup Directory ###### $BakDirsToCheck = "D:\SQLBackup" ###### Backup Directory ######
# TRN files to ignore, separated by comma's $TrnIgnore = "master", "Northwind", "model", "msdb", "pubs", "tempdb" , "other"
# BAK files to ignore, separated by comma's $BakIgnore = "Northwind"
# Check for Gzipped (.gz) files. 1 is enabled, 0 is disabled $Gzipped = 0
# Specify multiple recipients by comma-separating them, e.g.: " Dit e-mailadres wordt beveiligd tegen spambots. JavaScript dient ingeschakeld te zijn om het te bekijken. This e-mail address is being protected from spambots. You need JavaScript enabled to view it " $SendLogTo=" Dit e-mailadres wordt beveiligd tegen spambots. JavaScript dient ingeschakeld te zijn om het te bekijken. This e-mail address is being protected from spambots. You need JavaScript enabled to view it "
# Sometimes backups are started before midnight. In this case, it is useful to ignore # the current day, as there are no backups made yet. Set $IgnoreDays to 1 to not # check the current date. Otherwise, set it to 0. $IgnoreDays = 0
# END CONFIGURATION ############################################################################# # Do not edit anything below this line unless you know what you're doing!!! # #############################################################################
$Version = "(c)2010 Duckplaza.com" $MailServer = "127.0.0.1" #### Provide email server ##### $serverPort = 25
$hostname = $env:COMPUTERNAME.toLower().toString()
$domain=$env:USERDNSDOMAIN.toLower().toString() #### Alleen gebruiken waneer in een domain anders b.v. $domain="pietje.nl"
$now = Get-Date $timestamp = Get-Date -format "yyyyMMdd HH:mm"
If ($IgnoreDays -eq 0) { $DaysToCheck += -1 }
function CheckDir([array]$DirsToCheck, [array]$Ignore, [string]$ext) { $err = 0 foreach ($MainDir in $DirsToCheck) { cd $MainDir ForEach ($CheckDir in Get-ChildItem) { $tmpnam = $checkdir.Name ForEach ($IgnoreName in $Ignore) { if ($IgnoreName -eq $tmpnam) { $nowai = 1 # The Database is not scheduled for backup, so ignore } } if ($nowai -ne 1) { $nowai = 0 cd $CheckDir $DTC = $DaysToCheck while ($DTC -gt ($IgnoreDays - 1)) { $TmpDTC = $DTC * -1 $CheckDay = $now.AddDays($TmpDTC) $yr=$CheckDay.Year $mo=(($CheckDay.Month).ToString()).PadLeft(2,"0") $dy=(($CheckDay.Day).ToString()).PadLeft(2,"0")
if ($Gzipped -eq 1) { $tmp = "*" + $yr + $mo + $dy + "????." + $ext + ".gz" } else { $tmp = "*" + $yr + $mo + $dy + "????." + $ext } $file = Get-ChildItem * -include $tmp if ($file -ne $null) { $txt+= "$file exists.`n" } else { $txt+= "$MainDir\$CheckDir\$tmp no new file with date $yr$mo$dy is missing`n" $err++ } $DTC -= 1 } if ($RemoveFiles -eq 1) { $DTR = $DaysToRemove While (($DTR + $IgnoreDays + $DaysToCheck - 1) -gt ($IgnoreDays + $DaysToCheck)) { $TmpDTR = $DTR * -1 $CheckRemove = $now.AddDays($TmpDTR) $yr=$CheckRemove.Year $mo=(($CheckRemove.Month).ToString()).PadLeft(2,"0") $dy=(($CheckRemove.Day).ToString()).PadLeft(2,"0")
$tmp = "*" + $yr + $mo + $dy + "????." + $ext $file = Get-ChildItem * -include $tmp if ($file -ne $null) { $txt+= " !!!REMOVING!!! $file`n" Remove-Item -path $file -force } if ($Gzipped -eq 1) { $tmp = "*" + $yr + $mo + $dy + "????." + $ext + ".gz" $file = Get-ChildItem * -include $tmp if ($file -ne $null) { $txt+= " !!!REMOVING!!! $file`n" Remove-Item -path $file -force } } $DTR -= 1 } } cd .. } else { $nowai = 0 } } } return $txt, $err }
if ($TrnCheck -eq 1) { $trn = CheckDir $TrnDirsToCheck $TrnIgnore "TRN" $testresult += $trn[0] $err += $trn[1] } if ($BakCheck -eq 1) { $bak = CheckDir $BakDirsToCheck $BakIgnore "BAK" $testresult += $bak[0] $err += $bak[1] }
if ($err -ge 1) { $Subject = "MSSQL Backup Error " + $(get-itemproperty "HKLM:\SOFTWARE\Quest Software\BigBrother\bbnt\AliasName")."(default)" + " $timestamp" $SendLogTo+="" } else { $Subject = "MSSQL Backup OK " + $(get-itemproperty "HKLM:\SOFTWARE\Quest Software\BigBrother\bbnt\AliasName")."(default)" + " $timestamp" }
# create mailerobject $sc = New-Object Net.Mail.SmtpClient -arg $mailserver, $serverport
$from = "Backup-$Env:ComputerName" + "@" + "DOMEIN NAAM" ##### plaats hier u domein naam $message = "The following information is received:`n" $message += "__________________________________________________`n`n" $message += $testresult $message += "`n`nThis machine uses the following IP Address(es): `n" $ips = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.IPEnabled} ForEach ($ip in $ips) { If ($ip.IPAddress.Length -gt 0) { ForEach ($ipaddr in $ip.IPAddress) { If ($ipaddr -ne "0.0.0.0") { $message += "`t$ipaddr`n" } } } } $message += "__________________________________________________`n" $message += $version
# Send e-mail $sc.Send($from, $SendLogTo, $Subject, $message) #echo $from, $SendLogTo, $Subject, $message
|