PoshCode Logo PowerShell Code Repository

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

Generates a Linux VMDK Backup Script

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