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


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