Advertisement
Ubidibity

VeeamMountandBackup.ps1

Nov 3rd, 2024 (edited)
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 5.00 KB | Software | 0 0
  1. # VeeamMountandBackup.ps1
  2. # Version 1.0 202411030627
  3. # Copyright (C) 2024 William Parish
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # To review the GNU General Public License see <https://www.gnu.org/licenses/gpl-3.0.html>.
  16. # There is also a copy at https://pastebin.com/kDW2RH0q
  17. #
  18. # Description:
  19. # Veeam Backup Script for Automated Job Execution on USB-Attached VHDX
  20. # This script checks for a mounted USB drive, waits for Veeam Backup Service to start, mounts a VHDX file,
  21. # initiates a Veeam backup job, monitors its completion, and finally unmounts the VHDX and stops relevant services.
  22.  
  23. # The motivation is that Windows 11 Pro no longer supports direct ReFS volumes (required by Veeam for Fast Clone)
  24. # 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)
  25. # if created on a removable USB disk.
  26. # Finally, another caveat: Veeam B&R won't utilize ReFS fast clone without at least a standard license.  So there's no point doing
  27. # 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
  28. # stand-alone agent chain into a BR repository)
  29.  
  30. # I have this set to run at logon from the scheduler with the highest privileges (to facilitate the mount/services functions)
  31. # You can use various other auto-run methods or even just run it on demand if you want.
  32.  
  33. # Load the Veeam PowerShell Snap-in, suppressing errors if it's already loaded.
  34. Add-PSSnapin VeeamPSSnapin -ErrorAction SilentlyContinue
  35.  
  36. # Define the path to the VHDX file.
  37. $vhdxPath = "S:\VeeamDevDrive.vhdx"
  38.  
  39. # Function to wait until the Veeam Backup Service is running.
  40. function Wait-VeeamService {
  41.     $serviceName = "Veeam Backup Service"
  42.    
  43.     while ($true) {
  44.         $service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
  45.         if ($service -and $service.Status -eq 'Running') {
  46.             Write-Output "Veeam Backup Service is now running."
  47.             break
  48.         } else {
  49.             Write-Output "Waiting for Veeam Backup Service to start..."
  50.             Start-Sleep -Seconds 10  # Retry every 10 seconds
  51.         }
  52.     }
  53. }
  54.  
  55. # Verify USB drive accessibility.
  56. if (-not (Test-Path "S:\" -PathType Container)) {
  57.     Write-Error "The USB drive is not attached or accessible."
  58.     exit
  59. }
  60.  
  61. # Check if the VHDX file exists.
  62. if (-not (Test-Path $vhdxPath -PathType Leaf)) {
  63.     Write-Error "The VHDX file does not exist at path: $vhdxPath"
  64.     exit
  65. }
  66.  
  67. # Ensure the VHDX file is accessible.
  68. try {
  69.     $fileStream = [System.IO.File]::Open($vhdxPath, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read)
  70.     $fileStream.Close()
  71. } catch {
  72.     Write-Error "Failed to access the VHDX file: $_"
  73.     exit
  74. }
  75.  
  76. # Wait for Veeam Backup Service to be running before proceeding.
  77. Wait-VeeamService
  78.  
  79. # Mount the VHDX file.
  80. Write-Host "Mounting VHDX file..."
  81. Mount-DiskImage -ImagePath $vhdxPath
  82. Start-Sleep -Seconds 15  # Allow time for the drive to be ready
  83.  
  84. # Retrieve and start the backup job.
  85. $job = Get-VBRComputerBackupJob -Name "Agent Backup Policy 1"
  86. Write-Host "Starting backup job..."
  87. $session = Start-VBRComputerBackupJob -Job $job -RunAsync
  88.  
  89. # Wait for the actual backup session to start.
  90. Write-Host "Waiting for the actual backup session to start..."
  91. $localhostSession = $null
  92. do {
  93.     Start-Sleep -Seconds 5
  94.     $localhostSession = Get-VBRComputerBackupJobSession | Where-Object {
  95.         $_.Name -like "*localhost" -and $_.State -eq "Working"
  96.     }
  97. } while (-not $localhostSession)
  98.  
  99. # Monitor the backup session for completion.
  100. Write-Host "Monitoring the backup session for completion..."
  101. while ($localhostSession.State -eq "Working") {
  102.     Start-Sleep -Seconds 10
  103.     $localhostSession = Get-VBRComputerBackupJobSession | Where-Object {
  104.         $_.Id -eq $localhostSession.Id
  105.     }
  106. }
  107.  
  108. Write-Host "Job completed with result: $($localhostSession.Result)"
  109.  
  110. # Unmount the VHDX file.
  111. Write-Host "Unmounting VHDX file..."
  112. Dismount-DiskImage -ImagePath $vhdxPath
  113. Write-Host "VHDX file has been unmounted."
  114.  
  115. # Stop all Veeam-related services.
  116. Write-Host "Stopping Veeam services..."
  117. Get-Service -Name 'veeam*' | Where-Object { $_.Status -eq "Running" } | Stop-Service -Force
  118.  
  119. # Stop Postgres services if they are running.
  120. Write-Host "Stopping Postgres services..."
  121. Get-Service -Name 'postgres*' | Where-Object { $_.Status -eq "Running" } | Stop-Service -Force
  122.  
  123. Write-Host "Backup script completed successfully."
  124.  
  125. # This is optional, but allows you to see and acknowledge whether or not the job succeeded successfully.
  126. pause
  127.  
Tags: VEEAM
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement