Záloha VM za nula peněz = záloha klonováním

Jen pro upřesnění, není to žádné sofistikované řešení, ale prachobyčejná tvorba klonů celých VM, takže je to víceméně pro SMB, který má menší infrastrukturu a také to chce mít poměrně rychlý cílový datastore s dost místem pro klony. A když už jsem se s tím dělal, včetně popisků, tak to sem přilepím. (+upg 2020)

1. První řešení je načasovat jeden script na všechny spuštěné VM:

CloneScript1-forAllServer.ps1:


# snap-in must be available on the PowerShell (BTW for tests PowerCli run as Administrator)
Add-PSSnapin "VMware.VimAutomation.Core"
# Connect to vCenter
$vCenter = '192.168.XYZ.XXX'
Connect-VIServer -Server $vCenter -Protocol https -Username 'user@vsphere.local' -Password 'password' –savecredential
# Select all VM Backup Clone1 and rename to Clone2
$vm1 = get-vm -Name *bckclone1
foreach($vm in $vm1){Set-VM -VM $vm -Name "$vm bckclone2" -confirm:$false}
# Select all VM for Backup and clone
$ESXi = '192.168.XYZ.YYY'
$Datastore = 'DatastoreName'
$vm2 = Get-VM -Name t* | Where-Object {$_.Notes -ne "CloneBackup"} | Where-Object {$_.PowerState -eq "PoweredOn"}
foreach($vm in $vm2){New-VM -VM $vm -Name "$vm bckclone1" -VMHost $ESXi -datastore $Datastore -Notes "CloneBackup"}
# Delete All second Backup
remove-vm -VM "*bckclone2" -deletepermanently -Server $vCenter -confirm:$false
# send Report (SMTP-Server and recipient from vCenter settings)
$vCenterSettings = Get-View -Id 'OptionManager-VpxSettings'
$MailSender = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "mail.sender"}).Value
$MailSmtpServer = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "mail.smtp.server"}).Value
$Datum = Get-date -format d
$ReportPath = "c:\script\log\report $Datum .txt"
Get-VM *clone* | Select Name,@{Name="ChangeVersion";Expression={$_.ExtensionData.Config.ChangeVersion}} | sort ChangeVersion -desc >$ReportPath
Send-MailMessage -from $MailSender -to "email@domain.xx" -subject "Clone Report $Datum" -body "Clone Report $Datum in attachment" -attachment "$ReportPath" -smtpServer $MailSmtpServer
# Disconnect from vCenter and clear Variables
Disconnect-VIServer -Server $vCenter -Confirm:$false
clear-variable vm1,vm2,vCenter,ESXi,Datastore,vCenterSettings,MailSender,MailSmtpServer,Datum,Reportpath,Reportcontent
exit

2. Druhé řešní je script pro každý server zvlášť, což umožní si pohrát si s časovaním (pozor, každý script, musít svou proměnnou názvu VM a času – vyznačeno červeně – pro druhý script místo 1 dát 2, atd)

CloneScript1-forOneServer.ps1:


# snap-in must be available on the PowerShell
Add-PSSnapin "VMware.VimAutomation.Core"
# Connect to vCenter
$vCenter = '192.168.XYZ.XXX'
Connect-VIServer -Server $vCenter -Protocol https -Username 'user@vsphere.local' -Password 'password' –savecredential
##########################
#  Backup(Clone) object  #  
##########################
$BCVM1 = get-vm -name "ServerXX"
# Backup(Clone)
$ESXi = '192.168.XYZ.YYY'
$Datastore = 'DatastoreName'
$Time1 = Get-date
New-VM -VM $BCVM1 -Name "$BCVM1-BCKClone" -VMHost $ESXi -datastore $Datastore -Notes "Clone $Time1" -Location "TemplateClone"
# Delete old Template
Remove-Template "Template $BCVM1-BCKClone" -DeletePermanently -Confirm:$false
# Report file
$Datum = Get-date -format d
$ReportPath = "c:\script\log\report $BCVM1 $Datum .txt"
Get-VM $BCVM1* | Select Name,@{Name="ChangeVersion";Expression={$_.ExtensionData.Config.ChangeVersion}},Notes | sort ChangeVersion -desc >>$ReportPath
# Covnert a rename Template
Set-VM -ToTemplate  "$BCVM1-BCKClone" -Confirm:$false
Set-Template "$BCVM1-BCKClone" -Name "Template $BCVM1-BCKClone"
# send Report (SMTP-Server and recipient from vCenter settings)
$vCenterSettings = Get-View -Id 'OptionManager-VpxSettings'
$MailSender = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "mail.sender"}).Value
$MailSmtpServer = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "mail.smtp.server"}).Value
Send-MailMessage -from $MailSender -to "email@domain.xx" -subject "$BCVM1 Clone Report $Datum" -body "$BCVM1 Clone Report $Datum in attachment" -attachment "$ReportPath" -smtpServer $MailSmtpServer
exit

3. Příklad Scheduled Tasku, jednou týdně pro jeden server (paradoxně nejtěžší na celé té srandě bylo odladit syntaxi pro spouštění cmd a co je vtipné na každé verzi WinSRV může být odlišná… btw toto je na WinSrv 2012)

TaskScript-forOneServer.xml:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2016-12-05T15:33:28.6065713</Date>
    <Author>Domain\user</Author>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <StartBoundary>2016-12-05T02:16:00</StartBoundary>
      <ExecutionTimeLimit>PT8H</ExecutionTimeLimit>
      <Enabled>true</Enabled>
      <ScheduleByWeek>
        <DaysOfWeek>
          <Saturday />
        </DaysOfWeek>
        <WeeksInterval>1</WeeksInterval>
      </ScheduleByWeek>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>S-1-5-19</UserId>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT4H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe</Command>
      <Arguments>-psconsolefile "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noexit -command c:\script\CloneScript1-forOneServer.ps1</Arguments>
    </Exec>
  </Actions>
</Task>

4.  Jen info o prvním spuštění Powercli:

  1. Win PowerShell (Run as Administrator) : Set-ExecutionPolicy RemoteSigned
  2. Win PowerShell (Run as Administrator) : Add-PSSnapin „VMware.VimAutomation.Core“
  3. PowerCli (Run as Administrator) : Set-PowerCLIConfiguration -InvalidCertificateAction ignore -confirm:$false

5. Update 2020

Změny po upgrade powerCLI 6.3R1 -> 6.5R1 a upgrade vCenter z 6.0 -> 6.7:
1.  Ve skriptu změnit Add-PSSnapin „VMware.VimAutomation.Core“ za Import-Module „VMware.VimAutomation.Core“ -Force
 2. V Scheduled Tasku vypustit argument-psconsolefile „C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1“
3. Do system path dát ;C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Modules\

About Author:

Error! Keyboard not detected. Press any key to continue.