ps/Modules/Alkami.PowerShell.Common/Public/Backup-ORBLogFiles.ps1

95 lines
3.5 KiB
PowerShell
Raw Permalink Normal View History

2023-05-30 22:51:22 -07:00
function Backup-ORBLogFiles {
<#
.SYNOPSIS
Saves old ORB Logs in an Archive subfolder
.PARAMETER logDirectory
Directory of orb logs
.PARAMETER skipActiveLogs
Should we skip the logs of running services?
.EXAMPLE
Backup-OrbLogFiles
.EXAMPLE
Backup-OrbLogFiles -logDirectory Get-OrbLogsPath -skipActiveLogs
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $false)]
[Alias("LogPath")]
[string]$logDirectory,
[Parameter(Mandatory = $false)]
[Alias("SkipActive")]
[switch]$skipActiveLogs
)
$logLead = Get-LogLeadName
if ([string]::IsNullOrEmpty($logDirectory)) {
$logDirectory = Get-OrbLogsPath
}
$datetime = Get-Date -Format "MM_dd_yy_HHmmss"
$archivePath = Join-Path $logDirectory "Archive"
$7zipPath = Get-7ZipPath
if (!(Test-Path $archivePath)) {
New-Item -Path $archivePath -ItemType Directory | Out-Null
}
$filter = if ($skipActiveLogs) { "*.log.*" } else { "*.log*" }
# Remove any potential trailing wildcarded directories
while ($logDirectory.Substring($logDirectory.get_length() - 2) -eq "\*" ) {
Write-Host "$logLead : Found a wildcard. Trimming..."
$logDirectory = $logDirectory.Substring(0, $logDirectory.get_length() - 2)
}
# Tack on a single \* so that Get-ChildItem plays nicely with both the Include and Exclude parameters
$logDirectory = $logDirectory + "\*"
$logFiles = @()
if (Test-Path -Path $logDirectory -Include $filter -Exclude "*.lnk") {
$logFiles += [array](Get-ChildItem -Path $logDirectory -Include $filter -Exclude "*.lnk" -File) -notmatch "\d{12}"
}
# Slog files get saved with the date stamp in the filename
# For example: Alkami.Services.BillPayOrchestration-20210901.slog
# Super annoying
# Slog files also have file locks unless they've been written to, even if the date has rolled
# This should handle most cases
if ($skipActiveLogs) {
$slogDateFormat = (Get-Date).ToString("yyyyMMdd")
if (Test-Path -Path $logDirectory -Include "*.slog*" -Exclude "*$slogDateFormat.slog*") {
$logFiles += [array](Get-ChildItem -Path $logDirectory -Include "*.slog*" -Exclude "*$slogDateFormat.slog*" -File) -notmatch "\.(gz|7z|zip)"
}
} else {
if (Test-Path -Path $logDirectory -Include "*.slog*" ) {
$logFiles += [array](Get-ChildItem -Path $logDirectory -Include "*.slog*" -File) -notmatch "\.(gz|7z|zip)"
}
}
$uniqueLogFiles = ($logFiles.Basename -replace "(.log|-\d{8})" , "") | Sort-Object | Get-Unique
Write-Host "$logLead : Backing up $archivePath files to $filter"
foreach ($uniqueLogFile in $uniqueLogFiles) {
$logs = $logFiles | Where-Object { ($_.BaseName -replace "(.log|-\d{8})" , "") -eq $uniqueLogFile }
if ($logs) {
Write-Host "$logLead : Compressing Files"
Write-Host $logs.FullName
$zipname = "$($archivePath)\$($uniqueLogFile)\$($env:COMPUTERNAME)_$($datetime).zip"
Write-Host "$logLead : Archive Files | $($zipname)"
try {
if ($null -ne $logs.FullName) {
Write-Verbose "$logLead : Invoking 7zip with: a -tzip -sdel -y $zipname $($logs.FullName)"
& $7zipPath a -tzip -sdel -y $zipname $logs.FullName
}
} catch {
Write-Warning $_.Exception
}
}
}
Write-Host ("$logLead : Backup Complete")
}