PoshCode Logo PowerShell Code Repository

vibackup-lx1.ps1 (modification of post by Patrick view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1048"></script>download | new post

Generates a Linux VMDK Backup Script 0.7.1

  1. Param (
  2.         $viServer,
  3.         $bakVM,
  4.         $lxDest
  5. )
  6.  
  7. #region check
  8. if (!$viServer) { $viServer = Read-Host -Prompt "VI Server " }
  9. if (!$bakVM) { $bakVM = Read-Host -Prompt "VM to Backup " }
  10. if (!$lxDest) { $lxDest = Read-Host -Prompt "Backup Path (ex. /srv/backup) " }
  11. #endregion
  12.  
  13. #region globalvars
  14. $encoding = "OEM"
  15. $version = "0.7.1"
  16. $scriptout = @()
  17. [regex]$curlpat = "(\s{1})+(.*vmdk$)"
  18. [regex]$vmxpat = "(\s{1})+(.*vmx$)"
  19. #endregion
  20.  
  21. #region stkmvars
  22. $viUser = "vmware"
  23. $viPass = "vmware"
  24. #endregion
  25.  
  26. Write-Host "viBackup Script Generator - " $version
  27. Write-Host "--------------------------------------------"
  28.  
  29. if (($vmCon = Connect-VIServer -Protocol https $viServer) -eq "") { exit }
  30. $vm = Get-VM $bakVM -Server $vmCon -ErrorAction SilentlyContinue
  31.  
  32. if (!$vm) {
  33.         Write-Host "No VM found."
  34.         Write-Host "Enter the Display Name from the VI Center:" -NoNewline
  35.         $tvm = Read-Host
  36.         if (!($vm=Get-VM $tvm -ErrorAction SilentlyContinue)) {
  37.                 return $false
  38.                 exit
  39.         }
  40.         Write-Host "You have entered 2 different Names. Please check that the VMX and the VM Name are the same!"
  41.         Write-Host -ForegroundColor yellow "Entered VMX Name: {$bakVM}"
  42.         Write-Host -ForegroundColor yellow "Entered VM Name: {$tvm}"
  43.         Write-Host -ForegroundColor yellow "Returned VM Name: {$vm}"
  44.         Write-Host -ForegroundColor yellow "IS THIS CORRECT (Yes/No)?:" -NoNewline
  45.         $sure = Read-Host
  46.         if ($sure -ne "yes") { exit }
  47. }
  48.  
  49. #check Snapshots
  50. if ((Get-Snapshot -VM $vm -Server $vmCon)) {
  51.         Write-Host "VM has Snapshots. No Backup possible."
  52.         return $false
  53.         exit
  54. }
  55.  
  56. #check HardDisk Mode
  57. foreach ($hd in $vm.harddisks) {
  58.         if ($hd.Persistence -ne "Persistent") {
  59.                 write-host "Hard Disk is not Persistent - no Snapshot allowed."
  60.                 return $false
  61.                 exit
  62.         }
  63. }
  64.  
  65. #write script
  66. $scriptname = $vm.name + ".sh"
  67. $vmname = $vm.name
  68. $vmhost = $vm.Host
  69. $vmview= Get-View $vm.id
  70. $vmvname = $vmview.config.files.vmpathname
  71.  
  72. #generate Text
  73.  
  74. $header = "#!/bin/bash"
  75. $user = "USER=`"${viUser}`""
  76. $pass = "PASS=`"${viPass}`""
  77. $dest = "DEST=`"${lxDest}`""
  78. $lxVM = "VM=`"${vmname}`""
  79. $dcPath = $vm | Get-Datacenter
  80.  
  81. #region writefile
  82.  
  83. $scriptout += $header
  84. $scriptout += $user
  85. $scriptout += $pass
  86. $scriptout += $dest
  87. $scriptout += $lxvm
  88. $scriptout += ""
  89. $scriptout += "BKUP=`"${vmvname}`""
  90. $scriptout += "SNAPCHECK=``vmware-cmd -H ${viserver} -T ${vmhost} -U `${USER} -P `${PASS} `"`${BKUP}`" hassnapshot`` "
  91. $scriptout += "if [ `"`$SNAPCHECK`" != `"hassnapshot () =`" ]; then `n echo 'VM has a Snapshot. Aborting...' `n exit `n fi"
  92. $scriptout += "vmrun -T esx -h https://${viserver}/sdk -u `${USER} -p `${PASS} snapshot `"`${BKUP}`" vmBackup"
  93.  
  94. # Hard Disk
  95. foreach ($hd in $vm.Harddisks) {
  96.         $hdname = $hd.Filename
  97.         $patstr = $curlpat.split($hdname)
  98.         #dirty hack
  99.         $patstr[0] = $patstr[0].replace("[","")
  100.         $patstr[0] = $patstr[0].replace("]","")
  101.        
  102.         $dsPath = $patstr[0].replace(" ","%20")
  103.         $vmdkpath = $patstr[2].replace(" ","%20")
  104.         $vmdkflatpath = $patstr[2].replace(".vmdk","-flat.vmdk")
  105.        
  106.         #curl options
  107.         $vmdk = "curl --user `${USER}:`${PASS} -o `${DEST}/${vmdkpath} `"https://${viserver}/folder/${vmdkpath}?dcPath=${dcPath}&dsName=${dsPath}`" --insecure "
  108.         $vmdkflat = "curl --user `${USER}:`${PASS} -o `${DEST}/${vmdkflatpath} `"https://${viserver}/folder/${vmdkflatpath}?dcPath=${dcPath}&dsName=${dsPath}`" --insecure"
  109.  
  110.         #$vmdk = "vifs --server ${viserver} --dc 'KM' --username `${USER} --password `${PASS}  --get `"``echo $hdname```" `${DEST}/`${VM}.vmdk "
  111.         #$vmdkflat = "vifs --server ${viserver} --dc 'KM' --username `${USER} --password `${PASS}  --get `"``echo $hdname | sed 's/.vmdk/-flat.vmdk/g'```" `${DEST}/`${VM}-flat.vmdk "
  112.  
  113.         $scriptout += $vmdk
  114.         $scriptout += $vmdkflat
  115. }
  116.  
  117. #VMX
  118. $vmg = get-view $vm.id
  119. $vmxfile = $vmxpat.split($vmg.config.files.vmpathname)
  120. $vmxfile = $vmxfile[2]
  121.  
  122. $scriptout += "curl --user `${USER}:`${PASS} -o `${DEST}/${vmxfile} `"https://${viserver}/folder/${vmxfile}?dcPath=${dcPath}&dsName=${dsPath}`" --insecure "
  123.  
  124. #Remove Snapshot
  125. $scriptout += "vmrun -T esx -h https://${viserver}/sdk -u `${USER} -p `${PASS} deleteSnapshot `"`${BKUP}`" vmBackup"
  126.  
  127. $scriptout | Out-File $scriptname -Encoding $encoding
  128. #endregion
  129.  
  130. Write-Host "Finished"
  131. Write-Host "Don't forget to convert the script under *nix/Linux with dos2unix!"

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me