PowerShell Script for S3 Backup and Email Notification
Overview
This PowerShell script automates the backup of files from a specified directory to an Amazon S3 bucket and sends email notifications with a log file attachment. It checks for errors during the backup process and adjusts the email subject to reflect the importance of any errors found.
Prerequisites
- AWS CLI installed and configured with appropriate credentials.
- AWS Tools for PowerShell installed.
- Access to an SMTP server for sending emails.
Configuration
- Source Directory: Modify the
$sourceDirectory
variable to specify the directory containing the files to be backed up. - S3 Bucket Name: Set the
$s3BucketName
variable to the name of your Amazon S3 bucket. - Recipients: Update the
$recipients
variable with the email addresses of the recipients who will receive the backup status notifications. - Email Parameters: Adjust the
$emailFrom
,$emailSMTPServer
, and$emailSMTPPort
variables according to your email configuration. - Log File Path: Set the
$logFilePath
variable to the desired location for the log file.
Script Execution
- The script begins by defining the necessary variables and creating a log file to capture the backup process’s status.
- It iterates through each file in the specified source directory and uploads them to the designated S3 bucket using the AWS CLI
aws s3 cp
command. The result of each upload operation is appended to the log file. - After completing the backup process, the script checks the log file for any errors or failures by searching for specific keywords such as “error” or “failed”.
- If errors are found, the email subject is set to indicate an error (
$emailSubjectError
). Otherwise, it indicates success ($emailSubjectSuccess
). - Finally, the script sends an email to the specified recipients with the log file attached. The email subject reflects the status of the backup process (success or error).
Usage
- Save the script with a
.ps1
extension, for example,backup_to_s3.ps1
. - Execute the script either manually or schedule it to run at desired intervals using Task Scheduler or another scheduling tool.
- Monitor the email notifications for the status of the backup process and investigate any errors reported in the log file.
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
# Set the source directory
$sourceDirectory = "C:\Backup"
# Set the S3 bucket name
$s3BucketName = "s3-bucket"
# Set recipients for email
$recipients = "[email protected]", "[email protected]"
# Set email parameters
$emailFrom = "[email protected]"
$emailSubjectSuccess = "S3 Backup Log - Success"
$emailSubjectError = "S3 Backup Log - Error"
$emailSMTPServer = "example.mail.protection.outlook.com"
$emailSMTPPort = 25
# Set log file path
$logFilePath = "C:\temp\s3_backup_log.txt"
# Create a log file
New-Item -Path $logFilePath -ItemType File -Force | Out-Null
# Loop through each file in the source directory
Get-ChildItem -Path $sourceDirectory | ForEach-Object {
# Upload the file to S3 bucket and log the result
$result = aws s3 cp $_.FullName s3://$s3BucketName/backup/ --region ap-southeast-2 2>&1
$result | Out-File -Append -FilePath $logFilePath
}
# Check for errors in the log file
$errorsFound = Select-String -Path $logFilePath -Pattern "error|failed" -Quiet
# Determine email subject based on errors
if ($errorsFound) {
$emailSubject = $emailSubjectError
} else {
$emailSubject = $emailSubjectSuccess
}
# Send email with the log file attached
Send-MailMessage -From $emailFrom -To $recipients -Subject $emailSubject -SmtpServer $emailSMTPServer -Port $emailSMTPPort -Attachments $logFilePath
Notes
- Ensure that the AWS CLI and AWS Tools for PowerShell are properly installed and configured before running the script.
- Verify the SMTP server configuration and network access to ensure successful email delivery.
This post is licensed under
CC BY 4.0
by the author.