Backup with PowerShell

I’ve migrated recently a Domain Controller based on Windows 2008 R2 to Windows 2012 and I took the opportunity to dig into the Backup cmdlets.

Before trying to upgrade it, I wanted to take a full backup of the system state plus the critical partition where the operating system has been installed so that I could restore the server just in case things go wrong.

The following good blog post from Richard Siddaway, http://richardspowershellblog.wordpress.com/2008/01/11/powershell-for-windows-server-backup/, had almost everything to get started.

As a prerequisite, you may need to define a backup strategy. Even if it’s not 100% suitable for corporate environments, you can also find a good example on this page: Scott Hanselman’s basic non-cloud-based personal backup strategy

Hands on! First, we need some server features to be added:

Import-Module ServerManager            
Get-WindowsFeature | ? { $_.DisplayName -match "Backup" }            
Add-WindowsFeature -Name Backup-Features -IncludeAllSubFeature:$true -Restart:$false            

The above commands will install the following 3 features:
2008 R2 Windows Backup Feature

Add-WindowsFeature -Name Windows-Server-Backup -Restart:$false

Now, we can load the module that contains the cmdlets for backup operations:

Add-Pssnapin windows.serverbackup

Everything has been simplified on Windows 2012, you just need to add a single backup feature and the module preloading feature makes it available automatically

Add-WindowsFeature -Name Windows-Server-Backup -Restart:$false

Windows 2012 Backup Feature

There are 30 cmdlets on Windows 2008 R2 vs. 49 in Windows Server 2012. Here’s the list of the 19 new cmdlets:

  • Add-WBVirtualMachine
  • Get-WBBackupVolumeBrowsePath
  • Get-WBPerformanceConfiguration
  • Get-WBVirtualMachine
  • Get-WBVssBackupOption
  • Remove-WBBackupSet
  • Remove-WBCatalog
  • Remove-WBVirtualMachine
  • Restore-WBCatalog
  • Resume-WBBackup
  • Resume-WBVolumeRecovery
  • Set-WBPerformanceConfiguration
  • Set-WBVssBackupOption
  • Start-WBApplicationRecovery
  • Start-WBFileRecovery
  • Start-WBHyperVRecovery
  • Start-WBSystemStateRecovery
  • Start-WBVolumeRecovery
  • Stop-WBJob

I digress, let’s stick to the backup of my 2008 R2 Domain Controller:

  • Get inventory data
    # Check if there's a policy in place            
    Get-WBPolicy            
    # List disks and their properties            
    Get-WBDisk

    Get-WBdisk
    NB: In my case, HP disk 0 is a RAID 1 with 2 volumes (C: & D:) + I’ve got a spare disk as volume E:. Only C: is actually critical.

  • Create a new Windows Backup policy object
    # Create an empty policy object            
    $pol = New-WBPolicy

    Empty default WB-policy object

  • Configure desired backup settings
    # Set the 'BareMetalRecovery (BMR)' checkbox to true            
    $pol | Add-WBBareMetalRecovery            
                
    # Set the System state checkbox to true            
    $pol | Add-WBSystemState            
                
    # Backup all critical volumes            
    Add-WBVolume -Policy $pol -Volume (Get-WBVolume -CriticalVolumes)            
                
    # Add a target volume where the backup files will be written            
    $targetvol = Get-WBDisk | Where { $_.Properties -match "ValidTarget" } | Get-WBVolume            
    Add-WBBackupTarget -Policy $pol -Target (New-WBBackupTarget -Volume $targetvol)            
                
    # Set a schedule             
    Set-WBSchedule -Policy $pol -Schedule ([datetime]::Now.AddMinutes(10))

    WB-policy settings

  • Launch it
    Start-WBBackup -Policy $pol
  • Review the result
    # Get a summary of previously run backup operations.            
    Get-WBSummary            
                
    # Get info from the backup target files            
    Get-WBBackupSet -BackupTarget (Get-WBBackupTarget $pol)            
                
    # Gets the status of the last backup job from the backup events in the event manager.            
    Get-WBJob -Previous 1

    Backup result
    I took 3 minutes to backup the C: drive of my DC and to create a 30GB Vhd file using a VSS snapshot.

Without PowerShell, I could have also simply used this one-liner to launch and backup the C: drive:

wbadmin.exe start backup -backupTarget:E: -include:C:\

http://technet.microsoft.com/en-us/library/cc742083%28v=ws.10%29.aspx

Before upgrading a domain controller, the schema of the forest needs to be extended otherwise the setup wizard ends with the following warning:
upgrade DC forestprep
Active Directory on this domain controller does not contain Windows Server 2012 ADPREP /FORESTPREP updates. See http://go.microsoft.com/fwlink/?LinkId=113955.
schema upgrade prereq
Here is the best fully detailed technet page about the upgrading Domain Controllers to Windows Server 2012

In my case I quickly typed the following command to confirm that the account I was using is a member of the abouve groups.

net user /dom $env:USERNAME

Finally, before completing the setup wizard, I extended the schema with these two commands:

F:\support\adprep\adprep.exe /forestprep            
F:\support\adprep\adprep.exe /domainprep /gpprep

Bonus: On the following page we can also find out what is a system image and how it works: http://blogs.technet.com/b/filecab/archive/2009/10/31/learn-more-about-system-image-backup.aspx
WB image

Advertisements

2 thoughts on “Backup with PowerShell

  1. Hi this looks amazing . I want to fully backup my win2012r2 vm , reformat and change the allocation size then restore. Could I use your approach work the restore also recreate my previous allocation size.

    • Hi,

      I would say yes to your question but I’d like also to add that if you go this way, you should have 2 backup made differently before you test the approach just in case anything goes wrong.

      As your server is already a VM, you can use the backup feature of your hypervisor. If it’s a Hyper-V 2012 R2, you can shutdown or not the VM and export it. If it’s a Hyper-V 2012, you can use the backup wizard of the Hypervisor to backup your previously shutdown VM.

      My DC was a physical server. I’ve backed it up with the PowerShell cmdlets that are the equivalent of what you’ve in the backup snap-in. My upgrade went smoothly so I hadn’t to restore the backup I made.

      That said, later on in 2013, I’ve made a backup of the DC (standalone) and restored it in a empty VM.
      I’ve made somehow a P2V (physical to virtual) using a full backup. The only thing I remember is that you should boot the original DVD/ISO of the same version of the OS that you want to restore. If your server is 2008R2, boot the VM with the 2008R2 ISO, enter the restore mode, point it to the location of your full backup,…

      What do you mean exactly by allocation size? Can you tell me more about it?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s