WBADMIN Backup Script via Scheduled Task (Windows Backup)

WBADMIN Backup Script via Scheduled Task (Windows Backup)

# Backup Script for wbadmin (Windows Backup)
# Script created by Martijn Kamminga
# PowerShell Backup Report
# Example usage: .\backup-script.ps1 .\list.txt
# Remember that list.txt is the file containing a list of Server names to run this against
# v1.1
# Free to use and distribute as long as above comments are kept within the file.

# Set Weekly and Monthly backups. 7 day' s 1 week and/or 4-5 weeks a month
$Weekly = "No"
$Montly = "Yes"

# Set the Computer Name Variable
$ComputerName = ${env:computername}
$Path2File = (Get-Item -Path ".\" -Verbose).FullName
$File = "\list.txt"
$ServerListToProcess = $Path2File + $File
If(!(Test-Path $ServerListToProcess)) {
Write-Host "Initial File with Server does not Exists..... creating"
New-Item $ServerListToProcess -Type File
Add-Content $ServerListToProcess "$ComputerName"
}
else {
Write-Host $ServerListToProcess
Write-Host File Allready exists
}
# Get the Server out of the list.txt file
$computers = get-content $ServerListToProcess #grab the names of the servers/computers to check from the list.txt file.

#Set Up Variables for Sending Mail
$smtpServer = "x.x.x.x"
$subject = "Backup Script Executed on $ComputerName"
$sendfrom = "Dit e-mailadres wordt beveiligd tegen spambots. JavaScript dient ingeschakeld te zijn om het te bekijken."
$sendTo = "Dit e-mailadres wordt beveiligd tegen spambots. JavaScript dient ingeschakeld te zijn om het te bekijken."

#Set Variables for Weekday Backup Location
$BackupLocation = "\\x.x.x.x\backupfolder\"
$FolderToday = "{0:ddd}" -f (get-date)
$Folder1 = "1"
$Folder2 = "2"
$Folder3 = "3"
$Folder4 = "4"
$Folder5 = "5"
$Folder6 = "6"
$Folder7 = "7"

$BackupToday1 = $BackupLocation + $ComputerName + $FolderToday + $Folder1
$BackupToday2 = $BackupLocation + $ComputerName + $FolderToday + $Folder2
$BackupToday3 = $BackupLocation + $ComputerName + $FolderToday + $Folder3
$BackupToday4 = $BackupLocation + $ComputerName + $FolderToday + $Folder4
$BackupToday5 = $BackupLocation + $ComputerName + $FolderToday + $Folder5
$BackupToday6 = $BackupLocation + $ComputerName + $FolderToday + $Folder6
$BackupToday7 = $BackupLocation + $ComputerName + $FolderToday + $Folder7

# Begin schedule
# ----------------------
$date = (date -Hour 0 -Minute 0 -Second 0)
$now = Get-Date
$currentMonth = $now.Month
# ----------------------
$setweekday1 = "Monday"
$setweekday2 = "Tuesday"
$setweekday3 = "Wednesday"
$setweekday4 = "Thursday"
$setweekday5 = "Friday"
$setweekendday6 = "Saturday"
$setweekendday7 = "Sunday"
# ----------------------

# Weekly backup with 7 day's retention
If ($Weekly -eq "Yes") {
# Day 1
if (($date.DayOfWeek -eq $setweekday1) -or ($date.DayOfWeek -eq $setweekday2) -or ($date.DayOfWeek -eq $setweekday3) -or ($date.DayOfWeek -eq $setweekday4) -or ($date.DayOfWeek -eq $setweekday5) -or ($date.DayOfWeek -eq $setweekendday6) -or ($date.DayOfWeek -eq $setweekendday7)) {
Write-Host Day is $date
Write-Host Execute your command here
Remove-Item -Path $BackupToday1 -Force -recurse
New-Item $BackupToday1 -Type Directory
$note = "Backup Today: $date"
C:\Windows\System32\wbadmin.exe start backup -include:c: -backupTarget:$BackupToday1 -quiet
}
# Day 2
if (($date.DayOfWeek -eq $setweekday1) -or ($date.DayOfWeek -eq $setweekday2) -or ($date.DayOfWeek -eq $setweekday3) -or ($date.DayOfWeek -eq $setweekday4) -or ($date.DayOfWeek -eq $setweekday5) -or ($date.DayOfWeek -eq $setweekendday6) -or ($date.DayOfWeek -eq $setweekendday7)) {
Write-Host Day is $date
Write-Host Execute your command here
Remove-Item -Path $BackupToday2 -Force -recurse
New-Item $BackupToday2 -Type Directory
$note = "Backup Today: $date"
C:\Windows\System32\wbadmin.exe start backup -include:c: -backupTarget:$BackupToday2 -quiet
}
# Day 3
if (($date.DayOfDay -eq $setweekday1) -or ($date.DayOfWeek -eq $setweekday2) -or ($date.DayOfWeek -eq $setweekday3) -or ($date.DayOfWeek -eq $setweekday4) -or ($date.DayOfWeek -eq $setweekday5) -or ($date.DayOfWeek -eq $setweekendday6) -or ($date.DayOfWeek -eq $setweekendday7)) {
Write-Host Day is $date
Write-Host Execute your command here
Remove-Item -Path $BackupToday3 -Force -recurse
New-Item $BackupToday3 -Type Directory
$note = "Backup Today: $date"
C:\Windows\System32\wbadmin.exe start backup -include:c: -backupTarget:$BackupToday3 -quiet
}
# Day 4
if (($date.DayOfWeek -eq $setweekday1) -or ($date.DayOfWeek -eq $setweekday2) -or ($date.DayOfWeek -eq $setweekday3) -or ($date.DayOfWeek -eq $setweekday4) -or ($date.DayOfWeek -eq $setweekday5) -or ($date.DayOfWeek -eq $setweekendday6) -or ($date.DayOfWeek -eq $setweekendday7)) {
Write-Host Day is $date
Write-Host Execute your command here
Remove-Item -Path $BackupToday4 -Force -recurse
New-Item $BackupToday4 -Type Directory
$note = "Backup Today: $date"
C:\Windows\System32\wbadmin.exe start backup -include:c: -backupTarget:$BackupToday4 -quiet
}
# Day 5
if (($date.DayOfWeek -eq $setweekday1) -or ($date.DayOfWeek -eq $setweekday2) -or ($date.DayOfWeek -eq $setweekday3) -or ($date.DayOfWeek -eq $setweekday4) -or ($date.DayOfWeek -eq $setweekday5) -or ($date.DayOfWeek -eq $setweekendday6) -or ($date.DayOfWeek -eq $setweekendday7)) {
Write-Host Day is $date
Write-Host Execute your command here
Remove-Item -Path $BackupToday5 -Force -recurse
New-Item $BackupToday5 -Type Directory
$note = "Backup Today (Leap Date): $date"
C:\Windows\System32\wbadmin.exe start backup -include:c: -backupTarget:$BackupToday5 -quiet
}
# UnComment these 2 if's if you want the week backups to carry on in the weekend (double backup if month is set to yes).
# Day 6
#if (($date.DayOfWeek -eq $setweekday1) -or ($date.DayOfWeek -eq $setweekday2) -or ($date.DayOfWeek -eq $setweekday3) -or ($date.DayOfWeek -eq $setweekday4) -or ($date.DayOfWeek -eq $setweekday5) -or ($date.DayOfWeek -eq $setweekendday6) -or ($date.DayOfWeek -eq $setweekendday7)) {
# Write-Host Day is $date
# Write-Host Execute your command here
# Remove-Item -Path $BackupToday6 -Force -recurse
# New-Item $BackupToday6 -Type Directory
# $note = "Backup Today: $date"
# C:\Windows\System32\wbadmin.exe start backup -include:c: -backupTarget:$BackupToday6 -quiet
# }
## Day 7
#if (($date.DayOfWeek -eq $setweekday1) -or ($date.DayOfWeek -eq $setweekday2) -or ($date.DayOfWeek -eq $setweekday3) -or ($date.DayOfWeek -eq $setweekday4) -or ($date.DayOfWeek -eq $setweekday5) -or ($date.DayOfWeek -eq $setweekendday6) -or ($date.DayOfWeek -eq $setweekendday7)) {
# Write-Host Day is $date
# Write-Host Execute your command here
# Remove-Item -Path $BackupToday7 -Force -recurse
# New-Item $BackupToday7 -Type Directory
# $note = "Backup Today: $date"
# C:\Windows\System32\wbadmin.exe start backup -include:c: -backupTarget:$BackupToday7 -quiet
# }

}
else {
Write-Host Weekly backup is set to another value then Yes.
}

# Monthly backup with 5 day retention
If ($Montly -eq "Yes" ) {
# Week 1
if(($date.Day -le 7) -and (($date.DayOfWeek -eq $setweekday1) -or ($date.DayOfWeek -eq $setweekday2) -or ($date.DayOfWeek -eq $setweekday3) -or ($date.DayOfWeek -eq $setweekday4) -or ($date.DayOfWeek -eq $setweekday5) -or ($date.DayOfWeek -eq $setweekendday6) -or ($date.DayOfWeek -eq $setweekendday7))) {
Write-Host Day is $date
Write-Host Execute your command here
Remove-Item -Path $BackupToday1 -Force -recurse
New-Item $BackupToday1 -Type Directory
$note = "Backup Today: $date"
C:\Windows\System32\wbadmin.exe start backup -include:c: -backupTarget:$BackupToday1 -quiet
}
Else {
Write-Host This is not a weekday in the first week of the month
Write-Host $date
Write-Host Commands need not to be executed
}
# Week 2
if (($date.Day -gt 7) -and ($date.Day -le 14) -and (($date.DayOfWeek -eq $setweekday1) -or ($date.DayOfWeek -eq $setweekday2) -or ($date.DayOfWeek -eq $setweekday3) -or ($date.DayOfWeek -eq $setweekday4) -or ($date.DayOfWeek -eq $setweekday5) -or ($date.DayOfWeek -eq $setweekendday6) -or ($date.DayOfWeek -eq $setweekendday7))) {
Write-Host Day is $date
Write-Host Execute your command here
Remove-Item -Path $BackupToday2 -Force -recurse
New-Item $BackupToday2 -Type Directory
$note = "Backup Today: $date"
C:\Windows\System32\wbadmin.exe start backup -include:c: -backupTarget:$BackupToday2 -quiet
}
Else {
Write-Host This is not a weekday in the second week of the month
Write-Host $date
Write-Host Commands need not to be executed
}
# Week 3
if(($date.Day -gt 14) -and ($date.Day -le 21) -and (($date.DayOfWeek -eq $setweekday1) -or ($date.DayOfWeek -eq $setweekday2) -or ($date.DayOfWeek -eq $setweekday3) -or ($date.DayOfWeek -eq $setweekday4) -or ($date.DayOfWeek -eq $setweekday5) -or ($date.DayOfWeek -eq $setweekendday6) -or ($date.DayOfWeek -eq $setweekendday7))) {
Write-Host Day is $date
Write-Host Execute your command here
Remove-Item -Path $BackupToday3 -Force -recurse
New-Item $BackupToday3 -Type Directory
$note = "Backup Today: $date"
C:\Windows\System32\wbadmin.exe start backup -include:c: -backupTarget:$BackupToday3 -quiet
}
Else {
Write-Host This is not a weekday in the third week of the month
Write-Host $date
Write-Host Commands need not to be executed
}
# Week 4
if(($date.Day -gt 21) -and ($date.Day -lt 28) -and (($date.DayOfWeek -eq $setweekday1) -or ($date.DayOfWeek -eq $setweekday2) -or ($date.DayOfWeek -eq $setweekday3) -or ($date.DayOfWeek -eq $setweekday4) -or ($date.DayOfWeek -eq $setweekday5) -or ($date.DayOfWeek -eq $setweekendday6) -or ($date.DayOfWeek -eq $setweekendday7))) {
Write-Host Day is $date
Write-Host Execute your command here
Remove-Item -Path $BackupToday4 -Force -recurse
New-Item $BackupToday4 -Type Directory
$note = "Backup Today: $date"
C:\Windows\System32\wbadmin.exe start backup -include:c: -backupTarget:$BackupToday4 -quiet
}
Else {
Write-Host This is not a weekday in the fourth week of the month
Write-Host $date
Write-Host Commands need not to be executed
}
# Week 5
if (($date.Day -ge 28) -and ($date.Day -le 31) -and (($date.DayOfWeek -eq $setweekday1) -or ($date.DayOfWeek -eq $setweekday2) -or ($date.DayOfWeek -eq $setweekday3) -or ($date.DayOfWeek -eq $setweekday4) -or ($date.DayOfWeek -eq $setweekday5) -or ($date.DayOfWeek -eq $setweekendday6) -or ($date.DayOfWeek -eq $setweekendday7))) {
Write-Host Day is $date
Write-Host Execute your command here
Remove-Item -Path $BackupToday5 -Force -recurse
New-Item $BackupToday5 -Type Directory
$note = "Backup Today: $date"
C:\Windows\System32\wbadmin.exe start backup -include:c: -backupTarget:$BackupToday5 -quiet
}
Else {
Write-Host This is not a weekday in the last week of the month
Write-Host $date
Write-Host Commands need not to be executed
}
}
else {
Write-Host Monthly backup is set to other value then Yes.
}
# End schedule

$StartEmailLayout = @"
http://www.w3.org/TR/html4/frameset.dtd">
My Systems Report


"@

foreach ($computer in $computers) {

$BackupReport = @()
$BackupEvent = Get-WinEvent -ComputerName $Computer -LogName "Microsoft-Windows-Backup" -MaxEvents 3
foreach ($event in $BackupEvent) {
$row = New-Object -Type PSObject -Property @{
Message = $event.Message
LevelDisplayName = $event.LevelDisplayName
Id = $event.Id
TimeCreated = $event.TimeCreated
}
$BackupReport += $row
}

$BackupReport = $BackupReport | ConvertTo-Html -Fragment

# Setup Mail Format
$bodyinfo = @"


 


$Computer Backup Report


$note


These are the last 3 Backup Log Events


The following is a list of the last 3 items Backup log events on $Computer


$BackupReport
"@
# Add the current System HTML Report into the final HTML Report body
$EventInfo += $bodyinfo

}
$EndEmailLayout = @"




"@

# Assemble the final report from all our HTML sections
$Content = $StartEmailLayout + $EventInfo + $EndEmailLayout
# Save the report out to a file in the current path
$DateStr = Get-Date -format "yyyy-MM-dd-hh-mm"
$AbsolutePath = (Get-Item -Path ".\" -Verbose).FullName
$BackupLogsDir = "\BackupLogs"
$LogLocation = $AbsolutePath + $BackupLogsDir
If(!(Test-Path $LogLocation)) {
# Folder does NOT Exists
Write-Host "Initial Backup Log folder does not Exists..... creating"
New-Item $LogLocation -Type Directory
}
$Content | Out-File "$LogLocation\$Computer $DateStr Backup Report.html"
# Email our report out
send-mailmessage -from $sendfrom -to $sendTo -subject "$ComputerName Back-up Report" -BodyAsHTML -body $Content -priority high -smtpServer $smtpServer