Reader small image

You're reading from  Windows Server 2016 Automation with PowerShell Cookbook - Second Edition

Product typeBook
Published inSep 2017
Reading LevelBeginner
Publisher
ISBN-139781787122048
Edition2nd Edition
Languages
Right arrow
Authors (2):
Thomas Lee
Thomas Lee
author image
Thomas Lee

Thomas Lee is a consultant/trainer/writer based in the UK and has been in the IT business since the late 1960s. After graduating from Carnegie Mellon University, Thomas joined ComShare where he was a systems programmer building the Commander II time-sharing operating system, a forerunner of today's cloud computing paradigm. In the mid-1970s, he moved to ICL to work on the VME/K operating system. After a sabbatical in 1980/81, he joined Accenture, leaving in 1988 to run his own consulting and training business, which is still active today. Thomas holds numerous Microsoft certifications, including MCSE (one of the first in the world) and later versions, MCT (25 years), and was awarded Microsoft's MVP award 17 times.
Read more about Thomas Lee

 Ed Goad
Ed Goad
author image
Ed Goad

Ed Goad is a systems architect who has been working in various roles in the IT field for 16 years. He first became interested in scripting and automation when presented with a task to uninstall software from over 1,000 systems with limited time and resources. He has worked with scripting and automation on multiple platforms and languages including PowerShell, VBscript, C#, and BASH scripting. Ed currently holds multiple Microsoft certifications, most recently including the Microsoft Certified IT Professional Enterprise Administrator. Additional non-Microsoft certifications include VMware Certified Professional (VCP), Red Hat Certified System Administrator (RHCSA), EMC Proven Professional, Brocade Certified Network Engineer (BCNE), and Cisco Certified Network Associate (CCNA). Ed is currently on a sabbatical and volunteering full time at the Amor Fe y Esperanza school in Honduras(http://www.afehonduras.org). There he is teaching computer and math classes to the kids who live and work in the garbage dump outside of the capital city of Tegucigalpa.
Read more about Ed Goad

View More author details
Right arrow

Chapter 5. Managing Server Backup

This chapter covers the following recipes:

  • Configure and set backup policy
  • Examine the results of a backup
  • Initiate a backup manually
  • Restore files and folders from a backup
  • Backup and restore a Hyper-V virtual machine
  • Backup and perform bare metal recovery
  • Restore the registry from a backup
  • Create a daily backup report
  • Create an Azure backup

Introduction


The ability to backup and restore a Windows Server has been a feature of Windows since the first version of Windows NT, released in 1993. In the early days, you used NTBackup (via a GUI) to carry out backup and restore activities. With Server 2008, Windows Server Backup replaced NTBackup and offered 15 cmdlets (provided in an old-school PS Snap-in) to augment the GUI. The Snap-in was replaced with a module, WindowsServerBackup, and was improved with Server 2012. The module remains unchanged in Server 2016 and provided 49 cmdlets.

Windows Server Backup (WSB) provides a set of features to enable you to backup and restore files, folders, and Hyper-V VMs as well as an entire system. These features are more than adequate for many organizations and come for free with Windows Server 2016. Nevertheless, some organizations need more functionality. There is a rich third party backup industry with a variety of competing products that deliver more than the in-box Windows Server Backup offers...

Configure and set backup policy


With WSB, you create a backup policy that describes what you want to backup from your server (backup source), where you want to put the backup (backup target), and when you want the backup to take place (the backup schedule). You first create an empty policy in memory or get an editable copy of the active backup policy. You then configure the policy object with a backup source and backup to your requirements. You then either save the policy as the (new!) active policy or use it to run a one-off backup. Once you have set an active policy, WSB runs the backup automatically based on the schedule you define when you populate the backup policy.

In this recipe, you create and configure a policy that backs up the C: drive every morning at 06:00. This policy object is the starting point for examining backup with WSB. Later recipes enable you to perform, explicitly, system state backup, one-off backup, selective file backup, and VM backup (and restores)—all of which...

Examine the results of a backup


In the previous recipe, you created a basic backup policy that runs a full backup of the C: volume every day at 6:00. This recipe examines the results of the backup and helps you understand the details of the resulting backup.

Getting ready

Before carrying out this recipe, you need to have had a backup job completed. In the recipe Configure and set backup policy, you created a backup job that would run every morning. Thus, you can wait until the day after you set up this daily backup, or you can run a once-off job as shown at the end of that recipe.

How to do it...

The steps for the recipe are as follows:

  1. Retrieve and view the current WSB backup policy:
Get-WBPolicy
  1. View an overview of what is on the target disk:
Get-ChildItem -Path Q: -Recurse -Depth 3 -Directory
  1. View the details of a backup:
$Backup = (Get-ChildItem -Path Q:\WindowsImageBackup\Psrv `
                               -Directory |
                                 Select-Object -First 1).FullName
    ...

Initiate a backup manually


In most cases, your server's backup policy is fire and forget. You create and set the backup policy, and from then on backup just happens at the time you specify in the backup schedule. In other cases, perhaps before and after a major system change, you may want to initiate a one-off backup and not wait for WSB to create a scheduled backup. Additionally, you may just want to create a backup of key files. In those cases, you can run a backup based on a one-off policy.

Getting ready

To perform this recipe, you may find it helpful to remove the existing backup policy. This ensures the one-off policy you create in this recipe does not overlap with earlier/later recipes. If you perform this recipe on a live production server, make sure reinstate the backup policy after you have completed your testing.

Removing the policy is straightforward:

If (Get-WBPolicy) { Remove-WBPolicy -All -Force } 

This recipe also uses a folder C:\Foo. If this does not exist on your system, create...

Restore files and folders


Performing backups is great, but backups are only useful if you can use them to restore files and folders. The reasons for restoring include recovering from a major system disaster, recovering from a single disk failure, or just restoring a file a user has inadvertently deleted.

In the previous recipe, you saw how you could mount the VHDX that WSB created for you and give it a drive letter. So one way to recover the file is just to mount the backup VHDX and then use Explorer or other tools to copy files from the backup to a new home. This recipe uses the WSB cmdlets to recover to a separate folder. You would do this if you wanted to recover some particular files and did not want to overwrite anything.

Getting ready

In this recipe, you first create a new backup policy and start it to create a backup. This script, which is derived from the recipe Configure and set a backup policy, is as follows:

If (Get-WBPolicy) {Remove-WBPolicy -All}
   If (-NOT (Test-Path C:food1.txt...

Backup and restore a Hyper-V Virtual Machine


There are many ways to back up a Hyper-V VM. You could, of course, use the recipes in this chapter inside the VM to back up your VM's hard drive contents to a network drive. As an alternative, you could use WSB to backup an entire VM and restore it.

Regarding the WSB commands to use when backing up a Hyper-V VM, you have options. You can use the WSB cmdlets, as you have done so far in this chapter. Or, you can use the Wbadmin console application. Wbadmin is a Windows command line tool that pre-dates the PowerShell module. It has the advantage of being higher ranked in search engines—useful if you wish to learn more. You may also find Wbadmin simpler to use in practice. This recipe shows how to use Wbadmin to create a backup of your VM and then restore it.

Getting ready

For this recipe, you need a Windows Server 2016 system with Hyper-V installed plus a working VM. In this recipe, you use the Windows Server 2016 host, HV1, that runs the DC1.Reskit...

Backup and perform bare metal recovery


The Bare metal recovery (BMR) is the process of restoring a backup onto a new machine. Suppose your file server, FS1, has a catastrophic failure and has to be replaced by a new server computer. To get this new host up and running, you could manually run through the setup of the OS, add relevant print drivers, re-define the printers, and then restore the data. Or you could just do a BMR from a backup. This recipe shows how to do bare metal recovery using the PowerShell cmdlets in the WindowsServerBackup module and then the bare metal recovery feature of Windows Setup.

Getting ready

To enable you to see BMR in action on a server, you must first install the Windows backup feature loaded on the server you wish to back up. You run the first part of this recipe on your server to create a backup capable of being restored using BMR. Then, after replacing the hardware, you perform a recovery onto to a new server based on the backup previously taken. This recipe...

Restore the registry from a backup


In Windows, applications such as the registry can register with WSB. These applications contain a Volume Shadow Copy Service (VSS) writer. WSB uses that writer in the backup process to ensure application data is consistent when WSB takes the backup. The feature enables WSB to restore the application from the backup. In this recipe, you create a backup of a system including the registry and restore the registry from a backup to view the recovered hive.

Getting ready

This recipe uses the application server SRV1. Ensure the server has the WSB feature added, as shown in the Configure and set backup policy recipe. Your VM needs to have a second VHD added and setup as the E: drive. You use this second drive as the backup target. If you are creating a new virtual hard drive to test this recipe, ensure you bring the drive online, initialize it, and format it.

How to do it...

The steps for the recipe are as follows:

  1. You begin this recipe by creating keys/values in the...

Create a daily backup report


Most backup jobs are fire and forget. You set them up, and they run. In such an environment, it is easy to forget about backups until the time when you need them (that is to recover a file, folder or entire server). One thing you can do is to generate a daily report on the state of backup on a critical server. You can run this report early every morning, and email it to one or more people in your organization to notify them of any issues that may have arisen with processing backups on critical servers.

This recipe creates a scheduled task which sends an email containing a summary of backup operations on your server, in this case: you use the file server FS1. This recipe is in two parts: the first part is a simple script that creates a backup report (on FS1) and the used email to send you the backup report. The second part of this recipe sets up the scheduled task that runs the backup report script. This second script also summarizes the results of setting up the...

Backup and restore using Microsoft Azure


The recipes in this chapter, thus far, have been focused on Windows Server Backup. An alternative to performing backups is backing up to the cloud. This recipe demonstrates using Azure Backup as an alternative to the Windows Server Backup product.

Getting ready

For this recipe to succeed, you need a computer to backup from, an Azure account, and you need to have the Azure cmdlets loaded. You also need to load the online backup cmdlets (which you do in this recipe).

In this recipe, you use SRV2 as the server to backup (and restore). You can run this recipe on any server.

To load the Azure cmdlets, you use the Install-Module cmdlet, as demonstrated in the Explore PowerShellGet recipe in Chapter 1, What's new in PowerShell and Windows Server. You use this cmdlet on SRV2 to find and load the Azure Resource Manager cmdlets that you use in this recipe. Then, you can view the newly installed module, as follows:

Install-Module -Name AzureRm -Repository PSGallery...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Windows Server 2016 Automation with PowerShell Cookbook - Second Edition
Published in: Sep 2017Publisher: ISBN-13: 9781787122048
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (2)

author image
Thomas Lee

Thomas Lee is a consultant/trainer/writer based in the UK and has been in the IT business since the late 1960s. After graduating from Carnegie Mellon University, Thomas joined ComShare where he was a systems programmer building the Commander II time-sharing operating system, a forerunner of today's cloud computing paradigm. In the mid-1970s, he moved to ICL to work on the VME/K operating system. After a sabbatical in 1980/81, he joined Accenture, leaving in 1988 to run his own consulting and training business, which is still active today. Thomas holds numerous Microsoft certifications, including MCSE (one of the first in the world) and later versions, MCT (25 years), and was awarded Microsoft's MVP award 17 times.
Read more about Thomas Lee

author image
Ed Goad

Ed Goad is a systems architect who has been working in various roles in the IT field for 16 years. He first became interested in scripting and automation when presented with a task to uninstall software from over 1,000 systems with limited time and resources. He has worked with scripting and automation on multiple platforms and languages including PowerShell, VBscript, C#, and BASH scripting. Ed currently holds multiple Microsoft certifications, most recently including the Microsoft Certified IT Professional Enterprise Administrator. Additional non-Microsoft certifications include VMware Certified Professional (VCP), Red Hat Certified System Administrator (RHCSA), EMC Proven Professional, Brocade Certified Network Engineer (BCNE), and Cisco Certified Network Associate (CCNA). Ed is currently on a sabbatical and volunteering full time at the Amor Fe y Esperanza school in Honduras(http://www.afehonduras.org). There he is teaching computer and math classes to the kids who live and work in the garbage dump outside of the capital city of Tegucigalpa.
Read more about Ed Goad