-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathpowershell_salt-createsnapshot.ps1
343 lines (328 loc) · 12.9 KB
/
powershell_salt-createsnapshot.ps1
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import-module -Name C:\testbc\lib\standard\Logging.psm1
$MVERSION = $(get-host).Version.Major
If($MVERSION -eq 2)
{
Import-Module "C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1"
}
else
{
import-module AWSPowerShell -errorAction Stop
}
import-module -Name C:\testbc\lib\standard\errohandling.psm1
#import-module AWSPowerShell -errorAction Stop
$VerbosePreference = 'Continue'
$DebugPreference = 'Continue'
start-sleep -m (Get-Random -minimum 1000 -maximum 3000)
$strtimestamp = Get-Date -format "yyyyMMddHHmmssffffff"
write-host "###################Application starting at $strtimestamp#####################"
$strtimedate = Get-Date -format "MM-dd-yyyy"
$outputfile2='C:\testbc\log\' +$strtimedate+'\salt-createsnapshot-history.log'
function uploadtxttos3($snapshotloc,$sbucket,$scustomerprefixval)
{
try
{
write-host '--------------In the uploadtxttos3 function.-----------'
if (!(Test-Path -path $snapshotloc)){New-Item -ItemType directory -Path $snapshotloc}
if ((Get-S3Bucket -BucketName $sbucket).BucketName)
{
write-host 'Bucket Name exists...'
}
else
{
New-S3Bucket -BucketName $sbucket -errorAction Stop
write-host 'Bucket is creating...'
}
$slist = Get-ChildItem $snapshotloc | Where-Object {$_.Extension -eq ".txt"}
if($slist)
{
foreach ($sfiles in $slist)
{
write-host $sfiles.Basename
$finalkey = '/backup/'+$scustomerprefixval+'/'+ $sfiles.Basename+'.txt'
write-host $finalkey
$fName = $snapshotloc + $sfiles.Basename +'.txt'
write-host "File Name : $fName"
Write-S3Object -BucketName $sbucket -Key $finalkey -File $fName -ServerSideEncryption 'AES256' -errorAction Stop
write-host "The file is uploaded to aws S3 : $fName"
}
}
else
{
write-host 'No txt files present to upload to s3.'
}
}
catch
{
write-host '#############Error################'
write-host $_.Exception.Message
write-host '-----------uploadtxttos3 is having issues. Please contact administrator.-------'
exit 1
}
}
function createsnapshot($customerprefix,$scustomerprefixval,$psversion)
{
$retries = 30
$retrycount = 0
$completed = $false
while (-not $completed)
{
try
{
write-host '-----------------Running createsnapshot Function---------------------'
if (!(Test-Path -path $snapshotloc)){New-Item -ItemType directory -Path $snapshotloc}
$urlinstance = "http://169.254.169.254/latest/meta-data/instance-id"
$urlavb = "http://169.254.169.254/latest/meta-data/placement/availability-zone/"
$macId = "http://169.254.169.254/latest/meta-data/network/interfaces/macs/"
$vpcid = ""
$backuptype = 'online'
if ($psversion -eq 2)
{
write-host "Using System.Net.WebRequest for ps 2.0"
$r = [System.Net.WebRequest]::Create($urlinstance)
$resp = $r.GetResponse()
$reqstream = $resp.GetResponseStream()
$sr = new-object System.IO.StreamReader $reqstream
$InstanceName = $sr.ReadToEnd()
write-host "Instance ID is $InstanceName"
$r = [System.Net.WebRequest]::Create($urlavb)
$resp = $r.GetResponse()
$reqstream = $resp.GetResponseStream()
$sr = new-object System.IO.StreamReader $reqstream
$Region = $sr.ReadToEnd()
write-host "Region is $Region"
$rmacId = [System.Net.WebRequest]::Create($macId)
$respmacid = $rmacId.GetResponse()
$reqstreammacid = $respmacid.GetResponseStream()
$srmacid = new-object System.IO.StreamReader $reqstreammacid
$macid = $srmacid.ReadToEnd()
write-host "THe Mac ID is $macid "
$vpcId = "http://169.254.169.254/latest/meta-data/network/interfaces/macs/"+$macid+"vpc-id"
$rvpcId = [System.Net.WebRequest]::Create($vpcId)
$respvpcid = $rvpcId.GetResponse()
$reqstreamvpcid = $respvpcid.GetResponseStream()
$srvpcid = new-object System.IO.StreamReader $reqstreamvpcid
$vpcid = $srvpcid.ReadToEnd()
write-host "THe VPC ID is $vpcid "
}
else
{
write-host "Using invoke-restmethod for ps 3.0 or more"
$InstanceName = invoke-restmethod -uri $urlinstance -errorAction Stop
write-host "Instance ID is $InstanceName"
$Region = invoke-restmethod -uri $urlavb -errorAction Stop
$macid = invoke-restmethod -uri $macId -errorAction Stop
write-host "Instance ID is $macid"
$vpcId = "http://169.254.169.254/latest/meta-data/network/interfaces/macs/"+$macid+"vpc-id"
$vpcid = invoke-restmethod -uri $vpcId -errorAction Stop
write-host "Instance ID is $vpcid"
}
$Region = $Region.Substring(0,$Region.Length-1)
Set-DefaultAWSRegion $Region
$filtertagkey = New-Object Amazon.EC2.Model.Filter -errorAction Stop
$filtertagkey.Name = "attachment.instance-id"
$filtertagkey.Value.Add($InstanceName)
$filterval = New-Object Amazon.EC2.Model.Filter -errorAction Stop
$filterval.Name = "tag-value"
$stime = Get-Date -format 'yyyyMMddHHmmss'
$filterval.Value.Add($scustomerprefixval)
$sgetallsnapshotid = Get-EC2Volume -Filter $filtertagkey -errorAction Stop
write-host $sgetallsnapshotid.VolumeId
$Attachments = $sgetallsnapshotid.VolumeId.Attachment
write-host $Attachments.Device
foreach ($s in $sgetallsnapshotid)
{
write-host "In the loop.."
$sdesc = "Snapshot created for volume id "+$s.VolumeId+" from instance id "+$InstanceName+" at time "+$stime
start-sleep -m (Get-Random -minimum 2000 -maximum 6000)
$getsnapshotID = New-EC2Snapshot -VolumeId $s.VolumeId -Description $sdesc -errorAction Stop
write-host "The New snapshot created is :"
write-host $getsnapshotID.SnapshotId
write-host "The device ID is :"
$sdriveid = $s.Attachment | select device
write-host $sdriveid.Device
$createdate = Get-Date -format 'dd-MMM-yyyy'
$expirydate = (get-date).AddDays($expirydays).ToString("dd-MMM-yyyy")
$expiryformat = (get-date).AddDays($expirydays).ToString("yyyyMMddHHmmss")
$sBackupInfo = $backuptype +'|CreatedDate:'+$createdate+'|ExpiryDate:'+$expirydate
write-host $sBackupInfo
$snamelabel = $InstanceName + '-'+$s.VolumeId+'-'+$sdriveid.Device+'-'+$stime
write-host $snamelabel
$strhostname = $env:COMPUTERNAME
$smahinelabel = $strhostname +"-" + $scustomerprefixval
$tagname = New-Object Amazon.EC2.Model.Tag
$tagname.Key = "Name"
$tagname.Value = $snamelabel
$tagBackupInfo = New-Object Amazon.EC2.Model.Tag
$tagBackupInfo.Key = "BackupInfo"
$tagBackupInfo.Value = $sBackupInfo
$tagcustprefix = New-Object Amazon.EC2.Model.Tag
$tagcustprefix.Key = "customerPrefix"
$tagcustprefix.Value = $scustomerprefixval
$tagmachinel = New-Object Amazon.EC2.Model.Tag
$tagmachinel.Key = "machineLabel"
$tagmachinel.Value = $smahinelabel
New-EC2Tag -ResourceId $getsnapshotID.SnapshotId -Tag $tagname, $tagBackupInfo, $tagmachinel, $tagcustprefix -errorAction Stop
$sappendtxt = 'YYYYMMDDHHMMSS:'+$stime + ' & expdate:'+$expiryformat +' & SnapshotID:'+ $getsnapshotID.SnapshotId +' & VolumeID:'+$s.VolumeId + ' & region:'+ $Region+ ' & Deviceid:'+$sdriveid.Device
$file = $snapshotloc+$InstanceName+ '-' + $smahinelabel + '-' + $vpcid +'.txt'
if (Test-Path $file)
{
write-host 'File exists.'
if ((Get-Item $file).length -gt 10240kb)
{
write-host 'File is greater than 5 mb'
$strtimestamp = Get-Date -format "yyyyMMddHHmmss"
$filerename = $snapshotloc+$InstanceName+$strtimestamp+'.txt'
Rename-Item $file $filerename
}
}
write-host 'Appending the Text.'
Start-Sleep -s 4
start-sleep -m (Get-Random -minimum 1000 -maximum 4000)
Add-Content $file "`n$sappendtxt" -errorAction SilentlyContinue
write-host 'Appended to the Text file.'
}
$completed = $true
write-host "Completed value is true.."
}
catch
{
$serror = $_.Exception
write-host "Into catch..primary Error details"
write-host $serror
if ($retrycount -ge $retries)
{
write-host '#############Error################'
write-host $_.Exception.Message
write-host '-----------Failed for 5 attempts.-------'
write-host '-----------CreateSnapshot is having issues. Please contact administrator.-------'
exit 1
}
else
{
if($serror -Contains "Request limit exceeded")
{
write-host "Request limit exceeded occured. Again running into the loop."
Start-Sleep -s 120
start-sleep -m (Get-Random -minimum 90000 -maximum 300000)
$retrycount++
}
else
{
write-host "New Exception occured...."
write-host '#############Error################'
write-host $_.Exception.Message
Start-Sleep -s 120
start-sleep -m (Get-Random -minimum 90000 -maximum 300000)
$retrycount++
}
}
}
}
}
function Getparamsvalue
{
param(
[Parameter(Mandatory=$true)] [String]$contentfile,
[Parameter(Mandatory=$true)] [String]$strparameter
)
$inum = 0
$content = Get-Content $contentfile
$fullstrparameter = $strparameter + ':'
foreach ($line in $content)
{
if($line.StartsWith($fullstrparameter))
{
$slen = $fullstrparameter.length
$sval = $line.Substring($slen)
write-host $sval
$finalval = $sval.trim()
write-host $finalval
$inum = 1
return $finalval
}
}
if($inum -eq 0){return $null}
}
function runcreatesnapshot([String]$sbucket,[String]$expirydays,[String]$customerPrefix,[String]$pversion)
{
try{
$backuptype = 'online'
write-host '---------------Running the Create snapshot-------------------'
write-host "The Backup Type is $backuptype"
$customerprefixTag = 'customerPrefix'
$dirloc = "c:\testbc\"
$snapshotloc = "c:\snapshot\"
if([string]::IsNullOrEmpty($customerPrefix))
{
write-host '#############Error################'
write-host "The CustomerPrefix value is having some issues. Please contact administrator."
exit 1
}
else
{
createsnapshot -customerprefix $customerprefixTag -scustomerprefixval $customerprefix -psversion $pversion
start-sleep -m (Get-Random -minimum 2000 -maximum 6000)
uploadtxttos3 -snapshotloc $snapshotloc -sbucket $sbucket -scustomerprefixval $customerprefix
}
}
catch{
write-host '#############Error################'
write-host "Error occurred during runcreatesnapshot createsnapshot...."
write-host $_.Exception.Message
#Write-Host($error)
exit 1
}
}
try{
$contentfile = 'C:\salt\conf\grains'
$strcurrentDate = Get-Date -format "yyyyMMddHHmmss"
$sbucket=$env:BUCKET_NAME
$LogFilePreference = $outputfile2
$strtime = Get-Date -format "MM-dd-yyyy HH:mm:ss:ffffff"
write-host "###################Application started running at $strtime###################"
#if([string]::IsNullOrEmpty($sbucket))
#{
# write-host "#############Error################"
# write-host "Bucket Name does not exist. Please contact administrator."
# exit 1
#}
write-host 'Checking the Powershell version..'
$PSVERSION = $(get-host).Version.Major
If($PSVERSION -eq 2)
{
$PSVERSION = 2
}
else
{
$PSVERSION = 3
}
write-host "Final PSVERSION value is $PSVERSION"
$CustPrefix = Getparamsvalue -contentfile $contentfile -strparameter "CustPrefix"
write-host "The customer prefix is $CustPrefix"
$accessKey=$env:AWS_ACCESS_KEY
$secretKey=$env:AWS_SECRET_ACCESS_KEY
if($accessKey -ne $null -and $secretKey -ne $null)
{
write-host 'Able to read the AWS keys from Envirnment variables.Using AWS Keys to complete this task..'
$creds = New-AWSCredentials -AccessKey $accessKey -SecretKey $secretKey
Set-AWSCredentials -Credential $creds
}
$strBACKUP_ENVDate=$env:BACKUP_EXPIRY_DAYS
if([string]::IsNullOrEmpty($strBACKUP_ENVDate)) { $strBACKUP_ENVDate=30 }
$intexpiryDate = [int]$strBACKUP_ENVDate
start-sleep -m (Get-Random -minimum 1000 -maximum 30000)
#runcreatesnapshot([String]$sbucket,[String]$expirydays,[String]$customerPrefix,[String]$psversion)
runcreatesnapshot -sbucket $sbucket -expirydays $intexpiryDate -customerPrefix $CustPrefix -pversion $PSVERSION
#$strprocessoutput = Start-Process powershell "C:\testbc\lib\standard\createsnapshot.ps1 -sbucket $sbucket -expirydays $intexpiryDate -customerPrefix $CustPrefix -pversion $PSVERSION" -RedirectStandardOutput $outputfile -PassThru -windowstyle Hidden -Wait
$strtime = Get-Date -format "MM-dd-yyyy HH:mm:ss:ffffff"
write-host "###################Application ending at $strtime###################"
}
catch{
write-host '#############Error################'
write-host "Error while running the sql calling program. Please contact administrator."
write-host $_.Exception.Message
#write-host($error)
$strtime = Get-Date -format "MM-dd-yyyy HH:mm:ss:ffffff"
write-host "###################Application ending at $strtime###################"
exit 1
}