6. Windows Server Backup cmdlets
The Windows Server Backup cmdlets are the family of
cmdlets that help the administrator manage his backups, just as the
name implies. Before these
cmdlets can be used, you once again need to import the snap-in that
contains all these cmdlets. Importing these cmdlets is not as
straightforward as some of the other modules. First, you need to
install the Windows Server Backup Tools and then, you need to import
the PowerShell snap-in. To enable the Windows Server Backup cmdlets,
you first install the Windows Server Backup Tools using Server Manager
as follows:
1. | Click Start
|
2. | Click Administrative Tools
|
3. | Click Server Manager
|
4. | Server Manager will run, click on Features on the left panel
|
5. | This will bring up the features panel on the right side
|
6. | Click Add Features on the right panel
|
7. | This will open the Add Features wizard and expand Windows Server Backup Features
|
8. | Check the checkbox for Windows Server Backup
|
9. | Check the checkbox for Command-line Tools
|
10. | Click Install
|
11. | Once
the installation is complete, you have the backup tools and are almost
done. Now, you need to import the Windows Server Backup cmdlet snap-in
as follows:
|
12. | Startup PowerShell
|
13. | Type add-pssnapin windows.serverbackup and hit enter
|
The windows server backup cmdlets snap-in should now
be available. You can verify whether it has been imported properly by
typing the getpssnapin cmdlet, which will list all the snap-ins, and check to make sure that the windows.serverbackup snap-in is listed (see Figure 22).
Once this is done, you are ready to start with the
Server Backup cmdlets. You divide the cmdlets into five categories: the
cmdlets that create, the cmdlets that get, the cmdlets that set, the
cmdlets that remove, and the miscellaneous cmdlets (see Tables 17–21).
Table 17. Windows Backup cmdlets That Create Objects, the Constructors
cmdlet Name | Description |
---|
New-WBPolicy | This
cmdlet is used to create a new empty WBPolicy object. This is the
object that will govern all your backup rules once you set it. You will
need to populate it with all the specifications and properties you want
it to have |
New-WBFileSpec | This
cmdlet is used to create a new WBFileSpec object. This object specifies
a rule about what to include or exclude in a WBPolicy when the backup
is run |
New-WBBackupTarget | This cmdlet is used to create a new WBBackupTarget object. This object specifies where the backups are to be stored |
Table 18. Windows Backup cmdlets That Get Items, the Getters
cmdlet Name | Description |
---|
Get-WBBackupSet | This cmdlet gets an object that represents a list of all the backups created for the location you specify |
Get-WBBackupTarget | This cmdlet gets an object representing the backup target location for the specified policy |
Get-WBBareMetalRecovery | This
cmdlet gets a value that represents if the policy specified has the
bare metal recovery option enabled. If this is enabled, then backups
created by the policy can be used for bare metal recovery |
Get-WBDisk | This cmdlet gets a list of disks that are available for the computer |
Get-WBFileSpec | This cmdlet gets a list of the WBFileSpec objects that have been associated with the specified policy |
Get-WBJob | This cmdlet gets the currently running WBJob object which represents a backup job |
Get-WBPolicy | This cmdlet gets the policy that is currently set as the backup policy for the computer |
Get-WBSchedule | This
cmdlet gets the backup schedule for the specified policy. Giving it no
parameter will default to the current active backup policy |
Get-WBSummary | This cmdlet gets the history of backups performed |
Get-WBSystemState | This
cmdlet gets a value that represents if the policy specified has the
system state recovery option enabled. If this is enabled, then backups
created by the policy can be used for system state recovery |
Get-WBVolume | This cmdlet gets a list of volumes determined by the parameters you give it |
Get-WBVssBackupOptions | This cmdlet gets a value of either VssFullbackup or VssCopyBackup depending on the setting of the policy specified |
Table 19. Windows Backup cmdlets That Set or Modify Items, the Setters
cmdlet Name | Description |
---|
Set-WBPolicy | This cmdlet sets the specified policy as the active policy to be used for backups |
Set-WBSchedule | This cmdlet sets the time or times to create daily backups for the specified policy |
Set-WBVssBackupOptions | This cmdlet sets the value specifying if the backups created by this policy are going to be VssFullbackups or VssCopyBackups |
Table 20. Windows Backup cmdlets That Remove or Delete Items
cmdlet Name | Description |
---|
Remove-WBBackupTarget | This cmdlet removes a specified WBBackupTarget from a specified policy |
Remove- | This cmdlet removes the bare metal recovery capability from the specified policy. |
WBBareMetalRecovery | Backups created by the policy after this cmdlet is run will not be able to be used to make a bare metal recovery |
Remove-WBFileSpec | This cmdlet removes a specified WBFileSpec from a specified policy |
Remove-WBPolicy | This
cmdlet removes the currently set backup policy if no policy is
specified. If a policy is specified it removes it as long as it is the
currently set backup policy |
Remove-WBSystemState | This
cmdlet removes the system state recovery option from the specified
policy. Backups created by the policy will no longer be able to be used
to make system state recoveries |
Remove-WBVolume | This cmdlet removes a specified volume from a specified policy |
Table 21. Miscellaneous Windows Backup cmdlets
cmdlet Name | Description |
---|
Start-WBBackup | This cmdlet starts a backup operation using the specified policy |
The Windows Server Backup PowerShell cmdlets give
you access to all the tools you need to create your own backups,
specify when they need to run, and specify what to backup and where to
put it. Let us say you walk into work one day, and after passing
through all the metal detectors and security checkpoints, you arrive at
your desk. On it, you see a curious manila folder with the words TOP
SECRET in red. You open it and pull out some yellow-lined paper with
strange, illegible writing. You quickly take out your decoder ring and
you are able to read the first few lines, “I need you to set up a
Backup Policy” it says, followed by more and more specifics. You decide
that the first thing you will need to do is create a WBPolicy object. You type $secretPolicy = New-WBPolicy
at the prompt and hit enter. When the cmdlet completes, you will have
created a new empty WBPolicy object in the $secretPolicy variable. It
is this object that you will use to set up your backups. You want to
make sure that your policy will create backups that can be used for
bare metal recoveries if needed. You type Add-WBBareMetalRecovery –Policy $secretPolicy
and hit enter. This adds the bare metal recovery setting to the policy
you created. Using the Add-WBBareMetalRecovery cmdlet on a policy not
only adds
the bare metal recovery setting to the policy but also adds the system
state recovery setting without having to run the Add-WBSystemState
cmdlet. So, now the policy can create backups which can be used for
bare metal or system state recoveries as well as full server recovery.
Decoding a few more lines, you read that the backups must be full VSS
backups. You put the decoder ring down and type Set-WBVssBackupOptions –Policy $secretPolicy –VssFullBackup
and hit enter. Now that you have taken care of that, you will also need
to decide where you are going to back up to. You will need to set up a
backup target object. Because you might not be aware of all the disks
you could back up to in your server, you will check to see what is
available by typing Get-WBDisk and hitting enter. When the cmdlet executes, you see a list of all the disks on the system (see Figure 23).
You
then specify a backup location using the New-WBBackupTarget cmdlet by
specifying the disk you want to backup to. In this case, you decide
that to back up everything to a volume, set aside just for backups, you
will be backing up to volume D:. You type $secretDriveBackupLocation = NewWBBackupTarget –VolumePath D:
and hit enter. This has now created a WBBackupTarget object for you in
the $secretDriveBackupLocation variable. You still need to do a few
more things before you are ready because your policy is still empty.
You have a backup policy and a backup target; you need to decide what
exactly needs to be backed up. You check your top-secret mission
objectives with your decoder ring and find that you need to back up all
the files in a directory called C:\ForYourEyesOnly. Easy enough, you
realize you need to create a WBFileSpec object to specify what to
backup and what to exclude if anything. You will name the WBFileSpec
object $secretFileSpec to be consistent and type $secretFileSpec = New-WBFileSpec –FileSpec C:\ForYourEyesOnly
and hit enter. The C:\ForYourEyesOnly directory contents and all its
subfolders have been marked for backup with this WBFileSpec. If you
want to exclude some of the files in the directory, you would use the
–Exclude flag. Reading in, you
notice that you need to exclude any files that have the word medals at
the beginning. You create another WBFileSpec object by typing $noMedalsFileSpec = New-WBFileSpec –FileSpec C:\ForYourEyesOnly \*medals*.* -Exclude
and hit enter. You pick up your decoder ring and read through the
instructions but they seem to stop suddenly. “Typical,” you mumble to
yourself. You once again open your desk drawer and take out the special
pen that was handed to you for just an occasion. You quickly scribble
with the pen over the next section of the instructions and wait for
them to appear. You are to set the backup process to run at a secret
time of your choosing. It can be anytime as long as you do not tell
anyone. You decide to schedule it at 11 pm during the off-peak hours.
You will set the WB schedule straight into the secret policy with the
SetWBSchedule cmdlet by typing Set-WBSchedule –Policy $secretPolicy –Schedule 23:00
and hitting enter. The time for the –Schedule flag must be written in
HH:MM format. If you need to include more than one time to make
backups, at then you can separate them with a comma. Once the cmdlet
executes, you will see a confirmation that will show the next time the
backup will run according to schedule, you set on that policy. You now
have all the basic pieces and just need to put them together. First,
you add all the WBFileSpec objects you created to the policy, otherwise
it will not know what to backup and what to exclude. You type Add-WBFileSpec –Policy $secretPolicy $secretFileSpec, $noMedalsFileSpec
and hit enter. This will add both of your specifications to the policy.
If you had any more specifications, you would just create them and add
them the same way. You can check the specifications on the policy by
typing Get-WBFileSpec –Policy $secretPolicy and hit enter. This will list the specifications you have added so far (see Figure 24).
You continue by adding the BackupTarget object you created earlier to the policy. You type Add-WBBackupTarget –Policy $secretPolicy –Target $secretDriveBackupLocation
and hit enter, suppressing the urge to look around the room in a
paranoid way. When the cmdlet executes, the display will show you a
list of properties as confirmation. You are pretty much done. You can
put away the decoder ring and special pen and get ready to burn the
directions in the garbage can. All that is left to do is set the policy
you created as the backup policy. This time you disable the smoke
detector before you set the garbage can contents on fire and sit down
to finish. You type Set-WBPolicy –Policy $secretPolicy
and hit enter. You wait for the cmdlet to execute and then you pick up
your phone, dial your boss’s extension, and as soon as he picks up, you
whisper “The eagle has landed” and hang up. You sit back down amid the
strange stares from your coworkers and wish you had a normal boss. The
steps for creating a complete policy using PowerShell are very
straightforward. Here are the steps needed.
1. | Create the policy object with the New-WBPolicy cmdlet.
|
2. | Create
all the specifications about what to backup and what to exclude with a
WBFileSpec object for each one using New-WBFileSpec cmdlet.
|
3. | Add the WBFileSpec objects you created to the policy using the Add-WBFileSpec cmdlet.
|
4. | Create a WBBackupTarget object to indicate where to backup to using the New-WBBackupTarget cmdlet.
|
5. | Add the WBBackupTarget object you created to your policy using the Add-WBBackupTarget cmdlet.
|
6. | The
final step is to take the policy you created, which now has all the
information you specified, and set it as the active backup policy using
the Set-WBPolicy cmdlet.
|
Using the PowerShell Windows Backup cmdlets
is easy and straightforward. You can accomplish everything with a few
intuitive easy-to-remember cmdlets. Keep in mind the pattern that there
are only three kinds of objects to create in the reference of Windows
Backups. There are Policies, BackupTargets, and FileSpecs. All the
other cmdlets are there to manipulate these objects or to actually run
the backups themselves. You can force a backup to run at anytime using
the Start-WBBackup cmdlet and you can make it run with a policy that
you just created without setting it as the backup policy. This is
useful for one-time backup runs with a specific policy instead of the
standard backup policy that is set in place.