Last active
January 22, 2019 07:33
-
-
Save ezeeetm/5065126d3b5e7c5518fa to your computer and use it in GitHub Desktop.
backupToS3.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config | |
clear | |
$accessKey = "your AK" # these keys are for an IAM user with read/write access only to the -dr S3 account | |
$secretKey = "your SK" | |
$bucketName = "your bucketname" | |
$dataVol = "D:" # omit \ | |
$dataDirs = $null | |
function initializeAwsSession | |
{ | |
Initialize-AWSDefaults -AccessKey $accessKey -SecretKey $secretKey -Region us-east-1 | |
} | |
function getDataDirs ($dataDirpath) | |
{ | |
$dataDirs = Resolve-Path -Path $dataDirpath | |
return $dataDirs | |
} | |
function getLastModified ($item) | |
{ | |
$key = $item.FullName.replace("\","/") | |
try | |
{ | |
$itemMetaData = Get-S3ObjectMetadata -BucketName $bucketName -Key $key | |
} | |
catch | |
{ | |
return $null | |
} | |
return $itemMetaData.LastModified | |
} | |
function backup ($item) | |
{ | |
# write-host $item.FullName | |
$key = $item.FullName.replace("\","/") | |
try | |
{ | |
Write-S3Object -BucketName $bucketName -Key $key -File $($item.FullName) | |
} | |
catch | |
{ | |
return | |
} | |
} | |
################################# script entry point | |
initializeAwsSession | |
$dataDirpath = "$dataVol\*\*\data" | |
$dataDirs = getDataDirs $dataDirpath | |
get-date | |
foreach ($dataDir in $dataDirs) | |
{ | |
write-host "$(get-date)`t`tProcessing $dataDir" | |
$items = gci $dataDir -Recurse | |
foreach ($item in $items) | |
{ | |
if (Test-Path $($item.FullName) -pathtype container) {continue} # don't try to upload folders, they will get created automatically as key paths to items, and throw if you try to create them | |
$lastModified = getLastModified $item | |
if (($lastModified -eq $null) -or ($item.LastWriteTime -gt $lastModified)) # only backup new and changed files | |
{ | |
backup $item | |
} | |
} | |
# $cont = read-host "CTRL+C to exit, ENTER to continue" | |
} | |
get-date |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment