Skip to content

Instantly share code, notes, and snippets.

@rterbush
Last active January 12, 2016 00:34
Show Gist options
  • Save rterbush/b08c2eb429162d40d77f to your computer and use it in GitHub Desktop.
Save rterbush/b08c2eb429162d40d77f to your computer and use it in GitHub Desktop.
Param(
[Parameter(Mandatory=$True)]
[string]$Bucket,
[Parameter(Mandatory=$True)]
[string]$Prefix,
[Parameter(Mandatory=$True)]
[string]$ARN
)
$CompName = $env:ComputerName
If (Test-Path "C:\Backups\$CompName") {
$Dest = "C:\Backups\$CompName"
}
cd $Dest
# Get all the files to compress
$fileList = Get-ChildItem -Path $Dest -Filter *.bak -Recurse
foreach ($file in $fileList) {
$zipFile = $file.DirectoryName + '\' + $file.Basename + '.zip'
if (-not (test-path($zipFile))) {
set-content $zipFile ("PK" + [char]5 +[char]6 + ("$([char]0)" * 18))
(dir $zipFile).IsReadOnly = $false
}
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipFile)
$zipPackage.CopyHere($file.FullName)
do
{
Start-sleep -milliseconds 250
}
while ($zipPackage.Items().count -eq 0)
# Remove-Item $file.Fullname
}
# Get all the files to upload
$fileList = Get-ChildItem -Path $Dest -Filter *.zip -Recurse
$maxSize = 0
# Copy each file to bucket
foreach ($file in $fileList) {
$maxSize = ($maxSize, $file.Length | Measure -Max).Maximum
$localfile = $file.FullName
$filename = $file.Name
$key = $Prefix + '/' + $filename
$remoteObject = Get-S3Object -BucketName $Bucket -Key $key -Region us-west-2
if ($remoteObject.Size -eq $null) {
Write-S3Object -File $localfile -BucketName $Bucket -Key $key -ServerSideEncryption AES256
}
else {
write-host "$file exists"
}
Remove-Item $file.FullName
}
$backupDrive = $Dest.Substring(0,2)
$freeSpace = (Get-WmiObject Win32_LogicalDisk | ? { $_.DeviceId -like $Dest.Substring(0,2)}).FreeSpace
if ($freeSpace -lt 2*$maxSize) {
$message = "Free space less than 2* largest backup file on " + $env:ComputerName
$subject = "Backup drive space: " + $env:ComputerName
Publish-SNSMessage -TargetArn $ARN -Message $message -Subject $subject
}
@rterbush
Copy link
Author

rterbush commented Jan 7, 2016

user_db_backup_full:
  cmd.run:
    - name: |
        sqlcmd -E -S localhost -d master -Q "EXECUTE dbo.DatabaseBackup @Databases = 'USER_DATABASES', @Directory = 'C:\Backups', @BackupType = 'FULL', @Verify = 'Y', @CheckSum = 'Y'" -b
    - shell: cmd
    - stateful: False

user_db_archive_full:
  cmd.script:
    - source: salt://db/util/s3archive.ps1
    - args: "-Bucket mybucket -Prefix /put/backups/full -ARN 'arn:sns:::' "
    - shell: powershell
    - stateful: false
    - require:
      - cmd: user_db_backup_full

@rterbush
Copy link
Author

InvalidOperationExceptio
    + FullyQualifiedErrorId : Amazon.S3.AmazonS3Exception,Amazon.PowerShell.Cmdlets.S3.GetS3ObjectCmdlet
Write-S3Object : Access Denied
At C:\\windows\\temp\\tmphhfgwg.ps1:56 char:9
+         Write-S3Object -File $localfile -BucketName $Bucket -Key $key 
-ServerSid ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidOperation: (Amazon.PowerShe...eS3ObjectCmdlet:WriteS3ObjectCmdlet) [Write-S3Object], InvalidOperationException
    • FullyQualifiedErrorId : Amazon.S3.AmazonS3Exception,Amazon.PowerShell.Cmdlets.S3.WriteS3ObjectCmdlet
      Get-S3Object : Access Denied
      At C:\windows\temp\tmphhfgwg.ps1:54 char:21
    • $remoteObject = Get-S3Object -BucketName $Bucket -Key $key -Region us-west-2
      
    + CategoryInfo          : InvalidOperation: (Amazon.PowerShe...tS3ObjectCmdlet:GetS3ObjectCmdlet) [Get-S3Object], InvalidOperationException
    + FullyQualifiedErrorId : Amazon.S3.AmazonS3Exception,Amazon.PowerShell.Cmdlets.S3.GetS3ObjectCmdlet
Write-S3Object : Access Denied
At C:\\windows\\temp\\tmphhfgwg.ps1:56 char:9
+         Write-S3Object -File $localfile -BucketName $Bucket -Key $key 
-ServerSid ...\r\n+ ~

@rterbush
Copy link
Author

              stderr:
                  Param : The term 'Param' is not recognized as the name of a cmdlet, function, script file, or operable
 program. Check
                  the spelling of the name, or if a path was included, verify that the path is correct and try again.
                  At C:\Users\Administrator\appdata\local\temp\2\tmpbwosxv.ps1:3 char:1
                  + Param(
                  + ~~~~~
                      + CategoryInfo          : ObjectNotFound: (Param:String) [], CommandNotFoundException
                      + FullyQualifiedErrorId : CommandNotFoundException�

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment