Powershell 之壓縮

   當管理的系統越來越多,協助開發通過日誌查找問題,相信是很多運維人員在日常工作中最頭疼的問題,反覆調試,來回調取日誌,在這種情況下,系統的日誌模式也必須調整爲info級別,隨之而來的也是日誌文件的快速增長,導致要定期進行清理。


   於是乎,我採用了日誌分離模式,將系統日誌統一存放在一臺Windows服務器上,因爲接下來我要將這些日誌文件定時進行壓縮,收縮服務器存儲空間。

function Auto-Zip{
    if(-not (Test-Path $args[0])){
        Write-Warning "請指定一個目錄"
        break
    }
    dir $args[0]|?{$_ -is [System.IO.DirectoryInfo]}|
    foreach{
        $LogPath=$_.FullName
        $nowDate=(Get-Date).ToString().Split(" ")[0]
        dir  $LogPath|?{$_ -is [System.IO.FileInfo] -and $_.name -match "2015" -and $_.name -notmatch ".zip$" -and $_.name -notmatch ".rar$"}|
        foreach{
            $file=$_.FullName
            $zipname=$file+".zip"
            while(Get-Process -Name winrar -ErrorAction SilentlyContinue){
                sleep -milliseconds 500 
            }
            Write-Host "開始壓縮! $file" -ForegroundColor Green
            winrar a $zipname $file -ibck -t -df
        }
    }
}

#調用,假如你的日誌存放在D:\Logs\nginx

Auto-Zip D:\Logs


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章