Skip to content

Instantly share code, notes, and snippets.

@colehocking
Created July 29, 2020 21:51
Show Gist options
  • Save colehocking/bea718554130bbe0de2d926562353f5f to your computer and use it in GitHub Desktop.
Save colehocking/bea718554130bbe0de2d926562353f5f to your computer and use it in GitHub Desktop.
Upload LogRhythm Archives to S3
$inactive_archives_path = 'D:\LogRhythmArchives\Inactive'
$s3_bucket = 's3-bucket-name-goes-here'
$TTL = 3
Set-AWSCredentials -StoredCredentials stored-cred-name
Set-DefaultAWSRegion us-east-1
# Get all archive directories
$inactive_archives = Get-ChildItem -Path $inactive_archives_path
# Get oldest date to keep
$ttl_date = (Get-Date).AddDays(-$TTL)
# Loop through all archive directories
foreach ($archive in $inactive_archives) {
# Get folder path
$archive_folder = $archive.FullName
# Get folder name and S3 KeyPrefix
$archive_name = $archive_folder.split("\")[-1]
$s3_path = "/"+$archive_name
# Parse the date from the folder name
$archive_date = [datetime]::ParseExact($archive_name.Substring(0,8), "yyyyMMdd", $null)
# If the date from folder name is older than TTL
if ($archive_date -lt $ttl_date) {
# Upload to S3
Write-S3Object -BucketName $s3_bucket -Folder $archive_folder -KeyPrefix $s3_path
Write-Host "Uploaded: $archive_folder" -ForegroundColor Green
# Loop through files within archive folder
$files = Get-ChildItem $archive_folder
foreach ($file in $files) {
# Check to make sure individual archive file was uploaded
$s3_filename = $s3_path+'/'+$file.Name
$s3_object = Get-S3Object -BucketName $s3_bucket -Key $s3_filename
if ([bool]$s3_object) {
# Remove local archive file if it was successfully uploaded
Remove-Item $file.FullName -Force
Write-Host "Deleting: $($file.FullName)" -ForegroundColor White
}
# Write error to host if not able to locate file
else {
Write-Host "Error: $s3_filename not detected" -ForegroundColor Red
}
}
# Check if the individual local archive directory is empty
if((Get-ChildItem $archive_folder -force | Select-Object -First 1 | Measure-Object).Count -eq 0) {
# If empty, remove folder
Remove-Item $archive_folder -Force
Write-Host "Empty: Deleting $archive_folder" -ForegroundColor Green
}
else {
# Write error to host if folder is not empty
Write-Host "Error: $archive_folder is not empty" -ForegroundColor Red
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment