VanillaGranilla

uploadToS3.ps1

May 23rd, 2016
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #requires -version 4
  2. <#
  3. .SYNOPSIS
  4.   Copies files from a folder recursively to Amazon S3
  5.  
  6. .DESCRIPTION
  7.   Copies files from a folder recursively to Amazon S3.  Note: this script uses the 'archive' attribute to determine if a file has been backed up previously.
  8.  
  9. .INPUTS
  10.   None
  11.  
  12. .OUTPUTS
  13.   None
  14.  
  15. .NOTES
  16.   Version:        1.0
  17.   Author:         Jim Gronowski
  18.   Creation Date:  20160520
  19.   Purpose/Change: Initial script
  20.  
  21. .EXAMPLE
  22.   None
  23. #>
  24.  
  25. #---------------------------------------------------------[Initialisations]--------------------------------------------------------
  26.  
  27. #Set Error Action to Silently Continue
  28. #$ErrorActionPreference = "SilentlyContinue"
  29.  
  30.  
  31. #----------------------------------------------------------[Declarations]----------------------------------------------------------
  32.  
  33. #Script Version
  34. $sScriptVersion = "1.0"
  35.  
  36. #Source Folder
  37. $sourceFolder = "Z:\BACKUP\"
  38.  
  39. #S3 Bucket Name
  40. $s3bucket = "BUCKET_NAME_HERE"
  41.  
  42. #Folder name (this will be the prefix in the S3 key - like a folder, but not)
  43. $folderName = "FOO"
  44.  
  45. #Array of files, only including SQL backup files.
  46. $files = Get-ChildItem $sourceFolder -Recurse -Include "*.bak", "*.trn"
  47.  
  48. #-----------------------------------------------------------[Execution]------------------------------------------------------------
  49.  
  50.  
  51.  
  52. foreach ($file in $files)
  53. {
  54.     if( $file.attributes -eq "Archive")
  55.     {
  56.         #Copy the file to S3
  57.         echo "uploading $($folderName)/$($file.name)"
  58.         Write-S3Object -BucketName $s3bucket -File "$($file.FullName)" -Key "$($folderName)/$($file.Name)" -ServerSideEncryption AES256 -StorageClass "STANDARD_IA"
  59.  
  60.  
  61.         #Flip the archive bit so file will not be uploaded again on next run
  62.         $file.Attributes = $file.Attributes -bxor ([System.IO.FileAttributes]::Archive)
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment