Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # VeeamMountandBackup.ps1
- # Version 1.0 202411030627
- # Copyright (C) 2024 William Parish
- #
- # 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.
- #
- # To review the GNU General Public License see <https://www.gnu.org/licenses/gpl-3.0.html>.
- # There is also a copy at https://pastebin.com/kDW2RH0q
- #
- # Description:
- # Veeam Backup Script for Automated Job Execution on USB-Attached VHDX
- # This script checks for a mounted USB drive, waits for Veeam Backup Service to start, mounts a VHDX file,
- # initiates a Veeam backup job, monitors its completion, and finally unmounts the VHDX and stops relevant services.
- # The motivation is that Windows 11 Pro no longer supports direct ReFS volumes (required by Veeam for Fast Clone)
- # You can, however, create a Dev Drive and format that as a ReFS volume, but it doesn't auto-mount (or may not even be present)
- # if created on a removable USB disk.
- # Finally, another caveat: Veeam B&R won't utilize ReFS fast clone without at least a standard license. So there's no point doing
- # this with the community edition (unless perhaps you expect to get a license in the future since you can't (as of 12.2) import a
- # stand-alone agent chain into a BR repository)
- # I have this set to run at logon from the scheduler with the highest privileges (to facilitate the mount/services functions)
- # You can use various other auto-run methods or even just run it on demand if you want.
- # Load the Veeam PowerShell Snap-in, suppressing errors if it's already loaded.
- Add-PSSnapin VeeamPSSnapin -ErrorAction SilentlyContinue
- # Define the path to the VHDX file.
- $vhdxPath = "S:\VeeamDevDrive.vhdx"
- # Function to wait until the Veeam Backup Service is running.
- function Wait-VeeamService {
- $serviceName = "Veeam Backup Service"
- while ($true) {
- $service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
- if ($service -and $service.Status -eq 'Running') {
- Write-Output "Veeam Backup Service is now running."
- break
- } else {
- Write-Output "Waiting for Veeam Backup Service to start..."
- Start-Sleep -Seconds 10 # Retry every 10 seconds
- }
- }
- }
- # Verify USB drive accessibility.
- if (-not (Test-Path "S:\" -PathType Container)) {
- Write-Error "The USB drive is not attached or accessible."
- exit
- }
- # Check if the VHDX file exists.
- if (-not (Test-Path $vhdxPath -PathType Leaf)) {
- Write-Error "The VHDX file does not exist at path: $vhdxPath"
- exit
- }
- # Ensure the VHDX file is accessible.
- try {
- $fileStream = [System.IO.File]::Open($vhdxPath, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read)
- $fileStream.Close()
- } catch {
- Write-Error "Failed to access the VHDX file: $_"
- exit
- }
- # Wait for Veeam Backup Service to be running before proceeding.
- Wait-VeeamService
- # Mount the VHDX file.
- Write-Host "Mounting VHDX file..."
- Mount-DiskImage -ImagePath $vhdxPath
- Start-Sleep -Seconds 15 # Allow time for the drive to be ready
- # Retrieve and start the backup job.
- $job = Get-VBRComputerBackupJob -Name "Agent Backup Policy 1"
- Write-Host "Starting backup job..."
- $session = Start-VBRComputerBackupJob -Job $job -RunAsync
- # Wait for the actual backup session to start.
- Write-Host "Waiting for the actual backup session to start..."
- $localhostSession = $null
- do {
- Start-Sleep -Seconds 5
- $localhostSession = Get-VBRComputerBackupJobSession | Where-Object {
- $_.Name -like "*localhost" -and $_.State -eq "Working"
- }
- } while (-not $localhostSession)
- # Monitor the backup session for completion.
- Write-Host "Monitoring the backup session for completion..."
- while ($localhostSession.State -eq "Working") {
- Start-Sleep -Seconds 10
- $localhostSession = Get-VBRComputerBackupJobSession | Where-Object {
- $_.Id -eq $localhostSession.Id
- }
- }
- Write-Host "Job completed with result: $($localhostSession.Result)"
- # Unmount the VHDX file.
- Write-Host "Unmounting VHDX file..."
- Dismount-DiskImage -ImagePath $vhdxPath
- Write-Host "VHDX file has been unmounted."
- # Stop all Veeam-related services.
- Write-Host "Stopping Veeam services..."
- Get-Service -Name 'veeam*' | Where-Object { $_.Status -eq "Running" } | Stop-Service -Force
- # Stop Postgres services if they are running.
- Write-Host "Stopping Postgres services..."
- Get-Service -Name 'postgres*' | Where-Object { $_.Status -eq "Running" } | Stop-Service -Force
- Write-Host "Backup script completed successfully."
- # This is optional, but allows you to see and acknowledge whether or not the job succeeded successfully.
- pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement