Function
Write-Log
{
Param
(
[string]
$logstring
)
$stamp
= (
Get-Date
).toString(
"yyyy/MM/dd HH:mm:ss"
)
$line
=
"$stamp $logstring"
Add-content
$logfile
-value
$line
}
Function
Send-Alert
{
Send-MailMessage
-SmtpServer
"Enter IP address"
-To
"Enter To email address"
-From
"Enter From Email address"
-Subject
"Company Monthly AWS S3 Copy Job"
-Body
"Company monthly backup copy job to AWS S3 has failed or completed with errors. Please alert the backup team immediately by opening a ticket. Copy job log: $logfile."
}
Function
Pause
{
Read-Host
'Press any key to continue…'
|
Out-Null
}
$AccessKey
=
"Enter AWS Access Key"
$SecretKey
=
"Enter AWS Secret Key"
$bucketname
=
"Enter S3 bucket name"
$root
=
"Enter the path of your monthly backup folder (e.g. D:\Monthly-Backups)"
$temproot
=
"Enter a path for the temporary vbk file (e.g. D:\Monthly-Backups\S3\Temp)"
$filter
=
"*.vbk"
$logfile
=
"Enter a path for the script log file (e.g. D:\Monthly-Backups\S3\Company-S3-Copy.log)"
$vbkfile
=
Get-ChildItem
-Path
$root
-Filter
$filter
|
Sort-Object
LastAccessTime
-Descending
|
Select-Object
-First
1
$vbkfilefullpath
=
join-path
-path
$root
-childpath
$vbkfile
$hash
= (
Get-FileHash
$vbkfilefullpath
-Algorithm
MD5).hash
Write-S3Object
-BucketName
$bucketname
-CannedACLName
bucket-owner-full-control
-File
$vbkfilefullpath
-Key
$vbkfile
-AccessKey
$AccessKey
-SecretKey
$SecretKey
$vbks3
= (
Get-S3Object
-BucketName
$bucketname
-Key
$vbkfile
-AccessKey
$AccessKey
-SecretKey
$SecretKey
).key
if
(
$vbks3
-ne
$vbkfile
)
{
Write-Log
"File upload to S3 was unsuccessful. Terminating script now!"
Send-Alert
Exit
}
else
{
Write-Log
"File upload to S3 was successful."
}
$temppath
=
"$temproot\$vbkfile"
Copy-S3Object
-BucketName
$bucketname
-Key
$vbkfile
-LocalFile
$temppath
-AccessKey
$AccessKey
-SecretKey
$SecretKey
$tempfile
= (
Get-ChildItem
-Path
$temppath
).Name
if
(
$tempfile
-ne
$vbkfile
)
{
Write-Log
"File download from S3 was unsuccessful. Terminating script now!"
Send-Alert
Exit
}
else
{
Write-Log
"File download from S3 was successful."
}
$hash2
= (
Get-FileHash
$temppath
-Algorithm
MD5).hash
if
(
$hash2
-ne
$hash
)
{
Write-Log
"Hash tags are not equal! Terminating script now!"
Send-Alert
Exit
}
else
{
Write-Log
"Hash=$hash Hash2=$hash2 Hash tags match!"
Remove-Item
–Path
$temppath
–recurse
$temppath2
= (
Get-ChildItem
-Path
$temproot
|
Measure-Object
).count
if
(
$temppath2
-eq
0)
{
Write-Log
"Removed Temp vbk file successfully."
}
else
{
Write-Log
"Couldn't remove Temp vbk file!"
Send-Alert
}
}