Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move IIS Logs to AWS s3 bucket

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

like image 878
DisplayName Avatar asked Oct 05 '18 11:10

DisplayName


1 Answers

UPDATE:

You could try and use the following method to create your folder structure within the 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
like image 167
Peter Kay Avatar answered Nov 04 '22 14:11

Peter Kay