Snippets
Created by
SeanB
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | #$DebugPreference = "Continue"
import-module "C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1"
add-type -assemblyName "System.Web"
$AccessKeyId = (get-item env:aws-access-key-id -erroraction SilentlyContinue).value
$AccessKeySecret = (get-item env:aws-access-key-secret -erroraction SilentlyContinue).value
if ( !($AccessKeyId) -or !($AccessKeySecret)) {
$ok = ($AccessKeyId) -or ($AccessKeySecret)
Write-Host "AWS Access keys have not been created."
Write-Host "==================================================================="
Write-Host "To run this script you must create two environment variables called"
Write-Host "aws-access-key-id and aws-access-key-secret"
Write-Host "--"
break
}
# Regions: us-east-1, us-west-2, us-west-1, eu-west-1, ap-southeast-1
$region = "eu-west-1" # Regions: us-east-1, us-west-2, us-west-1, eu-west-1, ap-southeast-1
$blocksize = (1024*1024*5)
$startblocks = (1024*1024*16)
$FilesList = @()
$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
Function CreateShadowCopy($drive) {
Write-Debug("Create a shadow copy for $drive")
$s1 = (gwmi -list win32_shadowcopy).Create($drive,"ClientAccessible")
$s2 = $s1.ShadowID
Write-Debug("Created Shadow Copy $s2")
return $s2
}
function DeleteShadowCopy($shadow) {
$shadow_obj = (gwmi Win32_ShadowCopy) | ? { $_.ID -eq $shadow }
$newId = $shadow_obj.ID
Write-Debug("deleting $newID")
$shadow_obj.Delete()
}
function ShadowFolder($shadow) {
$shadow_obj = (gwmi Win32_ShadowCopy) | ? { $_.ID -eq $shadow }
$path = $shadow_obj.DeviceObject
return $path
}
function AmazonEtagHashForFile($filename) {
$lines = 0
[byte[]] $binHash = @()
$reader = [System.IO.File]::Open($filename,"OPEN","READ")
if ((Get-Item $filename).length -gt $startblocks) {
$buf = new-object byte[] $blocksize
while (($read_len = $reader.Read($buf,0,$buf.length)) -ne 0){
$lines += 1
$binHash += $md5.ComputeHash($buf,0,$read_len)
}
$binHash=$md5.ComputeHash( $binHash )
}
else {
$lines = 1
$binHash += $md5.ComputeHash($reader)
}
$reader.Close()
$hash = [System.BitConverter]::ToString( $binHash )
$hash = $hash.Replace("-","").ToLower()
if ($lines -gt 1) {
$hash = $hash + "-$lines"
}
return $hash
}
function FindInCachedFileList($tofile) {
Foreach ($item in $FilesList) {
if ($item.key -eq $tofile) {
return $item
}
}
}
function s3BackupFile($file, $folder, $BucketID) {
if (!$file.PSIsContainer) {
$fromFile = $file.fullname.ToLower()
$filename = $file.fullname.ToLower()
$filename = $filename.replace("c:\shadowcopy\" , $folder)
$filename = $filename.replace("\" , "/")
$filename = [System.Web.HttpUtility]::UrlEncode($filename)
$filename = $filename.replace("%2f" , "/")
$s3file = FindInCachedFileList($filename)
if ($s3file -eq $null) {
Write-Host($fromFile + " -> $filename (not found)" )
Write-S3Object -BucketName $BucketID -File $fromfile -Key $filename
return
}
$hash = AmazonEtagHashForFile($file.fullname)
$etag =$s3file.etag.Replace('"',"")
if ($etag -ne $hash) {
Write-Debug("found file. etag differs")
Write-Host("$fromFile ($hash) -> $filename ($etag)" )
Write-S3Object -BucketName $BucketID -File $fromfile -Key $filename
}
}
}
function s3_filelist($folder, $BucketID) {
$marker = ""
$maxkeys = 1000
$files = @()
do {
$s3files = (Get-S3Object -Marker $marker -MaxKeys $maxkeys -BucketName $BucketID -KeyPrefix $folder )
$s3files | Foreach-Object{
$s3Hash = @{"key" = $_.key; "etag" = $_.etag}
$files += $s3Hash
$marker = $_.key
}
} while ($s3files.length -eq $maxkeys)
return $files
}
function folder_backup($BucketID, $BackupTo, $BackupFrom, $BackupDrive) {
Write-Host "Starting the backup from $BackupFrom"
Write-Host "============================================="
Write-Host("Backing up to S3")
if (Test-Path "c:\shadowcopy") {
Write-Host("Currently running a backup. If we aren't then remove folder c:\shadowcopy")
break
}
Set-AWSCredentials -AccessKey $AccessKeyId -SecretKey $AccessKeySecret
Set-DefaultAWSRegion $region
$day = ([string](get-date).dayofweek).ToLower()
$snapshot_folder = $BackupTo + "/" + $day + "/"
Write-Host("Writing to folder $snapshot_folder")
Write-Debug $BucketID
$FilesList = (s3_filelist $snapshot_folder $BucketID)
Write-Host("Creating a shadow copy")
$shadow=CreateShadowCopy($BackupDrive)
$folder = ShadowFolder($shadow)
$folder = $folder + $BackupFrom + "\"
Write-Host("Reading from folder $folder")
Write-Debug("====================================================")
cmd /c mklink /d c:\shadowcopy "$folder"
Write-Host("Removing items on S3 that no longer exist...")
$FilesList | Foreach-Object{
if ($_.key) {
$LocalFile = $_.key.replace($snapshot_folder,"c:\shadowcopy\")
$LocalFile = $LocalFile.replace("/" , "\")
$LocalFile = [System.Web.HttpUtility]::UrlDecode($LocalFile)
if (!(Test-Path $LocalFile)) {
$DeleteAmazonFile = $_.key
Write-Host ("Deleting... $DeleteAmazonFile from $LocalFile")
$response = Remove-S3Object -BucketName $BucketID -Key $DeleteAmazonFile -Force
}
}
}
$files = Get-ChildItem "C:\shadowcopy" -Recurse
$files | Foreach-Object{
s3BackupFile $_ $snapshot_folder $BucketID
}
Write-Host("Removing shadow copy")
cmd /c rmdir c:\shadowcopy
DeleteShadowCopy($shadow)
Write-Host("Finished")
}
folder_backup "Selectcs-Common" "REPORTS" "\VSMS8\REPORTS" "D:\"
folder_backup "Selectcs-Common" "COMMONFILES" "\VSMS8\COMMONFILES" "D:\"
folder_backup "Selectcs-Common" "PROGS" "\VSMS8\SMS7\PROGS" "D:\"
folder_backup "Selectcs-Common" "BITMAPS" "\VSMS8\BITMAPS" "D:\"
folder_backup "Selectcs-Common" "PROGRAMS" "\VSMS8\PROGRAMS" "D:\"
folder_backup "Selectcs-Common" "SCRIPTS" "\SCRIPTS" "D:\"
#folder_backup "selectcs-backup" "sean-scripts-2" "\scripts" "e:\"
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.