PoshCode Archive  Check-in [5c57dea9b6]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Generates a Linux VMDK Backup Script
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:5c57dea9b6afe8b5cc34d57c382bafd587d249cafbcf1b30580bd934a02be2a3
User & Date: Patrick 2018-06-10 14:27:50
Context
2018-06-10
14:27
This script customizes the welcome screen which PowerGUI admin console displays on start-up or when a folder is selected in the left-hand tree. check-in: e111cdcac1 user: Dmitry Sotnikov tags: trunk
14:27
Generates a Linux VMDK Backup Script check-in: 5c57dea9b6 user: Patrick tags: trunk
14:27
As promised, I started to expand on Joel’s functions (http://poshcode.org/877). A lot of stuff is missing, but… I have big plans for this module and a PKI module I want to write. check-in: 0f81aa8c25 user: tysonkopczynski tags: trunk
Changes

Changes to vibackup-lx1.ps1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
..
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
..
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
...
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# encoding: ascii
# api: bash
# title: vibackup-lx1.ps1
# description: Generates a Linux VMDK Backup Script 0.7.1
# version: 0.7.1
# type: script
# author: Patrick
# license: CC0
# x-poshcode-id: 1048
# x-archived: 2016-05-20T15:56:56
# x-published: 2009-04-20T04:10:00
#
#
Param (
	$viServer,
	$bakVM,
	$lxDest
)
................................................................................
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

................................................................................
#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

................................................................................
	$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!"



|
|



|
|
|







 







|


<







 







<
<
<
<
<
<
<
<
<







 







|
|








<
<
<
<
<
<
<
<







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
..
21
22
23
24
25
26
27
28
29
30

31
32
33
34
35
36
37
..
61
62
63
64
65
66
67









68
69
70
71
72
73
74
...
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119








120
121
122
123
124
125
126
# encoding: ascii
# api: bash
# title: vibackup-lx1.ps1
# description: Generates a Linux VMDK Backup Script
# version: 0.6
# type: script
# author: Patrick
# license: CC0
# x-poshcode-id: 983
# x-archived: 2016-05-20T15:59:20
# x-published: 2009-03-30T07:44:00
#
#
Param (
	$viServer,
	$bakVM,
	$lxDest
)
................................................................................
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.6"
$scriptout = @()
[regex]$curlpat = "(\s{1})+(.*vmdk$)"

#endregion

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

................................................................................
#check Snapshots
if ((Get-Snapshot -VM $vm -Server $vmCon)) {
	Write-Host "VM has Snapshots. No Backup possible."
	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

................................................................................
	$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}/`${VM}.vmdk `"https://${viserver}/folder/${vmdkpath}?dcPath=${dcPath}&dsName=${dsPath}`" --insecure "
	$vmdkflat = "curl --user `${USER}:`${PASS} -o `${DEST}/`${VM}.vmdk `"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
}









$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!"