· 8 years ago · Nov 21, 2017, 05:50 PM
1# Make sure you have AWS PowerShell module installed
2# Install-Module -Name AWSPowerShell
3
4$ErrorActionPreference = 'Stop'
5
6# MySQL Settings
7$server = '.'
8$database = '<DB NAME>'
9$user = '<DB USER>'
10$password = '<PASSWORD>'
11
12# AWS Settings
13$s3Bucket = '<BUCKET NAME>'
14$region = '<AWS REGION (e.g eu-west1)>'
15$accessKey = '<ACCESS KEY>'
16$secretKey = '<SECRET KEY>'
17
18# Local machine and file name settings
19$dumFileExtension = '.sql'
20$archiveExtension = '.zip'
21$timestamp = Get-Date -format yyyyMMddHHmmss
22$mysqldumpPath = "C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqldump.exe"
23$fileName = "$database-$timestamp"
24$backupPath = 'C:\HOME\MySQl-Backups\'
25$dumpFileFullPath = "$backupPath$fileName$dumFileExtension"
26$backupFileFullPath = "$backupPath$fileName$archiveExtension"
27$filePath = Join-Path $backupPath $fileName
28
29# Email settings
30$enableEmailNotifications = $TRUE
31$emailOnSuccess = $TRUE # Set to $FALSE if you want to receive email notifications for failures only
32$smtpServer = '<SES SMTP SERVER>'
33$sesAccessKey = '<SES USER ACCESS KEY>'
34$sesSecretKey = '<SES USER SECRET KEY>'
35$senderEmailAddress = '<SENDER EMAIL ADDRESS>'
36$recipientEmailAddress = '<RECIPIENT EMAIL ADDRESS>'
37$subject = '[<APPLICATION NAME>][Backup]'
38
39
40function Write-Log ($message)
41{
42 $timestamp = get-date -format yyyyMMddHHmmss
43 Write-Host [$timestamp] $message
44}
45
46function Send-Email ($body)
47{
48 Send-MailMessage -from $senderEmailAddress -to $recipientEmailAddress -subject $subject -body $body -SmtpServer $smtpServer -UseSsl -Port 587 -Credential $(New-Object System.Management.Automation.PSCredential -argumentlist $sesAccessKey, $(ConvertTo-SecureString -AsPlainText -String $sesSecretKey -Force))
49}
50
51Try
52{
53 #01. Execute mysqldump and create the backup file
54 $command = "cmd.exe /C '$mysqldumpPath' -u $user -p$password --databases $database > $dumpFileFullPath"
55 Invoke-Expression -Command:$command
56
57 # 02. Create a zip file containing the backup to reduce the upload size
58 Compress-Archive -LiteralPath $dumpFileFullPath -CompressionLevel Optimal -DestinationPath $backupFileFullPath
59
60 # 03. Upload zip file to S3 bucket
61 Write-S3Object -BucketName $s3Bucket -File $backupFileFullPath -Key $fileName$archiveExtension -Region $region -AccessKey $accessKey -SecretKey $secretKey
62
63 # 04. Send notification email (if enabled for success)
64 if ($enableEmailNotifications -and $emailOnSuccess)
65 {
66 $body = "$fileName-'has been moved to AWS S3"
67 Send-Email $body
68 }
69
70 # 05. Clear temp files
71 Remove-Item $dumpFileFullPath
72 Remove-Item $backupFileFullPath
73
74 Write-Log "Backup has been uploaded successfully"
75}
76Catch
77{
78 $ErrorMessage = $_.Exception.Message
79 $FailedItem = $_.Exception.ItemName
80 $LogMessage = $ErrorMessage-$FailedItem
81 Write-Log $LogMessage
82
83 if ($enableEmailNotifications)
84 {
85 Send-Email $LogMessage
86 }
87
88 Break
89}