(powershell) backup zip and delete log files/qlik sense script logs half-year backup program

前言

Qlik sense的script log是非常多的,需要定期清理,所以本程序用的是powershell进行编写,功能如下:

  • 复制半年前的日志(.AddDays(-180)可以修改具体的时间)到Temp文件夹
  • 用7Zip压缩日志文件夹并保存为.zip
  • 删除已经备份好的半年前日志文件

-Copy the log from half year ago (in .AddDays(-180) can modify the specific time) to the Temp folder
-Use 7Zip to compress the log folder and save it as .zip
-Delete the log files that has been backed up

Power Script

Please find the below code:请参考以下代码:

echo "  _      __  __    ___   "
echo " | |    |  \/  |  |   \  "
echo " | |__  | |\/| |  | |) | "
echo " |____| |_|  |_|  |___/  "
echo "-------------------------"
echo "ScriptLog HalfYear Backup"
echo "by zhengkai.blog.csdn.net"
echo "-------------------------"

#get the current time string , nowTime is for naming the file/folder ,nowTime2 for putputing to file as last backup time
[String]$nowTime = (Get-Date).ToString("yyyyMMddHHmmss")
[String]$lastBackupTime = (Get-Date).AddDays(-180).ToString("yyyy-MM-dd HH:mm:ss")
echo "TODAY=$($nowTime) HALFYEAR=$($lastBackupTime)"

#the 7z execution application,7z.exe for silent mode,7zG.exe for UI mode,can see the process bar
[String]$zip = "D:\Backups\app\7z.exe"

#the folder to incremental backup
[String]$sourcePath = "C:\ProgramData\Qlik\Sense\Log\Script\"

#the path to save backup compression file when incremental backup completed
[String]$destinationPath = "D:\Backups\ScriptLog\"

#define backup file name with the current time string
[String]$name = "$($destinationPath)\backup_$($nowTime).zip"

#temp folder that will save the files and then backup ,will be deleted at the end
[String]$temp = "$($destinationPath)\$($nowTime)"
#or using tmp folder "$($env:TMP)\$((Get-Date).ToString("yyyyMMddHHmmss"))"



if ($?)
{
    #create if not exists temp folder
	if (!(Test-Path -Path $temp))
	{
        New-Item -ItemType Directory -Path $temp
	}	
    #created if not exists destination folder
	if (!(Test-Path -Path $destinationPath))
	{
		New-Item -ItemType Directory -Path $destinationPath
	}
    #foreach the files and filter the items that the last write time equal than lastBackupTime
    Get-ChildItem -Path $sourcePath -Recurse -ErrorAction SilentlyContinue -include *.log |  Where-Object -FilterScript {($_.LastWriteTime -lt $lastBackupTime)} | Copy-Item -Destination $temp -Force 
    ECHO "Backup Done......"
    Get-ChildItem -Path $sourcePath -Recurse -ErrorAction SilentlyContinue -include *.log |  Where-Object -FilterScript {($_.LastWriteTime -lt $lastBackupTime)} | Remove-item
    ECHO "Deletion Done......"
}

#start to zip temp folder and then save to destination Path
& $zip a $name $temp
ECHO "Compression Done......"

#stop 5 seconds and then delete the temp folder
Start-Sleep -s 5
Remove-Item $temp -Recurse
ECHO "Remove Temp Done......"

echo "-------------------------"
echo "Backup SUCCESS!"

Screencap

Run in powershell ISE or as batch :
在这里插入图片描述
backup file:
在这里插入图片描述
Check the source folder, the logs file longer than half year were deleted , just keep in backup compression file.

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 博客之星2020 设计师:CY__ 返回首页