-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEsetRAServerBak.ps1
160 lines (142 loc) · 4.73 KB
/
EsetRAServerBak.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
function LogFile ($Message) {
$LogDate = Get-Date -UFormat "%D %T"
Add-Content -Value "$($LogDate) - $($Message)" -Path $ERAPath$LogName
}
Function Mailer($MError) {
$Message = @"
There seems to have been a problem with the ESET_RA backup. Please investigate.
Error - $MError
Cheers,
ESET Backup
"@
$emailto = "" #Gmail group
$subject = "WARNING - ESET Backup Failure"
$emailFrom = ""
$smtpserver = ""
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $message)
}
##############################################################
<# Lets define some variables #>
$ERAPath = "$env:ProgramData\ESET\ESET Remote Administrator\"
# Generate date of week as int for file names
$DayDigit = (get-date).dayofweek.value__
# Build log file name
$LogName = "ERAServerBak.$($DayDigit).log"
# Build bak name
$BakName = "ERAServerBak.$($DayDigit)"
###############################################################
# Check if log already exists, if so then delete
if (Test-Path -Path $ERAPath$LogName){
Remove-Item -Path $ERAPath$LogName -Force
}
LogFile -Message "Backup started..."
# Get ERA services
try {
$ERAServ = get-service | where {$_.name -eq 'ERA_SERVER'}
}
# Log error if no service was found
catch {
$ErrorLog = "The ERA_SERVER service was not found."
LogFile -Message $ErrorLog
Mailer -MError $ErrorLog
Exit
}
# Log message if the service was already stopped
if ($ERAServ.Status -eq 'Stopped'){
LogFile -Message "The ERA_SERVER service is already in the stopped state."
}
elseif ($ERAServ.Status -eq 'Running') {
# Stop services and add to log
try {
LogFile -Message "Attempting to stop $($ERAServ.Name) service."
Stop-Service $($ERAServ.Name) -ErrorAction stop
LogFile -Message "Service $($ERAServ.Name) stopped."
}
# If the service/s cannot be stopped then log error and quit
catch {
$ErrorLog = "The service $($ERAServ.Name) cannot be stopped - $($_.exception.innerexception.innerexception.message)."
LogFile -Message $ErrorLog
Mailer -MError $ErrorLog
Exit
}
}
# Now the ERA services are stopped we can copy/archive the application data
# Make a copy of the application data
LogFile -Message "Copying Data..."
try {
Copy-Item -Path ($ERAPath + "Server\") -Destination ($ERAPath + "ERAServerBak\") -Recurse -ErrorAction stop
LogFile "Successfully copied application data from $($ERAPath + "Server\")."
}
catch {
LogFile "Error when copying Eset application data, restarting service then aborting."
try {
Start-Service $($ERAServ.Name) -ErrorAction stop
LogFile -Message "Service $($ERAServ.Name) restarted."
}
# If the service/s cannot be started then log error and quit
catch {
$ErrorLog = "The ESET_Server service was stopped but cannot be started."
LogFile -Message $ErrorLog
Mailer -MError $ErrorLog
Exit
}
}
# Start services back up once copy is complete
try {
Start-Service $($ERAServ.Name) -ErrorAction stop
LogFile -Message "Service $($ERAServ.Name) started."
}
# If the service cannot be started then log error and quit
catch {
$ErrorLog = "The ESET_Server service was stopped but cannot be started."
LogFile -Message $ErrorLog
Mailer -MError $ErrorLog
Exit
}
# Create 7-zip archive from copied data
LogFile -Message "Attempting to create 7-Zip archive..."
try {
& $env:ProgramFiles'\7-Zip\7za.exe' a -t7z ($ERAPath + $BakName + '.7z') -mx9 ($ERAPath + 'ERAServerBak\') -r
LogFile -Message "Files successfully added to archive."
}
catch {
$ErrorLog = "Unable to create 7-Zip archive."
LogFile -Message $ErrorLog
Mailer -MError $ErrorLog
Exit
}
LogFile -Message "Attempting to remove data copy..."
# Once the archive is built we can delete the data we copied
try{
Remove-Item ($ERAPath + "ERAServerBak\") -Recurse -Force -ErrorAction stop
LogFile -Message "Data successfully removed."
}
catch {
$ErrorLog = "Unable to remove old data after archive was built."
LogFile -Message $ErrorLog
Mailer -MError $ErrorLog
Exit
}
# Move archive to storage.
LogFile -Message "Attempting to move archive."
cmd.exe /c "C:\Program Files (x86)\cwRsync\ERAServerBak.cmd" | Out-Null
if ($lastexitcode -ne '0'){
$ErrorLog = "Rsync failed with exit code $lastexitcode."
LogFile -Message $ErrorLog
Mailer -MError $ErrorLog
}
Else {
LogFile -Message "Successfully moved archive."
# Delete archive from local storage
try {
Remove-Item ($ERAPath + $BakName + '.7z') -Force
}
catch {
$ErrorLog = "Unable to delete archive."
LogFile -Message $ErrorLog
Mailer -MError $ErrorLog
Exit
}
}
LogFile -Message "Backup complete."