Wednesday, October 17, 2018

Move IIS Logs to AWS s3 bucket

Leave a Comment

I have a requirement to upload IIS logs 7 days older to AWS S3 Bukcet. By using below code I am able to access AWS folders under bucket

Import-Module "C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1" $AKey = "" $SKey = "" $source = "C:\inetpub\logs\LogFiles\*" $outputpath = "C:\scripts\Logs\logs3.txt" Set-AWSCredentials -AccessKey $AKey -SecretKey $SKey  function Get-Subdirectories {     param  (         [string] $BucketName,         [string] $KeyPrefix,         [bool] $Recurse     )      @(Get-S3Object -BucketName $BucketName -KeyPrefix $KeyPrefix -Delimiter '/') | Out-Null      if ($AWSHistory.LastCommand.Responses.Last.CommonPrefixes.Count -eq 0) {         return     }      $AWSHistory.LastCommand.Responses.Last.CommonPrefixes      if ($Recurse) {         $AWSHistory.LastCommand.Responses.Last.CommonPrefixes | ForEach-Object { Get-Subdirectories -BucketName $BucketName -KeyPrefix $_ -Recurse $Recurse }     } }  function Get-S3Directories {     param  (         [string] $BucketName,         [bool] $Recurse = $false     )     Get-Subdirectories -BucketName $BucketName -KeyPrefix '/' -Recurse $Recurse  } 

Now if I type Get-S3Directories -BucketName backups I get the following output:

SERVER-xxx-OLogs/ SERVER-xxx-untime-logs / SERVER-xxx-FRLogs/ SERVER-oooooRLogs/ SERVER-IISLogFiles/ 

Now the challenge is I have to move IIS older than 7 days under SERVER-IISLogFiles/ Directory

So I have created this

$sfolder = Get-S3Directories -BucketName Backups Foreach ($folder in $sfolder) {     $wc = New-Object System.Net.WebClient      Set-AWSCredentials -AccessKey $AKey -SecretKey $SKey -StoreAs For_Move     Initialize-AWSDefaults -ProfileName For_Move -Region US-east-1      Start-Transcript -path $outputpath -Force     foreach ($i in Get-ChildItem $source -include *.log -recurse) {         if ($i.CreationTime -lt ($(Get-Date).AddDays(-7))) {             $fileName = (Get-ChildItem $i).Name             $parentFolderName = Split-Path (Split-Path $i -Parent) -Leaf             Write-S3Object -BucketName $folder -File $i         }     } } Stop-Transcript 

I am not entirely sure whether its going to move to SERVER-IISLogFiles/ Directory, not sure if I am missing anything here apart from this, I doubt its going to keep the folder structure of local IIS folder on IIS

1 Answers

Answers 1

UPDATE:

You could try and use the following method to create your folder structure within you S3 Bucket. Using the Key Prefix parameter and splitting your path to your folders should work.

$Params = @{     BucketName = 'backup'     Folder = '$Source or whatever path'     KeyPrefix = (Split-Path -Path 'C:\PATHTO\SOURCE\DIRECTORY' -Leaf).TrimEnd('\')     Recurse = $true     Region = 'REGION' } Write-S3Object @Params 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment