incremental backup script(PowerShell+7z增量备份批处理脚本)

前言

前段时间得到一个任务,做个增量备份批处理脚本。语言PowerShell,压缩软件7z,所以就有了这个incremental backup script | 增量备份的solution。

步骤:

  • 1.从lastBackup.txt文件中获取最后一次保存时间
  • 2.通过增量复制(判断LastWriteTime>上次修改时间)源文件夹sourcePath 中需要备份的文件到temp临时目录
  • 3.7z压缩temp目录到目标文件夹destinationPath
  • 4.回写最新的备份时间到lastBackup.txt文件

解决方案

  1. powershell script脚本
echo "#####################################################"
echo "  _      __  __    ___   "
echo " | |    |  \/  |  |   \  "
echo " | |__  | |\/| |  | |) | - Incremental Backup 增量备份"
echo " |____| |_|  |_|  |___/  - by zhengkai.blog.csdn.net"
echo "#####################################################"

#load the last backup time from file and then print to console
#增量备份时间点,从文件读取并输出
[String]$lastBackupTimeTxt = "C:\Users\Administrator\Desktop\incremental_backup\lastBackup.txt"
[String]$lastBackupTime = Get-Content -Path $lastBackupTimeTxt
echo $lastBackupTime

#获取现在的时间,nowTime用于临时文件夹/zip文件命名,nowTime2用于保存到备份时间点文件
#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]$nowTime2 = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")

#调用7-zip进行压缩,如果想看到进度条用7zG.exe,静默模式用7z.exe
#the 7z execution application,7z.exe for silent mode,7zG.exe for UI mode,can see the process bar
[String]$zip = "C:\Program Files\7-Zip\7zG.exe"

#the folder to incremental backup
#需要增量备份的源文件夹
[String]$sourcePath = "C:\Users\Administrator\Desktop\incremental_backup\log"

#the path to save backup file when incremental backup completed
#备份后压缩文件存放的路径
[String]$destinationPath = "C:\Users\Administrator\Desktop\incremental_backup\backup"

#backup file name,with the current time string
#备份后的文件名,带有当前时间的字符串
[String]$name = "$($destinationPath)\backup_$($nowTime).7z"

#临时文件夹,先把需要备份的文件copy出来再进行备份,最后删除
#the 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 |  Where-Object -FilterScript {($_.LastWriteTime -gt $lastBackupTime)} | Copy-Item -Destination $temp -Force
	
}

#开始备份临时文件夹中需要增量备份的文件
#start to zip temp folder and then save to destination Path
& $zip a $name $temp

#save the lastBackupTime
$nowTime2 | Out-File $lastBackupTimeTxt

#休息5秒再删除临时文静啊
#stop 5 seconds and then delete the temp folder
Start-Sleep -s 5
Remove-Item $temp -Recurse
echo "Incremental Backup SUCCESS!"
  1. 第一次使用需要新增一个文件lastBackup.txt文件,把需要备份的时间点开始写进入,例如你要备份所有的,写个2000-01-01 00:00:00 就可以了,或者写个近一点的时间2020-05-15 00:00:00

  2. 如果你要做绿色版,请顺便打包埋7z的几个核心文件

  • 7z.dll 核心dll
  • 7z.exe 静默备份命令行
  • 7zG.exe 界面备份命令行,会显示备份进度
  • 7zFM.exe 管理器,可以打开来管理
    在这里插入图片描述
    4.文件夹如下
  • backup存放备份文件
  • log为需要备份的文件(通常log再其他地方,这里只是最为演示)
  • 7z部分
  • 脚本部分batps1文件
  • lastBackup.txt存放最后备份时间,用于增量
    在这里插入图片描述

运行效果

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/moshowgame/article/details/106147781
今日推荐