-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileserverBackup.ps1
executable file
·53 lines (46 loc) · 2.06 KB
/
FileserverBackup.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
<#
RoboCopy Backup Script with E-mail Notification
This script runs a robocopy backup and creates a log file.
An e-mail notification along with the attached log file will be sent out.
Variables to change:
- Source
- Destination
- LogFileLocation
- EmailTo
- SMTPServer
Original Script Created by Michel Stevelmans (http://michelstevelmans.com)
Edited by Jeff Seto (jeff.seto@hogarthww.com)
#>
# Variables
$Source = "E:\"
$Computer = hostname
$Destination = "\\server\Backup\$Computer\"
$Date = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
$LogFileLocation = "\\server\Backup\Logs\$Computer\"
$LogFilename = "BackupLog_$($Date)"
$EmailFrom = "Backup@company.com"
$EmailTo = "BackupLog@company.com"
$EmailBody = "Robocopy backup completed. See attached log file for details."
$EmailSubject = "$Computer $Date Backup Completed"
$SMTPServer = "relay.company.com"
$SMTPPort = "25"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, $SMTPPort)
# Reset archive bit (required workaround for Windows 2012/8 robocopy)
attrib -a "$($Source)*.*" /S /D
# Run robocopy backup
robocopy $Source $Destination /MIR /COPY:DAT /M /MT:32 /V /W:1 /R:0 /NP /TS /BYTES /XF ".DS_Store" ".apdisk" ".TemporaryItems" "Thumbs.db" /XD "$($Source)`$RECYCLE.BIN" "$($Source)System Volume Information" /LOG:"C:\Logs\backup_report.log"
# Update log filename, copy to share and send e-mail
if (($LastExitCode -eq 0) -or ($LastExitCode -eq 1)){
Copy-Item "C:\Logs\backup_report.log" "$LogFileLocation$($LogFilename)_SUCCESSFUL.log"
$EmailSubject += " Successfully."
$EmailBody += "`r`n`r`nLog file: $LogFileLocation$($LogFilename)_SUCCESSFUL.log"
$Message = New-Object Net.Mail.MailMessage($EmailFrom, $EmailTo, $EmailSubject, $EmailBody)
$SMTPClient.Send($Message)
}
else {
Copy-Item "C:\Logs\backup_report.log" "$LogFileLocation$($LogFilename)_FAILED.log"
$EmailSubject += " with Errors."
$EmailBody += "`r`n`r`nLog file: $LogFileLocation$($LogFilename)_FAILED.log"
$Message = New-Object Net.Mail.MailMessage($EmailFrom, $EmailTo, $EmailSubject, $EmailBody)
$SMTPClient.Send($Message)
}