Bacula - Backing up Hyper-V Virtual Machines (VM)

Managed ad

By justin, 7 March, 2015

I started this series off with:

and will be continuing it with

What good is a backup solution if you can't backup your Virtual Machines? You can always use Windows Server Backup but then you are using 2 systems and have to have things scheduled just right. Here we will look at getting backups of your VMs within Bacula.

This article builds on what we did in Bacula Windows Age​nt ands just adds a couple of scripts to take Checkpoints (Snapshots) of your VMs then backs them up on incrimental and Full backups.

Scripts

You will Add a couple scripts to your "C:\Program Files\Bacula" Directory. Those scripts can be found here

There is 1 .bat file:

Hyper-V-Full.bat Which Kicks off the Hyper-V.Full.ps1 Powershell script and contains only 1 line:​​

PowerShell -ExecutionPolicy RemoteSigned -File "C:\Program Files\Bacula\Hyper-V.ps1" %1

Hyper-V-Incremental.bat which kicks off the Hyper-V.Incremental.ps1 Powershell script and likewise, this one only contains 1 line:

 

The system will use a variable to determine which one should be kicked off.

 

Powershell Script:

The power shell scripts that the .bat scripts kick off create snapshots on the host. this makes it easier for Bacula to backup the Hyper-V

The Hyper-V.Full.ps1 is used on Full backups. It deletes all of the Bacula-* Checkpoints (Snapshots) and creates a new one.

 

#Copyright disclaimer:
#    Copyright (C) 2015,  ITHierarchy Inc (www.ithierarchy.com). ALl rights reserverd.
#    This program is free software: you can redistribute it and/or modify
#        it under the terms of the GNU General Public License as published by
#        the Free Software Foundation, either version 3 of the License, or
#        (at your option) any later version.
#
#        This program is distributed in the hope that it will be useful,
#        but WITHOUT ANY WARRANTY; without even the implied warranty of
#        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#        GNU General Public License for more details.
#
#        You should have received a copy of the GNU General Public License
#        along with this program.  If not, see <http://www.gnu.org/licenses/>.

Param(
  [string]$level

)
$DateStamp=$(((get-date)).ToString("yyyyMMddTHHmmss"))
if ($level -eq "Full"){$Backup="Bacula-*"}Else{$Backup="Bacula-$level*"}

$HyperVPath="C:\Hyper-V"  #Set path to your Hyper-V Machines to be backed up

#Sort out Actual Volume path to VM
$VMDrive=$HyperVPath.Substring(0,1)
$volume=Get-Volume $VMDrive
$TrueHyperVPath=$($HyperVPath.Replace("$($VMDrive):\",$($Volume.path)))

    #Get List of VMs
    $VMs=Get-VM 
        foreach ($VM in $VMs){
        #Check to make sure it is in the Hyper-V Path
        if ($($VM.path).ToUpper() -like ("$TrueHyperVPath\*").ToUpper()) { 
            $vm.Path

            #Snap those VMs
            
            write-host "Starting VM $($vm.Name) @ '$($vm.Path)'"
            $CurrentSnapShots = $VM | Get-VMSnapshot 

            foreach ($SnapShot in $CurrentSnapShots){
                if ($SnapShot.Name -like ("$Backup")){   
                    
                    write-host "Removing VM Checkpoint '$($SnapShot.Name)'"
                    $SnapShot | Remove-VMSnapshot  
                

                    $LoopCount=0
                    do {
                        Write-host "Waiting for snapshot '$($SnapShot.name)' to delete..."
                        
                        Start-Sleep -s 10 
                        $LoopCount=$LoopCount+1
                    }while ($VM.Status -eq "Merging disks" -and $LoopCount -lt 30)
                }
            }
            
            write-host "Creating Checkpoint  Bacula-$level.$DateStamp"
            $VM | Checkpoint-VM -SnapshotName Bacula-$level.$DateStamp
        }
    

    

Creating a File Set for Windows

The biggest thing to remember is that instead of using "\" in your paths, use "/". an example is "C:\Program Files\bacula" would be "C:/Program Files/bacula". Again we will use webmin to create the File Set.

  1. Click on "File Sets"

     
  2. On the File Sets page, click "Add a new backup file set.".

     
  3.  Now you will need to fill out the form. Since the backup is not on the local machine, you have to manually type the paths to use
    1. File Name Set: Give it a name that you and others that need access to this will recognize and understand
    2. Enter the file path that that your Hyper-V machines are stored at. REMEMBER use "/" instead of "\"
    3. You will always want to exclude *.bin since these will be locked and record an error in the log if your VM is started and these files are not needed.
    4. Click "Create" Button

Create Backup Job for Hyper-V

I will assume you already have schedules and know how to create those so I will cut right to the meat and show you what you need to do to the backup job. We will do this in Webmin using the Bacula Backup Syatem:

  1. Select Backup Jobs

     
  2. Select "Add a new backup job.".

     
  3. You will need to fill in the pertinent information
    1. Backup Job Name: Give it a descriptive name
    2. Make sure the job is enabled or it will not run
    3. Default type: I usually leave this at stand-alone job
    4. Job type: Backup
    5. Backup Level: Incremental (NOTE: it will defer to the settings in the schedule to determine if it needs to do a full backup. make sure your schedule has a regular Full Backup in it.)
    6. Client to backup: Select your Hyper-V host
    7. File set to backup: Select the fileset you created in the previous section. this does not have to be only Hyper-V, you can backup other files as well.
    8. Backup on Schedule: Select the Schedule that it will use. 
    9. Destination storage device: select what to backup to. I am using a LTO-2 drive in an autochanger, but you can use any tape device or backup to file.
    10. Volume pool: select the volume pool you want it to use. I keep all backups in the Default pool, but if you want, you can break it out.
    11. Destination for messages: I keep this at Standard, but you can have the messages go where you want.
    12. Backup priority: the Default is 10, but you can set this to any positive number you want above 0. The lower the number the more important the job, so a 1 has a higher priority than a 10.
    13. Command before job (on client): this is the only command that needs to be selected for Hyper-V backups. We will us "%l" to determine if the Hyper-V-Full.bat or the Hyper-V-Incremental.bat should run. The %l is a variable that is replaced by the backup level. We also need to use the Short names generated for non-8dor3. You can get these by running dir /X from the command line on the windows clients. (REMEMBER: in your path use "/" instead of "\")  the command line we put in here is:
      C:/Progra~1/Bacula/Hyper-V.bat %l

       
    14. Click Create

       

Ok Lets test it.

  1. Click on the link in your Job:

     
  2. At the bottom of the page click "Run Now"

     
  3. Watch the magic:

Starting backup job Hyper-V Backup for Host01 ..

Automatically selected Catalog: MyCatalog
Using Catalog "MyCatalog"
A job name must be specified.
The defined Job resources are:
     1: BackupCatalog
     2: Data
     3: Hyper-V
     4: LocalConfig
     5: Hyper-V Full Backup
     6: Hyper-V Backup for Host01 
Select Job resource (1-6): 6
Run Backup job
JobName:  Hyper-V Backup for Host01 
Level:    Incremental
Client:   host01-fd
FileSet:  Hyper-V Host
Pool:     Default (From Job resource)
Storage:  LTO-2-0 (From Job resource)
When:     2015-03-07 15:29:13
Priority: 10
OK to run? (yes/mod/no):
 

.. the backup job is now running. When complete, the results will be shown below ..

 yes
Job queued. JobId=120
You have messages.
messages
07-Mar 15:29 bacula-dir JobId 120: No prior Full backup Job record found.
07-Mar 15:29 bacula-dir JobId 120: No prior or suitable Full backup found in catalog. Doing FULL backup.
07-Mar 15:29 bacula-dir JobId 120: Start Backup JobId 120, Job=Hyper-V_Backup_for_Host01_.2015-03-07_15.29.13_36
07-Mar 15:29 bacula-dir JobId 120: Using Device "LTO-2-0"
07-Mar 15:29 bacula-fd JobId 120: shell command: run ClientRunBeforeJob "C:/Progra~1/Bacula/Hyper-V-Full.bat"   #Running Command we defined
07-Mar 15:29 host01-fd JobId 120: ClientRunBeforeJob: 
07-Mar 15:29 host01-fd JobId 120: ClientRunBeforeJob: C:\Windows\system32>PowerShell -ExecutionPolicy RemoteSigned -File "C:\Program Files\Bacula\Hyper-V.Full.ps1" #Executing our Powershell Script
07-Mar 15:29 host01-fd JobId 120: ClientRunBeforeJob: \\?\Volume{84a8f50f-788b-11e3-80bb-0030489bff17}\Hyper-V\vAD01
07-Mar 15:29 host01-fd JobId 120: ClientRunBeforeJob: Starting VM vADInator01 @ '\\?\Volume{84a8f50f-788b-11e3-80bb-0030489bff17}\Hyper-V\vAD01' #The first VM found
07-Mar 15:29 host01-fd JobId 120: ClientRunBeforeJob: Creating Checkpoint  Bacula-Full.20150307T152912 #Createing Checkpoint (snapshot)
07-Mar 15:29 host01-fd JobId 120: Generate VSS snapshots. Driver="Win64 VSS", Drive(s)="C"
07-Mar 15:54 host01-fd JobId 120: VSS Writer (BackupComplete): "Task Scheduler Writer", State: 0x1 (VSS_WS_STABLE)
07-Mar 15:54 host01-fd JobId 120: VSS Writer (BackupComplete): "VSS Metadata Store Writer", State: 0x1 (VSS_WS_STABLE)
07-Mar 15:54 host01-fd JobId 120: VSS Writer (BackupComplete): "Performance Counters Writer", State: 0x1 (VSS_WS_STABLE)
07-Mar 15:54 host01-fd JobId 120: VSS Writer (BackupComplete): "System Writer", State: 0x1 (VSS_WS_STABLE)
07-Mar 15:54 host01-fd JobId 120: VSS Writer (BackupComplete): "Cluster Shared Volume VSS Writer", State: 0x1 (VSS_WS_STABLE)
07-Mar 15:54 host01-fd JobId 120: VSS Writer (BackupComplete): "Microsoft Hyper-V VSS Writer", State: 0x1 (VSS_WS_STABLE)
07-Mar 15:54 host01-fd JobId 120: VSS Writer (BackupComplete): "ASR Writer", State: 0x1 (VSS_WS_STABLE)
07-Mar 15:54 host01-fd JobId 120: VSS Writer (BackupComplete): "Dedup Writer", State: 0x1 (VSS_WS_STABLE)
07-Mar 15:54 host01-fd JobId 120: VSS Writer (BackupComplete): "Shadow Copy Optimization Writer", State: 0x1 (VSS_WS_STABLE)
07-Mar 15:54 host01-fd JobId 120: VSS Writer (BackupComplete): "Registry Writer", State: 0x1 (VSS_WS_STABLE)
07-Mar 15:54 host01-fd JobId 120: VSS Writer (BackupComplete): "IIS Config Writer", State: 0x1 (VSS_WS_STABLE)
07-Mar 15:54 host01-fd JobId 120: VSS Writer (BackupComplete): "WMI Writer", State: 0x1 (VSS_WS_STABLE)
07-Mar 15:54 hostinator01-fd JobId 120: VSS Writer (BackupComplete): "COM+ REGDB Writer", State: 0x1 (VSS_WS_STABLE)
07-Mar 15:54 host01-fd JobId 120: VSS Writer (BackupComplete): "Cluster Database", State: 0x1 (VSS_WS_STABLE)
07-Mar 15:54 host01-fd JobId 120: VSS Writer (BackupComplete): "IIS Metabase Writer", State: 0x1 (VSS_WS_STABLE)
07-Mar 15:54 bacula-sd JobId 120: Job write elapsed time = 00:24:35, Transfer rate = 44.28 M Bytes/second
07-Mar 15:54 bacula-dir JobId 120: Bacula saninator-dir 5.2.6 (21Feb12):
#Results of Backup
  Build OS:               x86_64-pc-linux-gnu ubuntu 14.10
  JobId:                  120
  Job:                    Hyper-V_Backup_for_Host01_.2015-03-07_15.29.13_36
  Backup Level:           Full (upgraded from Incremental)
  Client:                 "host01-fd" 5.2.10 (28Jun12) Microsoft Datacenter Edition (build 9200), 64-bit,Cross-compile,Win64
  FileSet:                "Hyper-V Host" 2015-03-07 15:15:24
  Pool:                   "Default" (From Job resource)
  Catalog:                "MyCatalog" (From Client resource)
  Storage:                "LTO-2-0" (From Job resource)
  Scheduled time:         07-Mar-2015 15:29:13
  Start time:             07-Mar-2015 15:29:35
  End time:               07-Mar-2015 15:54:13
  Elapsed time:           24 mins 38 secs
  Priority:               10
  FD Files Written:       16 
  SD Files Written:       16 #Number of file written  
FD Bytes Written:       65,324,148,515 (65.32 GB) 
  SD Bytes Written:       65,324,151,332 (65.32 GB) #Amount written to tape or file storage
  Rate:                   44197.7 KB/s
  Software Compression:   None
  VSS:                    yes
  Encryption:             no
  Accurate:               no
  Volume name(s):         000168
  Volume Session Id:      13
  Volume Session Time:    1425654095
  Last Volume Bytes:      65,440,392,192 (65.44 GB) 
  Non-fatal FD errors:    0
  SD Errors:              0
  FD termination status:  OK
  SD termination status:  OK
  Termination:            Backup OK  #Final Results
​

 

Others in the Series:

and will be continuing it with

 

Comments