Digressive / HyperV-Backup-Utility

Flexible Hyper-V Backup Utility
https://gal.vin/posts/vm-backup-for-hyper-v/
MIT License
120 stars 38 forks source link
7zip backup backup-utility configurable hyper-v hyperv-backup-utility nas powershell virtual-machine zip

Hyper-V Backup Utility

Flexible Hyper-V Backup Utility

For full change log and more information, visit my site.

Hyper-V Backup Utility is available from:

Please consider supporting my work:

Please report any problems via the ‘issues’ tab on GitHub.

-Mike

Features and Requirements

7-Zip support

I've implemented support for 7-Zip into the script. You should be able to use any option that 7-zip supports, although currently the only options I've tested fully are '-t' archive type, '-p' password and '-v' split files.

When to use the -NoPerms switch

The -NoPerms switch is intended as a workaround when used in an environment where the Hyper-V host cannot be given the required permissions to run a regular export to a remote device such as a NAS device.

Hyper-V’s export operation requires that the computer account in Active Directory have access to the location where the exports are being stored. I recommend creating an Active Directory group for the Hyper-V hosts and then giving the group the required ‘Full Control’ file and share permissions.

When a NAS, such as a QNAP device is intended to be used as an export location, Hyper-V will not be able to complete the operation as the computer account will not have access to the share on the NAS. To copy all the files necessary for a complete backup, the VM must be in an offline state for the operation to be completed. Due to this the script will put the VM in a 'Saved' state so the files can be copied. Previously the VM would be shutdown but this is a faster and safer method as the VM does not require any integrations to be put in a saved state.

Generating A Password File For SMTP Authentication

The password used for SMTP server authentication must be in an encrypted text file. To generate the password file, run the following command in PowerShell on the computer and logged in with the user that will be running the utility. When you run the command, you will be prompted for a username and password. Enter the username and password you want to use to authenticate to your SMTP server.

Please note: This is only required if you need to authenticate to the SMTP server when send the log via e-mail.

$creds = Get-Credential
$creds.Password | ConvertFrom-SecureString | Set-Content c:\scripts\ps-script-pwd.txt

After running the commands, you will have a text file containing the encrypted password. When configuring the -Pwd switch enter the path and file name of this file.

Restoring a Virtual Machine

The easiest and quickest way to restore a Virtual Machine that has been backed up using this script is to use Hyper-V's native import function.

  1. Copy the backup of the VM you want to restore to a location on the VM host server that the VM should run from. If the backup is compressed, uncompress the file.
  2. In the Hyper-V Manager, right-click on the VM host and select 'Import Virtual Machine'.
  3. Browse to the location of the VM backup folder and click Next.
  4. Select the VM you want to restore.
  5. Select 'Register the virtual machine in-place' option.
  6. The VM will be registered in Hyper-V and available for use.

Configuration

Here’s a list of all the command line switches and example configurations.

Command Line Switch Description Example
-BackupTo The path the virtual machines should be backed up to. Each VM will have its own folder inside this location. [path]
-List Enter the path to a txt file with a list of Hyper-V VM names to backup. If this option is not configured, all running VMs will be backed up. [path]vms.txt
-CaptureState Enter a method to use when exporting the VM. If this option is not configured, the default method will be used. CaptureCrashConsistentState, CaptureSavedState, CaptureDataConsistentState
-Wd The path to the working directory to use for the backup before copying it to the final backup directory. Use a directory on local fast media to improve performance. [path]
-NoPerms Configures the utility to shut down running VMs to do the file-copy based backup instead of using the Hyper-V export function. N/A
-Keep Instructs the utility to keep a specified number of days worth of backups. VM backups older than the number of days specified will be deleted. [number]
-Compress This option will create a zip file of each Hyper-V VM backup. N/A
-Sz Configure the utility to use 7-Zip to compress the VM backups. 7-Zip must be installed in the default location $env:ProgramFiles if it is not found, Windows compression will be used. N/A
-SzOptions Use this switch to configure options for 7-Zip. The switches must be comma separated. "'-t7z,-v2G,-ppassword'"
-ShortDate Configure the script to use only the Year, Month and Day in backup filenames. N/A
-LowDisk Remove old backups before new ones are created. For low disk space situations. N/A
-L The path to output the log file to. [path]
-LogRotate Remove logs produced by the utility older than X days [number]
-NoBanner Use this option to hide the ASCII art title in the console. N/A
-Help Display usage information. No arguments also displays help. N/A
-ProgCheck Send notifications (email or webhook) after each VM is backed up. N/A
-OptimiseVHD Optimise the VHDs and make them smaller before copy. Must be used with -NoPerms option. N/A
-Webhook The txt file containing the URI for a webhook to send the log file to. [path]webhook.txt
-Subject Specify a subject line. If you leave this blank the default subject will be used "'[Server: Notification]'"
-SendTo The e-mail address the log should be sent to. For multiple address, separate with a comma. [example@contoso.com]
-From The e-mail address the log should be sent from. [example@contoso.com]
-Smtp The DNS name or IP address of the SMTP server. [smtp server address]
-Port The Port that should be used for the SMTP server. If none is specified then the default of 25 will be used. [port number]
-User The user account to authenticate to the SMTP server. [example@contoso.com]
-Pwd The txt file containing the encrypted password for SMTP authentication. [path]ps-script-pwd.txt
-UseSsl Configures the utility to connect to the SMTP server using SSL. N/A

How to use

[path\]Hyper-V-Backup.ps1 -BackupTo [path\]

This will backup all the VMs running to the backup location specified.

Change Log

2024-05-11: Version 24.05.11

2024-03-21: Version 24.03.21

2024-03-18: Version 24.03.18

2024-03-08: Version 24.03.08

2023-09-05: Version 23.09.05

2023-04-28: Version 23.04.28

2023-02-18: Version 23.02.18

2023-02-07: Version 23.02.07

2023-01-09: Version 23.01.09

2022-06-22: Version 22.06.22

2022-06-18: Version 22.06.18

2022-06-17: Version 22.06.17

2022-06-14: Version 22.06.11

2022-03-27: Version 22.03.26

2022-02-08: Version 22.02.08

2022-01-20: Version 22.01.19

2021-12-28: Version 21.12.28

2021-11-12: Version 21.11.09

2021-11-05: Version 21.11.05

2021-08-10: Version 21.08.10

2021-07-02: Version 21.07.02

2021-06-14: Version 21.06.14

2021-06-02: Version 21.06.02

2021-05-30: Version 21.05.30

2020-07-13: Version 20.07.13

2020-02-28: Version 20.02.28 ‘Artifact’

2020-02-18: Version 2020.02.14 ‘Valentine’

Current known issues:

New features:

2019-09-04 v4.5

2019-05-26 v4.4

2018-06-21 v4.3

2018-03-04 v4.2

2018-03-03 v4.1

2018-01-15 v4.0

2018-01-12 v3.9

2018-01-12 v3.8

2017-10-16 v3.7

2017-10-07 v3.6

2017-09-18 v3.5

2017-07-22 v3.4

2017-05-20 v3.3

2017-04-24 Minor Update

2017-04-21 Minor Update