Powershell 之压缩

   当管理的系统愈来愈多,协助开发经过日志查找问题,相信是不少运维人员在平常工做中最头疼的问题,反复调试,来回调取日志,在这种状况下,系统的日志模式也必须调整为info级别,随之而来的也是日志文件的快速增加,致使要按期进行清理。nginx


   因而乎,我采用了日志分离模式,将系统日志统一存放在一台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
相关文章
相关标签/搜索