initial version
This commit is contained in:
117
WinServerVMBackup.ps1
Normal file
117
WinServerVMBackup.ps1
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
#Script must be executed as Admin...
|
||||||
|
# it will stop virtual machines and docker-desktop, copy files to e:\backup\<date> and restart the services...
|
||||||
|
|
||||||
|
$VMs = @{}
|
||||||
|
|
||||||
|
function shutdownHyperV(){
|
||||||
|
$machines = Get-VM
|
||||||
|
foreach ($m in $machines) {
|
||||||
|
if ($m.State -eq "Running") {
|
||||||
|
Write-Host "Shutting down:"($m.Name)
|
||||||
|
$VMs[($m.Name)]=$m
|
||||||
|
Stop-VM -Name ($m.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function restartHyperV(){
|
||||||
|
foreach ($m in $VMs.Keys){
|
||||||
|
Write-Host "Starting VM:"($m)
|
||||||
|
Start-VM -Name $m
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function stopDocker(){
|
||||||
|
Write-Output "Stopping docker..."
|
||||||
|
|
||||||
|
foreach($svc in (Get-Service | Where-Object {$_.name -ilike "*docker*" -and $_.Status -ieq "Running"}))
|
||||||
|
{
|
||||||
|
$svc | Stop-Service -ErrorAction Continue -Confirm:$false -Force
|
||||||
|
$svc.WaitForStatus('Stopped','00:00:20')
|
||||||
|
}
|
||||||
|
|
||||||
|
Get-Process | Where-Object {$_.Name -ilike "*docker*"} | Stop-Process -ErrorAction Continue -Confirm:$false -Force
|
||||||
|
|
||||||
|
foreach($svc in (Get-Service | Where-Object {$_.name -ilike "*docker*" -and $_.Status -ieq "Stopped"} ))
|
||||||
|
{
|
||||||
|
$svc | Start-Service
|
||||||
|
$svc.WaitForStatus('Running','00:00:20')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function startDocker(){
|
||||||
|
Write-Output "Starting docker..."
|
||||||
|
& "C:\Program Files\Docker\Docker\Docker Desktop.exe"
|
||||||
|
|
||||||
|
$startTimeout = [DateTime]::Now.AddSeconds(90)
|
||||||
|
$timeoutHit = $true
|
||||||
|
while ((Get-Date) -le $startTimeout)
|
||||||
|
{
|
||||||
|
|
||||||
|
Start-Sleep -Seconds 10
|
||||||
|
$ErrorActionPreference = 'Continue'
|
||||||
|
try
|
||||||
|
{
|
||||||
|
$info = (docker info)
|
||||||
|
Write-Verbose "$((Get-Date).ToString("HH:mm:ss")) - `tDocker info executed. Is Error?: $($info -ilike "*error*"). Result was: $info"
|
||||||
|
|
||||||
|
if ($info -ilike "*error*")
|
||||||
|
{
|
||||||
|
Write-Verbose "$((Get-Date).ToString("HH:mm:ss")) - `tDocker info had an error. throwing..."
|
||||||
|
throw "Error running info command $info"
|
||||||
|
}
|
||||||
|
$timeoutHit = $false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
|
||||||
|
if (($_ -ilike "*error during connect*") -or ($_ -ilike "*errors pretty printing info*") -or ($_ -ilike "*Error running info command*"))
|
||||||
|
{
|
||||||
|
Write-Output "$((Get-Date).ToString("HH:mm:ss")) -`t Docker Desktop startup not yet completed, waiting and checking again"
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Write-Output "Unexpected Error: `n $_"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$ErrorActionPreference = 'Stop'
|
||||||
|
}
|
||||||
|
if ($timeoutHit -eq $true)
|
||||||
|
{
|
||||||
|
throw "Timeout hit waiting for docker to startup"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function copyFiles(){
|
||||||
|
|
||||||
|
$d = (Get-Date -format "yyyy-MM-dd").ToString()
|
||||||
|
$Destination = "E:\Backup\"+$d
|
||||||
|
$out = New-Item -ItemType Directory -Force -Path $Destination
|
||||||
|
|
||||||
|
$Source = "D:\Hyper-V"
|
||||||
|
Write-Host "Copying files from: $source to: $destination"
|
||||||
|
Copy-Item -Path $Source -Destination $Destination -Recurse
|
||||||
|
|
||||||
|
$Source = "D:\docker-desktop"
|
||||||
|
Write-Host "Copying files from: $source to: $destination"
|
||||||
|
Copy-Item -Path $Source -Destination $Destination -Recurse
|
||||||
|
}
|
||||||
|
|
||||||
|
#Main...
|
||||||
|
Write-Host "Starting Backup script..."
|
||||||
|
$s = Get-Date
|
||||||
|
|
||||||
|
shutdownHyperV
|
||||||
|
stopDocker
|
||||||
|
|
||||||
|
copyFiles
|
||||||
|
|
||||||
|
restartHyperV
|
||||||
|
startDocker
|
||||||
|
|
||||||
|
Write-Host "All done..."
|
||||||
|
$e = Get-Date
|
||||||
|
$Runtime = New-TimeSpan -Start $s -End $e
|
||||||
|
Write-Host "...in "+$Runtime
|
||||||
Reference in New Issue
Block a user