techgeek.biz being a geek is fun

[ Home ] [ Tech Tips ] [ Getting Started ] [ Contact ] [ About ]  
PowerShell | Windows Backup with email notifications

PowerShell | Windows Backup with email notifications 

 

We are always wanting a good solid backup in case something hits the fan and how much do we want to pay for this software? Well typically $0. Well $0 if it works! So lets have a look at wbadmin which is a builtin windows tool that uses Volume Shadow Copy Service.

 

Who is this aimed at?

These ideas are aimed at single PC, different configured machines, small environment or part of remote monitoring system. Typically in large environment you will just have a few images on a Server that you can push to a PC whenever it is going to crap or taking to long to fix. Plus in bigger environments you have standard apps and some kind of central storage for data.

Contents

  1. Security
  2. Introduction to wbadmin
  3. One Off Backups
  4. Scheduled Backups
  5. Scheduled Backups with Email Confirmations 
  6. Final Script 

1. Security

One thing to bare in mind when it comes to backups is SECURITY if you make backup and just leave it anywhere someone can just access all your data. So we want secure it whether be stored on external drives with Bitlocker encryption, on network shares with EFS (Encrypted File System) and Isolated from anyone but who really needs it. so creating a backup user account just for backups. This depends on your environment and where its stored of course.

But of course just think about someone stealing your backup and what that means to you and take a few mins to secure it.

 

2. Introduction to wbadmin

From Wikipedia, the free encyclopedia  https://en.wikipedia.org/wiki/WBAdmin

WBAdmin (WBAdmin.exe) is a command-line utility built into Windows VistaWindows Server 2008Windows 7Windows Server 2008 R2Windows 8 and Windows Server 2012. The command is used to perform backups and restores of operating systems, drive volumes, files, folders, and applications from a command-line interface. WBAdmin is a disk based backup system. WBAdmin can create a “bare metal” backup used to restore the Windows operating system to similar OR dissimilar hardware. The Backup file(s) created are primarily in the form of Microsoft’s “Virtual Hard Disk” (.VHD) files with some accompanying .xml configuration files. The backup .VHD file can be mounted in Windows Disk Manager to view content. However, the .VHD backup file is not a direct “disk clone”.

 

Technet on wbadmin

https://technet.microsoft.com/en-us/library/cc754015(v=ws.11).aspx

 

3. One Off Backups

One off Backups are great for just in case your doing something huge to the system or upgrading and want the most up to date backup possible.

Easy as pie. And who doesn’t like pie?

Of course anytime run as from elevated Powershell or cmd for all these backup options.

wbadmin /?

wbadmin START BACKUP /?

wbadmin ENABLE BACKUP /?

One Time backup Example to d Drive. This is my favourite as to do and the -quiet means no stress.

wbadmin start backup –backuptarget:d: –allcritical 

wbadmin start backup –backuptarget:d: –allcritical -quiet

One Time backup to network share. As well as extra folders with the -include

wbadmin start backup –backuptarget:\\backupserver\backups -include:e\donutfactory,h:\secret –allcritical

wbadmin start backup –backuptarget: \\backupshare\backup1 -nonrecurseinclude:d:\folder1

One Time backup to network share using a mountpoint and -noInheritAcl 

wbadmin start backup -backuptarget:f: -include:e:,d:\mountpoint,\\?\Volume{cc566d14-44a0-11d9-9d93-806e6f6e6963}\

 

4. Scheduled Backups

Okay so we want these done daily yea? Incrementally? maybe a full backup weekly?

So we have two options

  1. Using the built in command in wbadmin. As far as i know this is not very flexible and can only backup everyday. eg backup 3am everyday.

wbadmin enable backup -schedule:03:00 –backupTarget:d:  –allcritical -quiet

2.  Making this a Scheduled Task. This should tickle your fancy. Of course in powershell.

This is setup in a few different stages one to make a script to backup whatever you want and  to make a scheduled task where you compile all the data and throw it into “Register-ScheduldedTask” at the end.

FIRE UP THE ISE!!!!

wbadmin start backup -backuptarget:D: -allcritical -quiet

Wbadmin Script and save to “C:\scripts\backup.ps1″

 

Scheduled Task Setup

They way this  actually all comes together is intresting atleast. It pays to have template saved for setting up Scheduled Tasks.

$NewScheduledTaskAction = New-ScheduledTaskAction -Execute “PowerShell.exe” -Argument “-ExecutionPolicy Bypass C:\scripts\backup.ps1”

$NewScheduledTaskTrigger = New-ScheduledTaskTrigger -Daily -At ‘3AM’

$NewScheduledTask = New-ScheduledTask -Action $NewScheduledTaskAction -Trigger $NewScheduledTaskTrigger

#You need admin privildges to run wbadmin

$NewScheduledTask | Register-ScheduledTask -TaskName “Backup Script” -User “AdministratorUser” -Password “PasswordForAdmin”

 

5. Scheduled Backups with Email Confirmations 

I have a quick blog on how to use Send-MailMessage at /techtips/powershell-using-send-mailmessage-to-send-email/ 

So the Scheduled Task Setup stays the same as before it is only the scipt that needs a bit of love. To keep it cool we will build ontop of what we have already with C:\scripts\backup.ps1″ 

 

wbadmin start backup -backuptarget:D: -allcritical -quiet

# Credentials for Send-Mail Message
$UserName = “[email protected] ”
$Password = Get-Content .\SecurePassword.txt | ConvertTo-SecureString
$Credential = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $UserName, $Password

# Send-MailMessage Email
Send-MailMessage -To [email protected] -From [email protected] -Credential $Credential -SmtpServer test.techgeek.biz -UseSsl -Subject “Backup -Body “Backup script completed”

 

Thats pretty straight forward yea? But lacking in my eyes. It definitely needs some flavour.

Like some logs and some information and what pc this came from.

 

6. Final Script. FIRE UP THE ISE!!!

So lets add that flavour. The windows backup log is actually pretty poor in quality i find so we will grab.Size of backup before and after. Because if you backup isn’t growing there is something wrong.

(Get-ChildItem $BackupDrive\WindowsImageBackup\ -Recurse | Measure-Object -Property Length -Sum).Sum / 1GB

And lets grab event logs about windows backup. Which will give you plenty of detail.

Get-EventLog -LogName Application -Source Microsoft-Windows-Backup -Newest 10 

and Finally lets grab the versions from wbadmin

wbadmin get versions 

powershell_5-0_icon

 

Simple yea? Now we format it to make it pretty and add it to the final script which is below.

Copy and paste or download here.       download (1)

 

<#
.SYNOPSIS
Backup system using wbadmin and email with info

.DESCRIPTION

.EXAMPLE

.NOTES
Author. Luke Keam
Published. on 20/11/2016 AUS
Version. 1
Company Name. techgeek.biz
Copyright. Free for personal and comercial use. As long as reference kept.

Questions, comments, suggestions always welcome. Please email me.

.LINK
Website. techgeek.biz
Email. [email protected]
#>

# Set a password by running this one time! in folder location with script
Read-Host “Input Password” -AsSecureString | ConvertFrom-SecureString | Out-File .\SecurePassword.txt

# Set Backup Drive Loaction
$BackupDrive = “D:\”
$CustomerName = “CustomerNameOptional ” #Leave Space at end for formating. Or comment this line out.
$Date = Get-Date -Format g

# Log Check backup size
$TotalBackupsSizeBefore = (Get-ChildItem $BackupDrive\WindowsImageBackup\ -Recurse | Measure-Object -Property Length -Sum).Sum / 1GB

# Windows Backup Start
wbadmin start backup -backuptarget:$BackupDrive -allcritical -quiet

# Log Check backup size, wbadmin get versions and event logs
$TotalBackupsSizeAfter = (Get-ChildItem $BackupDrive\WindowsImageBackup\ -Recurse | Measure-Object -Property Length -Sum).Sum / 1GB
$wbadminversions = wbadmin get versions | Out-String
$eventlogs = Get-EventLog -LogName Application -Source Microsoft-Windows-Backup -Newest 10 | Format-List -Expand CoreOnly | Out-String

# Credentials for Send-Mail Message
$UserName = “[email protected] ”
$Password = Get-Content .\SecurePassword.txt | ConvertTo-SecureString
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, $Password

# Send-MailMessage Email
Send-MailMessage -To [email protected] -From [email protected] -Credential $Credential -SmtpServer test.techgeek.biz -UseSsl -Subject “$CustomerName$env:COMPUTERNAME Backup for $Date ” -Body ”
Windows Backup for $Date
Before size $TotalBackupsSizeBefore and after size $TotalBackupsSizeAfter

Event Logs
$EventLogs

Version Info
$wbadminversions”

 

Oh baby thats sweet like sugar!

 

Hope this was helpful 

 

Luke Keam

Luke Keam

Thank you for reading.

Any questions, comments or suggestions
Email [email protected]

Have a great day from

Luke Keam 
techgeek.biz 
 

AD


                   

        

FOLLOW US

youtubemail

AD