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存放最後備份時間,用於增量
    在這裏插入圖片描述

運行效果

在這裏插入圖片描述
在這裏插入圖片描述

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