-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path1048.ps1
More file actions
132 lines (108 loc) · 4.1 KB
/
Copy path1048.ps1
File metadata and controls
132 lines (108 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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.7.1"
$scriptout = @()
[regex]$curlpat = "(\s{1})+(.*vmdk$)"
[regex]$vmxpat = "(\s{1})+(.*vmx$)"
#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)) {
Write-Host "VM has Snapshots. No Backup possible."
return $false
exit
}
#check HardDisk Mode
foreach ($hd in $vm.harddisks) {
if ($hd.Persistence -ne "Persistent") {
write-host "Hard Disk is not Persistent - no Snapshot allowed."
return $false
exit
}
}
#write script
$scriptname = $vm.name + ".sh"
$vmname = $vm.name
$vmhost = $vm.Host
$vmview= Get-View $vm.id
$vmvname = $vmview.config.files.vmpathname
#generate Text
$header = "#!/bin/bash"
$user = "USER=`"${viUser}`""
$pass = "PASS=`"${viPass}`""
$dest = "DEST=`"${lxDest}`""
$lxVM = "VM=`"${vmname}`""
$dcPath = $vm | Get-Datacenter
#region writefile
$scriptout += $header
$scriptout += $user
$scriptout += $pass
$scriptout += $dest
$scriptout += $lxvm
$scriptout += ""
$scriptout += "BKUP=`"${vmvname}`""
$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. Aborting...' `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
$patstr = $curlpat.split($hdname)
#dirty hack
$patstr[0] = $patstr[0].replace("[","")
$patstr[0] = $patstr[0].replace("]","")
$dsPath = $patstr[0].replace(" ","%20")
$vmdkpath = $patstr[2].replace(" ","%20")
$vmdkflatpath = $patstr[2].replace(".vmdk","-flat.vmdk")
#curl options
$vmdk = "curl --user `${USER}:`${PASS} -o `${DEST}/${vmdkpath} `"https://${viserver}/folder/${vmdkpath}?dcPath=${dcPath}&dsName=${dsPath}`" --insecure "
$vmdkflat = "curl --user `${USER}:`${PASS} -o `${DEST}/${vmdkflatpath} `"https://${viserver}/folder/${vmdkflatpath}?dcPath=${dcPath}&dsName=${dsPath}`" --insecure"
#$vmdk = "vifs --server ${viserver} --dc 'KM' --username `${USER} --password `${PASS} --get `"``echo $hdname```" `${DEST}/`${VM}.vmdk "
#$vmdkflat = "vifs --server ${viserver} --dc 'KM' --username `${USER} --password `${PASS} --get `"``echo $hdname | sed 's/.vmdk/-flat.vmdk/g'```" `${DEST}/`${VM}-flat.vmdk "
$scriptout += $vmdk
$scriptout += $vmdkflat
}
#VMX
$vmg = get-view $vm.id
$vmxfile = $vmxpat.split($vmg.config.files.vmpathname)
$vmxfile = $vmxfile[2]
$scriptout += "curl --user `${USER}:`${PASS} -o `${DEST}/${vmxfile} `"https://${viserver}/folder/${vmxfile}?dcPath=${dcPath}&dsName=${dsPath}`" --insecure "
#Remove Snapshot
$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!"