用Powershell備份自己的電腦的資料

對於IT系統,備份是很重要滴!

當然對於IT人,IT人的個人電腦備份也是用重要的。

其實我最重要的是一顆256G的隨身碟,沒有它我就不知道怎麼上班了。

我這顆隨身碟裡面的部分重要資料是必須定時複寫其他兩顆隨身碟和一顆外接硬碟。

所以整個備份流程,我個人用三顆隨身碟+一顆外接硬碟來做備份。

說明一下程式的大概流程:

首先所有的備份動作都盡可能地會寫入Log按照月份分開

同時也會寫入本機的Windows Log裡面。

如果是用自帶的Function Logwrite,那就是寫入本機的文字檔。

如果是用Write-EventLog,那就是寫入系統的EventLog裡面。

所以如果扣掉寫Log,其實程式碼不多,這只是當時我想試驗一下這些功能而已。

接下來的程式碼會把寫Log部份抽離,這樣版面比較乾淨。

[cc lang=”powershell”]

[String]$Logfile = “D:\BatchFile\Backuplog$yymm.txt” #LogFile
[String]$loginfo = “[INFO]”
[String]$logwarn = “[WARN]”
[String]$logerr = “[ERR ]”

Function LogWrite
{
Param([string]$logstring)
Add-content $Logfile “$(Get-date -Format ‘yyyy-MM-dd tthh:mm:ss’) $logstring”
}

[/cc]

本機電腦因為有C / D,所以偵測隨身碟的動作,只有到E/F/G/H/I/J/Z這些磁碟機代號跟目錄去搜尋一個特定文字檔是否存在,透過Foreach我很方便只要在這裡增加我想要的磁碟機代號即可。

這是利用迴圈下去找的,看起來繁瑣,但是執行效率還不錯,至少我不需要去管說到底現在哪顆隨身碟是在哪一個磁碟機代號。

[cc lang=”powershell”]

$DetectDrvs = “E:”,”F:”,”G:”,”H:”,”I:”,”J:”,”Z:”
Foreach ($DetectDrv1 in $DetectDrvs)
{
[Bool]$Chkdrv1 = Test-Path “$DetectDrv1\Neo1.txt”
IF ($Chkdrv1 -eq $True)
{
$Drv1 = $DetectDrv1
Break
}
ELSE
{
$Drv1=”NA”
}
}
Foreach ($DetectDrv2 in $DetectDrvs)
{
[Bool]$Chkdrv2 = Test-Path “$DetectDrv2\Neo2.txt”
IF ($Chkdrv2 -eq $True)
{
$Drv2 = $DetectDrv2
Break
}
ELSE
{
$Drv2=”NA”
}
}
Foreach ($DetectDrv3 in $DetectDrvs)
{
[Bool]$Chkdrv3 = Test-Path “$DetectDrv3\Neo3.txt”
IF ($Chkdrv3 -eq $True)
{
$Drv3 = $DetectDrv3
Break
}
ELSE
{
$Drv3=”NA”
}
}
Foreach ($DetectDrv4 in $DetectDrvs)
{
[Bool]$Chkdrv4 = Test-Path “$DetectDrv4\Neo2T.txt”
IF ($Chkdrv4 -eq $True)
{
$Drv4 = $DetectDrv4
Break
}
ELSE
{
$Drv4=”NA”
}

[/cc]

接下來是一海票的變數定義

[cc lang=”powershell”]

#————————-code-by-Neo———————————–
[int]$Expireday = 10 #想要之過期天數-1
[String]$Now = (Get-Date).ToString(“yyyyMMdd_HHmm_ss”) #撮取日期時間做檔名
[String]$yymm = (Get-Date).ToString(“yyyyMM”) #撮取年月做Log檔名
[String]$Source = “$Drv1\BatchFile” #備份來源
[String]$ZipDestination = “$Drv1\BatchBackup” #備份目錄
[String]$ZipDestination2 = “$Drv2\BatchBackup” #備份目錄
[String]$ZipDestination3 = “$Drv3\BatchBackup” #備份目錄
[String]$ZipDestination4 = “$Drv4\BatchBackup” #備份目錄
[String]$tempDestination = “$Drv1\BatchBackup\$Now” #Batch備份暫存工作目錄
[String]$Logfile = “D:\BatchFile\Backuplog$yymm.txt” #LogFile
[String]$loginfo = “[INFO]”
[String]$logwarn = “[WARN]”
[String]$logerr = “[ERR ]”
#=====================Personal====================================================
[String]$PFTemp = “D:\BackupTemp\$Now” #個人資料備份暫存目錄
[String]$PFEDrv = “$Drv3\PFBackup” #Neo8GPFBackup
[String]$PFDesktop = “\Desktop” #個人桌面檔案
[String]$PFDocuments = “\Documents” #個人文件
[String]$PFDownload = “\Downloads” #個人下載
[String]$PFFavorites = “\Favorites” #我的最愛
[String]$PFPictures = “\Pictures” #個人圖片
[String]$FLXXX = “D:\XXX”
[String]$FLDocuments = “D:\Documents”
[Array]$PFData = $PFDesktop,$PFDocuments,$PFDownload,$PFFavorites,$PFPictures
[String]$ZipPFDestination = “$Drv4\PFBackup”
[Bool]$Checklogfile = Test-Path $Logfile
[Bool]$ChecktempExist = Test-Path $tempDestination #檢查Batch暫存目錄使否存在
[Bool]$CheckPFTempExist = Test-Path $PFTemp #檢查個人暫存目錄使否存在
[Bool]$CheckZipExist = Test-Path $ZipDestination\$Now.zip #檢查備份檔案是否存在
[Bool]$CheckFirstUSB = Test-Path $Drv1\PFBackup\ #檢查第一隨身碟是否存在
[Bool]$CheckSecondUSB = Test-Path $Drv2 #檢查第二隨身碟是否存在
[Bool]$CheckThirdUSB = Test-Path $Drv3 #檢查第三隨身碟是否存在
$OldBatchs = Get-ChildItem $ZipDestination #過期Batch檔案陣列
$OldBatchs2 = Get-ChildItem $ZipDestination2 -ErrorAction SilentlyContinue
$OldBatchs3 = Get-ChildItem $ZipDestination3 -ErrorAction SilentlyContinue
$OldBatchs4 = Get-ChildItem $ZipDestination4 -ErrorAction SilentlyContinue
$OldPFFiles = Get-ChildItem $ZipPFDestination -ErrorAction SilentlyContinue #過期個人備份檔案
#=============EventLog===========================================================
[Int]$elsrtjb = 5001 #5001 -info Start Job
[Int]$elendjb = 5002 #5002 -info End Job
[Int]$elcrtfl = 5003 #5003 -info Create file
[Int]$elcpfl = 5011 #5011 -info Copy data
[Int]$elrmfl = 5012 #5012 -info Remove data
[Int]$elinfo = 5013 #5013 -info Inforamtion
[Int]$elflex = 5014 #5014 -error File Exist
[Int]$elzpfl = 5015 #5015 -info Zip Files
[int]$elchkY = 5016 #5016 -info Check File OK
[int]$elchkN = 5017 #5017 -Error Check File Not OK
[String]$elapp = “Application”
[String]$eltyinfo = “Information”
[String]$eltywarn = “Warning”
[String]$eltyerr = “Error”
[String]$elsr = “NeoAPPs”

[/cc]

第一段開始先檢查Log檔在不在,不在就建立,每月一號會自動建立一個新的文字檔

[cc lang=”powershell”]

IF($Checklogfile -eq $False)
{
New-Item -Path “D:\BatchBackup” -Name “Backuplog$yymm.txt” -ItemType “file”
}

[/cc]

備份當然要壓縮來節省空間,所以寫了下面的Function來方便後面呼叫。

[cc lang=”powershell”]

function ZipFiles( $zipfilename, $sourcedir )
{
Add-Type -Assembly System.IO.Compression.FileSystem
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir,
$zipfilename, $compressionLevel, $false)
}

[/cc]

第一顆隨身碟是最主要要同步備份的,所以如我第一顆隨身碟不存在,直接終止整個備份程序。因為我也不是常常更動那些檔案,所以我有更動檔案才會做一次同步。

會先複製要備份的指定目錄到本機硬碟暫存區然後做壓縮,檔案壓縮備份完,當然要把暫存檔清掉。

[cc lang=”powershell”]

If ($Drv1 -ne “NA”) #如果第一顆隨身碟存在
{
#開始備份到暫存工作目錄
Copy-Item -Path $Source -Destination $tempDestination -Recurse
#從暫存目錄壓縮至備份目的地
Compress-Archive -Path $tempDestination -DestinationPath $ZipDestination\$Now.zip
Remove-Item -Path $tempDestination -Force -Recurse
#刪除過期之檔案
foreach($oldBatch in $OldBatchs)
{
$Olderday = ((Get-Date) – $OldBatch.LastWriteTime).Days
if ($Olderday -gt $Expireday -and $OldBatch.PsISContainer -ne $True)
{
$OldBatch.Delete()
}

}
}
ELSE #如果第一顆隨身碟不存在,終止所有備份程序
{
LogWrite “$logerr First USB can not Detect, Backup treminate.”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elcrtfl -EntryType $eltyinfo -Message “First USB can not Detect, Backup treminate.”
Exit
}

[/cc]

 

 

第二顆和第三顆隨身碟動作雷同,只是同步指定目錄的檔案而已。

[cc lang=”powershell”]

If ($Drv2 -ne “NA”) #如果第二顆隨身碟存在,開始同步批次檔案到指定目錄
{
Copy-Item -Path $Source -Destination $Drv2 -Recurse -Force
#同步BatchFile檔案
Copy-Item -Path $Source -Destination $Drv2\BatchFile -Recurse -Force
#從第一隨身碟複製備份檔案至第二隨身碟備份目錄
Copy-Item -Path $ZipDestination\$Now.zip -Destination $Drv2\BatchBackup
#刪除過期之檔案
foreach($oldBatch in $OldBatchs)
{
$Olderday = ((Get-Date) – $OldBatch.LastWriteTime).Days
if ($Olderday -gt $Expireday -and $OldBatch.PsISContainer -ne $True)
{
$OldBatch.Delete()
}

}
}

If ($Drv3 -ne “NA”) #如果第三顆隨身碟存在,開始同步批次檔案到指定目錄
{
Copy-Item -Path $Source -Destination $Drv3 -Recurse -Force
#從第一隨身碟複製備份檔案至第三隨身碟備份目錄
Copy-Item -Path $ZipDestination\$Now.zip -Destination $Drv3\BatchBackup
}

[/cc]

 

 

這顆2T比較特別,同時備份我電腦裡面的文件。當然也還會壓縮。

[cc lang=”powershell”]

If ($Drv4 -ne “NA”) #如果第四顆隨身碟存在,開始同步批次檔案到指定目錄
{
Copy-Item -Path $Source -Destination $Drv4 -Recurse -Force
#從第一隨身碟複製備份檔案至第三隨身碟備份目錄
Copy-Item -Path $ZipDestination\$Now.zip -Destination $Drv4\BatchBackup
#個人檔案備份
If ($CheckPFTempExist -eq $True) #檢查暫存檔區是否存在,如果有先清除之
{
Remove-Item $PFTemp -Force -Recurse
}
Foreach ($PFItem in $PFData)
{
Copy-Item -Path $env:USERPROFILE$PFItem -Destination $PFTemp$PFItem -Recurse -ErrorAction SilentlyContinue
}
Copy-Item -Path $FLXXX -Destination $PFTemp\D_XXX-Recurse
Copy-Item -Path $FLDocuments -Destination $PFTemp\D_Documents -Recurse
Compress-Archive -Path $PFTemp\ -DestinationPath $ZipPFDestination\$Now.zip
Copy-Item -Path $ZipPFDestination\$Now.zip -Destination $Drv4\PFBackup
foreach($oldPFFile in $OldPFFiles)
{
$Olderday = ((Get-Date) – $OldBatch.LastWriteTime).Days
if ($Olderday -gt $Expireday -and $OldPFile.PsISContainer -ne $True)
{
$OldPFile.Delete()

}

}
Remove-Item $PFTemp -Force -Recurse

}

[/cc]

最後一段,備份檢查,把結果寫到Log裡面。

[cc lang=”powershell”]

LogWrite “$loginfo Backup Finish”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elendjb -EntryType $eltyinfo -Message “End Backup Data”
#備份檔案檢查
IF(Test-Path $ZipDestination\$Now.zip)
{
LogWrite “$loginfo $ZipDestination\$Now.zip found, Batchfile Zip and Backup Success”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elchkY -EntryType $eltyinfo -Message “$ZipDestination\$Now.zip found, Batchfile Zip and Backup Success”
}
Else
{
LogWrite “$logerr $ZipDestination\$Now.zip not found, Batchfile Zip and Backup Fail”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elchkN -EntryType $eltyerr -Message “$ZipDestination\$Now.zip not found, Batchfile Zip and Backup Fail”
}

IF(Test-Path $ZipPFDestination\$Now.zip)
{
LogWrite “$loginfo $ZipPFDestination\$Now.zip found, Profile Zip and Backup Success”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elchkY -EntryType $eltyinfo -Message “$ZipPFDestination\$Now.zip found, Profile Zip and Backup Success”
}
Else
{
LogWrite “$logerr $ZipPfDestination\$Now.zip not found, Profile Zip and Backup Fail”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elchkN -EntryType $eltyerr -Message “$ZipPfDestination\$Now.zip not found, Profile Zip and Backup Fail”
}

[/cc]

下面是整段程式碼

[cc lang=”powershell”]

#===Backup Powershell File===

#個人資料備分->複製到D:\BackupTemp

[String]$Logfile = “D:\BatchFile\Backuplog$yymm.txt” #LogFile
[String]$loginfo = “[INFO]”
[String]$logwarn = “[WARN]”
[String]$logerr = “[ERR ]”
$DetectDrvs = “E:”,”F:”,”G:”,”H:”,”I:”,”J:”,”Z:”
Foreach ($DetectDrv1 in $DetectDrvs)
{
[Bool]$Chkdrv1 = Test-Path “$DetectDrv1\Neo1.txt”
IF ($Chkdrv1 -eq $True)
{
$Drv1 = $DetectDrv1
Break
}
ELSE
{
$Drv1=”NA”
}
}
Foreach ($DetectDrv2 in $DetectDrvs)
{
[Bool]$Chkdrv2 = Test-Path “$DetectDrv2\Neo2.txt”
IF ($Chkdrv2 -eq $True)
{
$Drv2 = $DetectDrv2
Break
}
ELSE
{
$Drv2=”NA”
}
}
Foreach ($DetectDrv3 in $DetectDrvs)
{
[Bool]$Chkdrv3 = Test-Path “$DetectDrv3\Neo3.txt”
IF ($Chkdrv3 -eq $True)
{
$Drv3 = $DetectDrv3
Break
}
ELSE
{
$Drv3=”NA”
}
}
Foreach ($DetectDrv4 in $DetectDrvs)
{
[Bool]$Chkdrv4 = Test-Path “$DetectDrv4\Neo2T.txt”
IF ($Chkdrv4 -eq $True)
{
$Drv4 = $DetectDrv4
Break
}
ELSE
{
$Drv4=”NA”
}
}
Function LogWrite
{
Param([string]$logstring)
Add-content $Logfile “$(Get-date -Format ‘yyyy-MM-dd tthh:mm:ss’) $logstring”
}
function ZipFiles( $zipfilename, $sourcedir )
{
Add-Type -Assembly System.IO.Compression.FileSystem
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir,
$zipfilename, $compressionLevel, $false)
}

#————————-code-by-Neo———————————–
[int]$Expireday = 10 #想要之過期天數-1
[String]$Now = (Get-Date).ToString(“yyyyMMdd_HHmm_ss”) #撮取日期時間做檔名
[String]$yymm = (Get-Date).ToString(“yyyyMM”) #撮取年月做Log檔名
[String]$Source = “$Drv1\BatchFile” #備份來源
[String]$ZipDestination = “$Drv1\BatchBackup” #備份目錄
[String]$ZipDestination2 = “$Drv2\BatchBackup” #備份目錄
[String]$ZipDestination3 = “$Drv3\BatchBackup” #備份目錄
[String]$ZipDestination4 = “$Drv4\BatchBackup” #備份目錄
[String]$tempDestination = “$Drv1\BatchBackup\$Now” #Batch備份暫存工作目錄
[String]$Logfile = “D:\BatchFile\Backuplog$yymm.txt” #LogFile
[String]$loginfo = “[INFO]”
[String]$logwarn = “[WARN]”
[String]$logerr = “[ERR ]”
#=====================Personal====================================================
[String]$PFTemp = “D:\BackupTemp\$Now” #個人資料備份暫存目錄
[String]$PFEDrv = “$Drv3\PFBackup” #Neo8GPFBackup
[String]$PFDesktop = “\Desktop” #個人桌面檔案
[String]$PFDocuments = “\Documents” #個人文件
[String]$PFDownload = “\Downloads” #個人下載
[String]$PFFavorites = “\Favorites” #我的最愛
[String]$PFPictures = “\Pictures” #個人圖片
[String]$FLXXX = “D:\XXX”
[String]$FLDocuments = “D:\Documents”
[Array]$PFData = $PFDesktop,$PFDocuments,$PFDownload,$PFFavorites,$PFPictures
[String]$ZipPFDestination = “$Drv4\PFBackup”
[Bool]$Checklogfile = Test-Path $Logfile
[Bool]$ChecktempExist = Test-Path $tempDestination #檢查Batch暫存目錄使否存在
[Bool]$CheckPFTempExist = Test-Path $PFTemp #檢查個人暫存目錄使否存在
[Bool]$CheckZipExist = Test-Path $ZipDestination\$Now.zip #檢查備份檔案是否存在
[Bool]$CheckFirstUSB = Test-Path $Drv1\PFBackup\ #檢查第一隨身碟是否存在
[Bool]$CheckSecondUSB = Test-Path $Drv2 #檢查第二隨身碟是否存在
[Bool]$CheckThirdUSB = Test-Path $Drv3 #檢查第三隨身碟是否存在
$OldBatchs = Get-ChildItem $ZipDestination #過期Batch檔案陣列
$OldBatchs2 = Get-ChildItem $ZipDestination2 -ErrorAction SilentlyContinue
$OldBatchs3 = Get-ChildItem $ZipDestination3 -ErrorAction SilentlyContinue
$OldBatchs4 = Get-ChildItem $ZipDestination4 -ErrorAction SilentlyContinue
$OldPFFiles = Get-ChildItem $ZipPFDestination -ErrorAction SilentlyContinue #過期個人備份檔案
#=============EventLog===========================================================
[Int]$elsrtjb = 5001 #5001 -info Start Job
[Int]$elendjb = 5002 #5002 -info End Job
[Int]$elcrtfl = 5003 #5003 -info Create file
[Int]$elcpfl = 5011 #5011 -info Copy data
[Int]$elrmfl = 5012 #5012 -info Remove data
[Int]$elinfo = 5013 #5013 -info Inforamtion
[Int]$elflex = 5014 #5014 -error File Exist
[Int]$elzpfl = 5015 #5015 -info Zip Files
[int]$elchkY = 5016 #5016 -info Check File OK
[int]$elchkN = 5017 #5017 -Error Check File Not OK
[String]$elapp = “Application”
[String]$eltyinfo = “Information”
[String]$eltywarn = “Warning”
[String]$eltyerr = “Error”
[String]$elsr = “NeoAPPs”

IF($Checklogfile -eq $False)
{
New-Item -Path “D:\BatchBackup” -Name “Backuplog$yymm.txt” -ItemType “file”
LogWrite “$loginfo $yymm logfile Created”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elcrtfl -EntryType $eltyinfo -Message “LogFile : Backuplog$yymm.txt created in D:\BatchBackup”
}

LogWrite “===================S=T=A=R=T====B=A=C=K=U=P===================”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elsrtjb -EntryType $eltyinfo -Message “Start Backup Data.”

If ($Drv1 -ne “NA”) #如果第一顆隨身碟存在
{
LogWrite “$loginfo First USB Detect, Driver letter is $Drv1.”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elcrtfl -EntryType $eltyinfo -Message “First USB Detect, Driver letter is $Drv1.”
#開始備份到暫存工作目錄
Copy-Item -Path $Source -Destination $tempDestination -Recurse
LogWrite “$loginfo Copy $Source to $tempDestination”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elcpfl -EntryType $eltyinfo -Message “Copy $Source to $tempDestination”
#從暫存目錄壓縮至備份目的地
Compress-Archive -Path $tempDestination -DestinationPath $ZipDestination\$Now.zip
Logwrite “$loginfo Zip file from $tempDestination to $ZipDestination\$Now.zip.”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elzpfl -EntryType $eltyinfo -Message “Zip file from $tempDestination to $ZipDestination\$Now.zip.”
Remove-Item -Path $tempDestination -Force -Recurse
#刪除過期之檔案
foreach($oldBatch in $OldBatchs)
{
$Olderday = ((Get-Date) – $OldBatch.LastWriteTime).Days
if ($Olderday -gt $Expireday -and $OldBatch.PsISContainer -ne $True)
{
$OldBatch.Delete()
LogWrite $loginfo “Remove old file $OldBatch.”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elrmfl -EntryType $eltyinfo -Message “Remove old file $OldBatch.”
}

}
}
ELSE #如果第一顆隨身碟不存在,終止所有備份程序
{
LogWrite “$logerr First USB can not Detect, Backup treminate.”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elcrtfl -EntryType $eltyinfo -Message “First USB can not Detect, Backup treminate.”
Exit
}

If ($Drv2 -ne “NA”) #如果第二顆隨身碟存在,開始同步批次檔案到指定目錄
{
LogWrite “$loginfo Second USB Detect, Driver letter is $Drv2.”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elcrtfl -EntryType $eltyinfo -Message “Second USB Detect, Driver letter is $Drv2.”
Copy-Item -Path $Source -Destination $Drv2 -Recurse -Force
LogWrite “$loginfo $Drv2 avaible, Sync $Source Batchfile to $Drv2 Batchfile.”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elcpfl -EntryType $eltyinfo -Message “$Drv2 avaible, copy Batchfile to $Drv2 Batchfile.”
#同步BatchFile檔案
Copy-Item -Path $Source -Destination $Drv2\BatchFile -Recurse -Force
LogWrite “$loginfo Sync $Source to $Drv2\BatchFile.”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elcpfl -EntryType $eltyinfo -Message “$loginfo Sync $Source to $Drv2\BatchFile.”
#從第一隨身碟複製備份檔案至第二隨身碟備份目錄
Copy-Item -Path $ZipDestination\$Now.zip -Destination $Drv2\BatchBackup
LogWrite “$loginfo Copy $ZipDestination\$Now.zip to $Drv2\BatchBackup.”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elcpfl -EntryType $eltyinfo -Message “$loginfo Copy $ZipDestination\$Now.zip to $Drv2\BatchBackup.”
#刪除過期之檔案
foreach($oldBatch in $OldBatchs)
{
$Olderday = ((Get-Date) – $OldBatch.LastWriteTime).Days
if ($Olderday -gt $Expireday -and $OldBatch.PsISContainer -ne $True)
{
$OldBatch.Delete()
LogWrite $loginfo “Remove old file $OldBatch.”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elrmfl -EntryType $eltyinfo -Message “Remove old file $OldBatch.”
}

}
}

If ($Drv3 -ne “NA”) #如果第三顆隨身碟存在,開始同步批次檔案到指定目錄
{
LogWrite “$loginfo Third USB Detect, Driver letter is $Drv3.”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elcrtfl -EntryType $eltyinfo -Message “Third USB Detect, Driver letter is $Drv3.”
Copy-Item -Path $Source -Destination $Drv3 -Recurse -Force
LogWrite “$loginfo $Drv3 avaible, copy Batchfile to $Drv3 Batchfile.”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elcpfl -EntryType $eltyinfo -Message “$Drv3 avaible, copy Batchfile to $Drv3 Batchfile.”
#從第一隨身碟複製備份檔案至第三隨身碟備份目錄
Copy-Item -Path $ZipDestination\$Now.zip -Destination $Drv3\BatchBackup
LogWrite “$loginfo Copy $ZipDestination\$Now.zip to $Drv3\BatchBackup.”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elcpfl -EntryType $eltyinfo -Message “$loginfo Copy $ZipDestination\$Now.zip to $Drv3\BatchBackup.”
}

If ($Drv4 -ne “NA”) #如果第四顆隨身碟存在,開始同步批次檔案到指定目錄
{
LogWrite “$loginfo Third USB Detect, Driver letter is $Drv4.”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elcrtfl -EntryType $eltyinfo -Message “USB2TB Detect, Driver letter is $Drv4.”
Copy-Item -Path $Source -Destination $Drv4 -Recurse -Force
LogWrite “$loginfo $Drv4 avaible, copy Batchfile to $Drv4 Batchfile.”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elcpfl -EntryType $eltyinfo -Message “$Drv4 avaible, copy Batchfile to $Drv4 Batchfile.”
#從第一隨身碟複製備份檔案至第三隨身碟備份目錄
Copy-Item -Path $ZipDestination\$Now.zip -Destination $Drv4\BatchBackup
LogWrite “$loginfo Copy $ZipDestination\$Now.zip to $Drv4\BatchBackup.”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elcpfl -EntryType $eltyinfo -Message “$loginfo Copy $ZipDestination\$Now.zip to $Drv4\BatchBackup.”
#個人檔案備份
If ($CheckPFTempExist -eq $True) #檢查暫存檔區是否存在,如果有先清除之
{
Remove-Item $PFTemp -Force -Recurse
LogWrite “$logwarn Temp file exists. Remove them”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elcrmfl -EntryType $eltywarn -Message “$logwarn Temp file exists. Remove them.”
}
Foreach ($PFItem in $PFData)
{
Copy-Item -Path $env:USERPROFILE$PFItem -Destination $PFTemp$PFItem -Recurse -ErrorAction SilentlyContinue
Logwrite “$loginfo Copy $env:USERPROFILE$PFItem to $PFTemp$PFItem.”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elcpfl -EntryType $eltyinfo -Message “Copy $env:USERPROFILE$PFItem to $PFTemp$PFItem.”
}
Copy-Item -Path $FLXXX -Destination $PFTemp\D_XXX -Recurse
LogWrite “$loginfo Copy $FLXXX to $PFTemp\D_XXX.”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elcpfl -EntryType $eltyinfo -Message “Copy $FLXXX to $PFTemp\D_XXX.”

Copy-Item -Path $FLDocuments -Destination $PFTemp\D_Documents -Recurse
LogWrite “$loginfo Copy $FLDocuments to $PFTemp\D_Documents.”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elcpfl -EntryType $eltyinfo -Message “$FLDocuments to $PFTemp\D_Documents.”

Compress-Archive -Path $PFTemp\ -DestinationPath $ZipPFDestination\$Now.zip
Logwrite “$loginfo Compress file from $PFTemp\ to $ZipPFDestination\$Now.zip.”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elzpfl -EntryType $eltyinfo -Message “Compress file from $PFTemp\ to $ZipPFDestination\$Now.zip.”

Copy-Item -Path $ZipPFDestination\$Now.zip -Destination $Drv4\PFBackup
LogWrite “$loginfo Copy $ZipPFDestination\$Now.zip to $Drv4\PFBackup\”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elcpfl -EntryType $eltyinfo -Message “Copy $ZipPFDestination\$Now.zip to $Drv4\PFBackup\”

foreach($oldPFFile in $OldPFFiles)
{
$Olderday = ((Get-Date) – $OldBatch.LastWriteTime).Days
if ($Olderday -gt $Expireday -and $OldPFile.PsISContainer -ne $True)
{
$OldPFile.Delete()
LogWrite “$loginfo Remove old file $OldPFile.”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elrmfl -EntryType $eltyinfo -Message “Remove old file $OldPFile.”
}

}
Remove-Item $PFTemp -Force -Recurse
LogWrite “$loginfo Remove Temp file $PFTemp.”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elrmfl -EntryType $eltyinfo -Message “Remove Temp file $PFTemp.”

}

LogWrite “$loginfo Backup Finish”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elendjb -EntryType $eltyinfo -Message “End Backup Data”
#備份檔案檢查
IF(Test-Path $ZipDestination\$Now.zip)
{
LogWrite “$loginfo $ZipDestination\$Now.zip found, Batchfile Zip and Backup Success”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elchkY -EntryType $eltyinfo -Message “$ZipDestination\$Now.zip found, Batchfile Zip and Backup Success”
}
Else
{
LogWrite “$logerr $ZipDestination\$Now.zip not found, Batchfile Zip and Backup Fail”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elchkN -EntryType $eltyerr -Message “$ZipDestination\$Now.zip not found, Batchfile Zip and Backup Fail”
}

IF(Test-Path $ZipPFDestination\$Now.zip)
{
LogWrite “$loginfo $ZipPFDestination\$Now.zip found, Profile Zip and Backup Success”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elchkY -EntryType $eltyinfo -Message “$ZipPFDestination\$Now.zip found, Profile Zip and Backup Success”
}
Else
{
LogWrite “$logerr $ZipPfDestination\$Now.zip not found, Profile Zip and Backup Fail”
Write-EventLog -LogName $elapp -Source $elsr -EventId $elchkN -EntryType $eltyerr -Message “$ZipPfDestination\$Now.zip not found, Profile Zip and Backup Fail”
}

 

 

[/cc]

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *