PoshCode Archive  Artifact [2b070f5e31]

Artifact 2b070f5e316d8149ad243b2d0fe988493cf522c2087ed4ce142bca1512b640b4:

  • File vibackup-linux-script.ps1 — part of check-in [24c657c7c6] at 2018-06-10 14:27:16 on branch trunk — Generate VM ware Backups via this script. (user: Patrick size: 3479)

# encoding: ascii
# api: bash
# title: vibackup linux script
# description: Generate VM ware Backups via this script.
# version: 0.4
# type: script
# author: Patrick
# license: CC0
# x-poshcode-id: 938
# x-archived: 2016-05-20T16:06:30
# x-published: 2009-03-12T09:04:00
#
# Only suitable for ESX Machines.
# You need vmware-rcli and vmware-api for this on the linux host.
# Generated script must be converted to unix file encoding (dos2unix ). Check for passwords in the script. Clear type!
#
Param (
	$viServer,
	$bakVM,
	$lxDest,
)

#region check
if (!$viServer) { $viServer = Read-Host -Prompt "VI Server " }
if (!$bakVM) { $bakVM = Read-Host -Prompt "VM to Backup " }
if (!$lxDest) { $lxDest = Read-Host -Prompt "Backup Path (ex. /srv/backup) " }
#endregion

#region globalvars
$encoding = "OEM"
$version = "0.4"
$scriptout = @()
#endregion

#region stkmvars
$viUser = "vmware"
$viPass = "vmware"
#endregion

Write-Host "viBackup Script Generator - " $version
Write-Host "--------------------------------------------"

if (($vmCon = Connect-VIServer -Protocol https $viServer) -eq "") { exit }
$vm = Get-VM $bakVM -Server $vmCon -ErrorAction SilentlyContinue

if (!$vm) {
	Write-Host "No VM found."
	Write-Host "Enter the Display Name from the VI Center:" -NoNewline
	$tvm = Read-Host
	if (!($vm=Get-VM $tvm -ErrorAction SilentlyContinue)) {
		return $false
		exit
	}
	Write-Host "You have entered 2 different Names. Please check that the VMX and the VM Name are the same!"
	Write-Host -ForegroundColor yellow "Entered VMX Name: {$bakVM}"
	Write-Host -ForegroundColor yellow "Entered VM Name: {$tvm}"
	Write-Host -ForegroundColor yellow "Returned VM Name: {$vm}"
	Write-Host -ForegroundColor yellow "IS THIS CORRECT (Yes/No)?:" -NoNewline
	$sure = Read-Host
	if ($sure -ne "yes") { exit }
}

#check Snapshots
if ((Get-Snapshot -VM $vm -Server $vmCon | Measure-Object | select count).Count -ne "0") {
	Write-Host "VM has Snapshots. No Backup possible."
	return $false
	exit
}

#write script
$scriptname = $vm.name + ".sh"
$vmname = $vm.Name
$vmhost = $vm.Host

#generate Text

$header = "#!/bin/bash"
$user = "USER=`"${viUser}`""
$pass = "PASS=`"${viPass}`""
$dest = "DEST=`"${lxDest}`""
$lxVM = "VM=`"${vmname}`""

#region writefile

$scriptout += $header 
$scriptout += $user 
$scriptout += $pass 
$scriptout += $dest 
$scriptout += $lxvm 
$scriptout += "" 
$scriptout += "BKUP=``vmrun -h https://${viserver}/sdk -u `${USER} -p `${PASS} list | grep `${VM}`` " 
$scriptout += "SNAPCHECK=``vmware-cmd -H ${viserver} -T ${vmhost} -U `${USER} -P `${PASS} `"`${BKUP}`" hassnapshot`` "
$scriptout += "if [ `"`$SNAPCHECK`" != `"hassnapshot () =`" ]; then `n echo 'VM has a Snapshot. Arborting...' `n exit `n fi" 
$scriptout += "vmrun -T esx -h https://${viserver}/sdk -u `${USER} -p `${PASS} snapshot `"`${BKUP}`" vmBackup"

# Hard Disk
foreach ($hd in $vm.Harddisks) {
	$hdname = $hd.Filename
	$vifs = "vifs --server ${viserver} --dc 'KM' --username `${USER} --password `${PASS}  --get `"``echo $hdname | sed 's/.vmdk/-flat.vmdk/g'```" `${DEST}/`${VM}.vmdk "
	$scriptout += $vifs
}

$scriptout += "vmrun -T esx -h https://${viserver}/sdk -u `${USER} -p `${PASS} deleteSnapshot `"`${BKUP}`" vmBackup" 

$scriptout | Out-File $scriptname -Encoding $encoding
#endregion

Write-Host "Finished"
Write-Host "Don't forget to convert the script under *nix/Linux with dos2unix!"