Question Script de backup HYPER-V sur cluster

Plus d'informations
il y a 13 ans 1 mois #13919 par admicom
Bonsoir,

Je suis nouveau sur ce forum.

J'ai un script HYPER-V en PowerShell, qui me backupe les VM de mon cluster vers un HDD externe.

Cependant, j'aimerais l'affiner car aujourd'hui ce scripte me met en pause les VM, une par une, avant de les exporter.

Je voudrais que cela soit plus \"propre\" et que le scripte arrête les VM (pas éteindre, mais bien arrêter correctement) avant de les sauvegarder.

En fait, j'ai changé le code d'état, mais apparemment la VM est arrêtée brutalement et non correctement éteinte.

Une idée ?


SCRIPT TROUVE SUR LE NET ET ADAPTE :

## Name this Hyper-V
## $name = \"HYPERV2-W2K8\"
$ipProp = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
$name = $ipProp.HostName

$dest = \"F:\"

## Specify where to save the log
$log = \"$dest\Journal.log\"

## Excluded Virtual Machines, comma separated: \"MACHINE1\",\"MACHINE2\"
$excludevm = \"\"

## Dont bother these
$type = \"Daily\"
$currentday = get-date -uformat \"%d\"
$currentmonth = get-date -uformat \"%m\"
$date = get-date -uformat \"%Y-%m-%d-%A\"
$VM_Service = get-wmiobject -namespace root\virtualization Msvm_VirtualSystemManagementService
$ListofVMs = get-wmiobject -namespace root\virtualization Msvm_ComputerSystem -filter \"ElementName <> Name\"

## Write start time to logfile
$dttm = get-date -uformat \"%Y-%m-%d %H:%M:%S\"
Add-Content $log \"$dttm :: Backup started\"

## Start loop
foreach ($VM in [array] $ListOfVMs)
{

$VMReturnState = $VM.EnabledState
$VMName = $VM.ElementName

## This part will exclude Virtual Machines that are specified within the $excludevm array
if ($excludevm -contains $VMname)
{
echo \"$dttm :: Excluding $VMName from Backup\"
Add-Content $log \"$dttm :: Excluding $VMName from Backup\"
}
else
{
## If the virtual machines isn't specified to be excluded, then continue

if (($VM.EnabledState -eq 2) -or ($VM.EnabledState -eq 32768) -or ($VM.EnabledState -eq 32770))
{
$VM.RequestStateChange(32769)
echo \"Saving the state of $VMName - $date\"
$savestart = get-date -uformat \"%Y-%m-%d %H:%M:%S\"
Add-Content $log \"$savestart :: $VMName : Saving the state\"
}

while (!($VM.EnabledState -eq 32769) -and !($VM.EnabledState -eq 3))
{
Start-Sleep(1)
$VM = get-wmiobject -namespace root\virtualization -Query \"Select * From Msvm_ComputerSystem Where ElementName='$VMName'\"
}

if ([IO.Directory]::Exists(\"$dest\TmpDir\$VMName\"))
{
[IO.Directory]::Delete(\"$dest\TmpDir\$VMName\", $True)
}

echo \"Exporting the VM: $VMName\"
$exportstart = get-date -uformat \"%Y-%m-%d %H:%M:%S\"
Add-Content $log \"$exportstart :: $VMName : Exporting started\"
$status = $VM_Service.ExportVirtualSystem($VM.__PATH, $True, \"$dest\TmpDir\")

if ($status.ReturnValue -eq 4096)
{
$job = [Wmi]$status.Job

while (!($job.PercentComplete -eq 100) -and ($job.ErrorCode -eq 0))
{
Start-Sleep(5)
$job = [Wmi]$status.Job
echo $job.PercentComplete
}
}

## Store the files on in a temp directory before moving them to their location and then rename folder to date of backup.

## Check to see if directories exists before we move
## and create them if they dont exist

[IO.Directory]::Delete(\"$dest\$VMName\", $True)
if (![IO.Directory]::Exists(\"$dest\$VMName\"))
{
[IO.Directory]::CreateDirectory(\"$dest\$VMName\")
}
if (![IO.Directory]::Exists(\"$dest\$VMName\$type\"))
{
[IO.Directory]::CreateDirectory(\"$dest\$VMName\$type\")
}
if (![IO.Directory]::Exists(\"$dest\$VMName\$type\$currentmonth\"))
{
[IO.Directory]::CreateDirectory(\"$dest\$VMName\$type\$currentmonth\")
}
if (![IO.Directory]::Exists(\"$dest\$VMName\$type\$currentmonth\$currentday\"))
{
[IO.Directory]::CreateDirectory(\"$dest\$VMName\$type\$currentmonth\$currentday\")
}

## Move from Temp directory to correct directory, and rename to date of backup

[IO.Directory]::Move(\"$dest\TmpDir\$VMName\", \"$dest\$VMName\$type\$currentmonth\$currentday\$date\")
$exportdone = get-date -uformat \"%Y-%m-%d %H:%M:%S\"
Add-Content $log \"$exportdone :: $VMName : Exporting completed\"

## Bring virtual machine back online

$VM.RequestStateChange($VMReturnState)
}
echo \"Done with $VMName $date\"
}
$allfinnish = get-date -uformat \"%Y-%m-%d %H:%M:%S\"
Add-Content $log \"$allfinnish :: Backup finnished\"
Add-Content $log \" \"

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 13 ans 1 mois #13928 par Matthew BETTON
Bonjour,

Peux tu utiliser les balises codes ?

exemple :
[code:1]
$ipProp = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
[/code:1]

Quelle est ta source ?

Quelles sont les parties que tu as modifié ?

@ +

Matthew

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 13 ans 1 mois #13931 par admicom
Bonsoir,

Code récupéré sur le Net, mais je ne me souviens plus de la source.

J'ai commencé à modifier cette partie :

[code:1]

## If the virtual machines isn't specified to be excluded, then continue



if (($VM.EnabledState -eq 2) -or ($VM.EnabledState -eq 32768) -or ($VM.EnabledState -eq 32770))

{

$VM.RequestStateChange(32769)

echo \"Saving the state of $VMName - $date\"

$savestart = get-date -uformat \"%Y-%m-%d %H:%M:%S\"

Add-Content $log \"$savestart :: $VMName : Saving the state\"

}



while (!($VM.EnabledState -eq 32769) -and !($VM.EnabledState -eq 3))

{

Start-Sleep(1)

$VM = get-wmiobject -namespace rootvirtualization -Query \"Select * From Msvm_ComputerSystem Where ElementName='$VMName'\"

}

[/code:1]


Code total :

[code:1]

## Name this Hyper-V

## $name = \"HYPERV2-W2K8\"

$ipProp = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()

$name = $ipProp.HostName



$dest = \"F:\"



## Specify where to save the log

$log = \"$destJournal.log\"



## Excluded Virtual Machines, comma separated: \"MACHINE1\",\"MACHINE2\"

$excludevm = \"\"



## Dont bother these

$type = \"Daily\"

$currentday = get-date -uformat \"%d\"

$currentmonth = get-date -uformat \"%m\"

$date = get-date -uformat \"%Y-%m-%d-%A\"

$VM_Service = get-wmiobject -namespace rootvirtualization Msvm_VirtualSystemManagementService

$ListofVMs = get-wmiobject -namespace rootvirtualization Msvm_ComputerSystem -filter \"ElementName <> Name\"



## Write start time to logfile

$dttm = get-date -uformat \"%Y-%m-%d %H:%M:%S\"

Add-Content $log \"$dttm :: Backup started\"



## Start loop

foreach ($VM in [array] $ListOfVMs)

{



$VMReturnState = $VM.EnabledState

$VMName = $VM.ElementName



## This part will exclude Virtual Machines that are specified within the $excludevm array

if ($excludevm -contains $VMname)

{

echo \"$dttm :: Excluding $VMName from Backup\"

Add-Content $log \"$dttm :: Excluding $VMName from Backup\"

}

else

{

## If the virtual machines isn't specified to be excluded, then continue



if (($VM.EnabledState -eq 2) -or ($VM.EnabledState -eq 32768) -or ($VM.EnabledState -eq 32770))

{

$VM.RequestStateChange(32769)

echo \"Saving the state of $VMName - $date\"

$savestart = get-date -uformat \"%Y-%m-%d %H:%M:%S\"

Add-Content $log \"$savestart :: $VMName : Saving the state\"

}



while (!($VM.EnabledState -eq 32769) -and !($VM.EnabledState -eq 3))

{

Start-Sleep(1)

$VM = get-wmiobject -namespace rootvirtualization -Query \"Select * From Msvm_ComputerSystem Where ElementName='$VMName'\"

}



if ([IO.Directory]::Exists(\"$destTmpDir$VMName\"«»))

{

[IO.Directory]:elete(\"$destTmpDir$VMName\", $True)

}



echo \"Exporting the VM: $VMName\"

$exportstart = get-date -uformat \"%Y-%m-%d %H:%M:%S\"

Add-Content $log \"$exportstart :: $VMName : Exporting started\"

$status = $VM_Service.ExportVirtualSystem($VM.__PATH, $True, \"$destTmpDir\"«»)



if ($status.ReturnValue -eq 4096)

{

$job = [Wmi]$status.Job



while (!($job.PercentComplete -eq 100) -and ($job.ErrorCode -eq 0))

{

Start-Sleep(5)

$job = [Wmi]$status.Job

echo $job.PercentComplete

}

}



## Store the files on in a temp directory before moving them to their location and then rename folder to date of backup.



## Check to see if directories exists before we move

## and create them if they dont exist



[IO.Directory]:elete(\"$dest$VMName\", $True)

if (![IO.Directory]::Exists(\"$dest$VMName\"«»))

{

[IO.Directory]::CreateDirectory(\"$dest$VMName\"«»)

}

if (![IO.Directory]::Exists(\"$dest$VMName$type\"«»))

{

[IO.Directory]::CreateDirectory(\"$dest$VMName$type\"«»)

}

if (![IO.Directory]::Exists(\"$dest$VMName$type$currentmonth\"«»))

{

[IO.Directory]::CreateDirectory(\"$dest$VMName$type$currentmonth\"«»)

}

if (![IO.Directory]::Exists(\"$dest$VMName$type$currentmonth$currentday\"«»))

{

[IO.Directory]::CreateDirectory(\"$dest$VMName$type$currentmonth$currentday\"«»)

}



## Move from Temp directory to correct directory, and rename to date of backup



[IO.Directory]::Move(\"$destTmpDir$VMName\", \"$dest$VMName$type$currentmonth$currentday$date\"«»)

$exportdone = get-date -uformat \"%Y-%m-%d %H:%M:%S\"

Add-Content $log \"$exportdone :: $VMName : Exporting completed\"



## Bring virtual machine back online



$VM.RequestStateChange($VMReturnState)

}

echo \"Done with $VMName $date\"

}

$allfinnish = get-date -uformat \"%Y-%m-%d %H:%M:%S\"

Add-Content $log \"$allfinnish :: Backup finnished\"

Add-Content $log \" \"

[/code:1]

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 13 ans 1 mois #13934 par Matthew BETTON

Je voudrais que cela soit plus \"propre\" et que le scripte arrête les VM (pas éteindre, mais bien arrêter correctement) avant de les sauvegarder.


Quelques billes sur le sujet :

Peut être une origine de ton script ? : Backup VMs in a Hyper-V Cluster (Core-version)

Ici, on utilise un module (qui évite d'avoir à utiliser explicitement WMI) : How to start/stop Hyper-V machines with PowerShell .

Le module se trouve sur Codeplex .

Voir 'Shutdown-VM' et 'Stop-VM', ainsi que les paramètres possibles de ces Cmdlets.

Sous Windows 8 et Windows 2012 Server, le module Hyper-V existe déjà.

Voir 'Stop-VM' .

Ensuite, il va falloir que tu creuses un peu tout ça, car je n'ai actuellement pas ce qu'il faut pour tester ton script et faire avancer plus le schmilblique ;) A moins qu'une autre personne participe à notre discussion ...

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 13 ans 1 mois #13935 par Matthew BETTON
Au sujet de ton code, pour mieux comprendre cette partie

[code:1]$VM.RequestStateChange(32769)[/code:1]

Voir ici : RequestStateChange method of the Msvm_ComputerSystem class

32769 : La VM est à l'état suspendu. Son état est sauvegardé.

Pour moi, il ne s'agit pas d'une pause et d'un backup du système. Cela correspond à un snapshot.

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 13 ans 1 mois #13936 par Matthew BETTON
Ici

[code:1]
$status = $VM_Service.ExportVirtualSystem($VM.__PATH, $True, \"$destTmpDir\"«»)[/code:1]

Voir les explications là .

On exporte tout ($true = virtual hard disks, saved state files, and memory content file) vers une destination (représentée par '$destTmpDir').

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.052 secondes
Propulsé par Kunena