-
Notifications
You must be signed in to change notification settings - Fork 1
/
BE-Restore.ps1
147 lines (95 loc) · 4.61 KB
/
BE-Restore.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
###############################################################################
# Script info : Symantec/VERITAS Automated Data Restore Test from Backup.
# Script : BERestore.ps1
# Verified on : Symantec 2014,2015,2020 and VERITAS 2016
# Author : Sijo John
# Version : V-1.0
# Last Modified : 09/05/2018
# The Script can be used to automate file/folder restore test from the Symantec/VERITAS backup.
# Restore test is to ensure that the data is recoverable from the backup..
# .SYNOPSIS
# Usage Example : PS>.BE-Restore.ps1
################################################################################
Begin
{
# Import required PS Modules
$VerbosePreference = 'SilentlyContinue'
Import-Module -name BEMCLI
write-host ("Module imported successfully")
$ContentFolder = "$PSScriptRoot\Content"
$LogFolder = "$PSScriptRoot\logs"
$today = Get-Date -Format "ddMMyyy"
$time = Get-Date
#region generate the transcript log
#Modifying the VerbosePreference in the Function Scope
$Start = Get-Date
$VerbosePreference = 'SilentlyContinue'
$TranscriptName = '{0}_{1}.log' -f $(($MyInvocation.MyCommand.Name.split('.'))[0]), $(Get-Date -Format ddMMyyyyhhmmss)
Start-Transcript -Path "$LogFolder\$TranscriptName"
#endregion generate the transcript log
#Edit this session
$Foldername = "D:\BackupTest"
$Server = "Server01"
$Restorepath = "\\backupexecservername\d$\BackupRestoreTest\$today"
$recipient = "Sijo John"
$FromAddress = "[email protected]"
$ToAddress = "[email protected]"
$Subject = "Symantec/VERITAS backup exec Monthly scheduled backup-Restore Test"
$SMTPServer = "Enter SMTP server name or IP here"
$SMTPPort = "Enter SMTP server port number here"
try {
# create a folder for every day
Get-Item "$Restorepath" -ErrorAction SilentlyContinue
if (!$?) {
New-Item "$Restorepath" -ItemType Directory
}
}
Catch {
Show-Message -Severity high -Message "Failed to create the folder. Permission!"
Write-Verbose -ErrorInfo $PSItem
Stop-Transcript
$PSCmdlet.ThrowTerminatingError($PSItem)
}
Write-Host ("Starting scheduled backup-restore test")
Write-Host ("Restoration start time $time")
$starttime = $time
#Restore data from backup
Submit-BEFileSystemRestoreJob -FileSystemSelection $Foldername -AgentServer $Server -RedirectToPath $Restorepath -NotificationRecipientList $recipient
Write-Host ("Restoration complete time $time")
$completetime = $time
Write-Host ("Scheduled restore job completed successfully, please check and verify the output path $Restorepath")
Write-Host ("Verifying restored files")
#Verify Restored data
$Restoredfile = Get-ChildItem "$Restorepath"| Where {$_.LastWriteTime -gt (Get-Date).AddHours(-24)} -ErrorAction SilentlyContinue
If ($Restoredfile.Exists) {Write-Host "Restored files verified and restore task succeeded"}
Else {Write-Host "File does not exist/ Restore failed- Check restore logs"}
#Verify size of data restored
$size = 0
$size = "{0:N2} MB" -f ((gci $Restorepath -Recurse | Measure-Object -property Length -s).Sum /1MB)
if ($size -gt 0) {$restorestatus = "Success"; Write-Host "Success"} Else {$restorestatus = "Failed" ;Write-Host "Failed - Check logs for details"}
$Summary = @"
Restore test performed on Server = $Server
Restored folder = $Foldername
Restore path = $Restorepath
Restore start time = $starttime
Restore complete time = $completetime
Size of data restored in MB = $size
Restore status = $restorestatus
Please find atached log report of scheduled file restoration test.
"@
Write-Host ("$Summary")
Write-Host ("Gathering logs to send email")
#Email restore report
$Attachments = Get-ChildItem $LogFolder\*.* -include *.txt,*.log | Where{$_.LastWriteTime -gt (Get-Date).AddMinutes(-3)}
$body = "<p>Restore test performed on Server = $Server</p>"
$body += "<p>Restored folder = $Foldername</p>"
$body += "<p>Restore path = $Restorepath</p>"
$body += "<p>Restore start time = $starttime</p>"
$body += "<p>Restore complete time = $completetime</p>"
$body += "<p>Size of data restored in MB = $size</p>"
$body += "<p>Restore status = $restorestatus</p>"
$bosy += "<p>Please find atached log report of scheduled file restoration test.</p>"
Send-Mailmessage -From $FromAddress -To $ToAddress -Subject $Subject -Attachments $Attachments -BodyAsHTML -Body $body -Priority Normal -SmtpServer $SMTPServer -Port $SMTPPort
Write-Host "Report has been sent by E-mail to " $ToAddress " from " $FromAddress
Stop-Transcript
}