PoshCode Logo PowerShell Code Repository

vibackup-lx by Patrick 35 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/957"></script>download | new post

Generate VMware ESX Backup Scripts for Linux Systems
-> check http://zwofreaks.de/derandere/scripting-ecke/

  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.5"
  16. $scriptout = @()
  17. #endregion
  18.  
  19. #region stkmvars
  20. $viUser = "uusdumVMWARE"
  21. $viPass = "1qayxsw2"
  22. #endregion
  23.  
  24. Write-Host "viBackup Script Generator - " $version
  25. Write-Host "--------------------------------------------"
  26.  
  27. if (($vmCon = Connect-VIServer -Protocol https $viServer) -eq "") { exit }
  28. $vm = Get-VM $bakVM -Server $vmCon -ErrorAction SilentlyContinue
  29.  
  30. if (!$vm) {
  31.         Write-Host "No VM found."
  32.         Write-Host "Enter the Display Name from the VI Center:" -NoNewline
  33.         $tvm = Read-Host
  34.         if (!($vm=Get-VM $tvm -ErrorAction SilentlyContinue)) {
  35.                 return $false
  36.                 exit
  37.         }
  38.         Write-Host "You have entered 2 different Names. Please check that the VMX and the VM Name are the same!"
  39.         Write-Host -ForegroundColor yellow "Entered VMX Name: {$bakVM}"
  40.         Write-Host -ForegroundColor yellow "Entered VM Name: {$tvm}"
  41.         Write-Host -ForegroundColor yellow "Returned VM Name: {$vm}"
  42.         Write-Host -ForegroundColor yellow "IS THIS CORRECT (Yes/No)?:" -NoNewline
  43.         $sure = Read-Host
  44.         if ($sure -ne "yes") { exit }
  45. }
  46.  
  47. #check Snapshots
  48. if ((Get-Snapshot -VM $vm -Server $vmCon)) {
  49.         Write-Host "VM has Snapshots. No Backup possible."
  50.         return $false
  51.         exit
  52. }
  53.  
  54. #write script
  55. $scriptname = $vm.name + ".sh"
  56. $vmname = $vm.Name
  57. $vmhost = $vm.Host
  58.  
  59. #generate Text
  60.  
  61. $header = "#!/bin/bash"
  62. $user = "USER=`"${viUser}`""
  63. $pass = "PASS=`"${viPass}`""
  64. $dest = "DEST=`"${lxDest}`""
  65. $lxVM = "VM=`"${vmname}`""
  66.  
  67. #region writefile
  68.  
  69. $scriptout += $header
  70. $scriptout += $user
  71. $scriptout += $pass
  72. $scriptout += $dest
  73. $scriptout += $lxvm
  74. $scriptout += ""
  75. $scriptout += "BKUP=``vmrun -h https://${viserver}/sdk -u `${USER} -p `${PASS} list | grep `${VM}`` "
  76. $scriptout += "SNAPCHECK=``vmware-cmd -H ${viserver} -T ${vmhost} -U `${USER} -P `${PASS} `"`${BKUP}`" hassnapshot`` "
  77. $scriptout += "if [ `"`$SNAPCHECK`" != `"hassnapshot () =`" ]; then `n echo 'VM has a Snapshot. Arborting...' `n exit `n fi"
  78. $scriptout += "vmrun -T esx -h https://${viserver}/sdk -u `${USER} -p `${PASS} snapshot `"`${BKUP}`" vmBackup"
  79.  
  80. # Hard Disk
  81. foreach ($hd in $vm.Harddisks) {
  82.         $hdname = $hd.Filename
  83.         $vifs = "vifs --server ${viserver} --dc 'KM' --username `${USER} --password `${PASS}  --get `"``echo $hdname | sed 's/.vmdk/-flat.vmdk/g'```" `${DEST}/`${VM}.vmdk "
  84.         $scriptout += $vifs
  85. }
  86.  
  87. $scriptout += "vmrun -T esx -h https://${viserver}/sdk -u `${USER} -p `${PASS} deleteSnapshot `"`${BKUP}`" vmBackup"
  88.  
  89. $scriptout | Out-File $scriptname -Encoding $encoding
  90. #endregion
  91.  
  92. Write-Host "Finished"
  93. 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