-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTsBackUpDR2
More file actions
37 lines (26 loc) · 1.91 KB
/
TsBackUpDR2
File metadata and controls
37 lines (26 loc) · 1.91 KB
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
#Requires -Version 3
Set-StrictMode -Version Latest
if (Test-Path '<directory path>') {
Set-Location '<directory path>'
} else {
mkdir '<directory path>' ; Set-Location '<directory path>'
}
#Remove local logs that are older than 8 days from today
Get-ChildItem -Filter *.tsbak | where {$_.LastWriteTime -lt (Get-Date).AddDays(-8)} | Remove-Item
##Remove logs from AWS that are older than 8 days from today
foreach ($k in @(Get-S3Object -BucketName "<your AWS Bucket>" -Key "<your AWS key name>" -Region "<your AWS region>"|`
where {$_.LastModified -lt (Get-Date).AddDays(-8) -and $_.Size -gt 0}|select -Expand Key)){
Remove-S3Object -BucketName "<your AWS Bucket>" -Key $k -Region "<your AWS region>" -Force
}
## Logging for removals
Get-S3Object -BucketName "<your AWS Bucket>" -Key "<your AWS key name>" -Region "<your AWS region>"|where {$_.Size -gt 0}|select *, @{n='Date';e={(Get-Date)}}, @{n='State';e={'Remove'}}|`
Export-Csv -Path 'C:\<directory path>\AWSLogging.csv' -NoTypeInformation -Append
#Add tsbak to AWS, based on diff from local
$diff = Get-S3Object -BucketName "<your AWS Bucket>" -Key "<your AWS key name>" -Region "<your AWS region>"|where {$_.Size -gt 0}|select -expand key
$awss3 = $diff -replace "<your AWS key name>/"
$local = Get-ChildItem -Path C:\<directory path>\ -Filter "*.tsbak" | select -expand basename
foreach ($i in @(diff -ReferenceObject $local -DifferenceObject $awss3|where {$_.SideIndicator -like "<="}|select -expand inputobject)) {
Write-S3Object -BucketName "<your AWS Bucket>/<your AWS key name>" -Key $i -File "C:\<directory path>\$i.tsbak" -Region "<your AWS region>"
}
Get-S3Object -BucketName "<your AWS Bucket>" -Key "<your AWS key name>" -Region "<your AWS region>"|where {$_.Size -gt 0}|select *, @{n='Date';e={(Get-Date)}}, @{n='State';e={'Add'}}|`
Export-Csv -Path 'C:\<directory path>\AWSLogging.csv' -NoTypeInformation -Append