使用FSRM的Task 的自定義Action功能並利用Hardlink功能來備份數據

背景信息:shell


    1. 咱們的數據存放位置有三個,本地、同城、異地。備份數據會放本地,而後常規經過DFS同步到同城、異地兩個地方。因爲DFS的特性是雙向同步的,因此你若是在一個地方刪除、更新文件,那麼三個地方是會同步更新成一致的。windows

    2. 因爲數據量的大小的限制,我沒有辦法無限制的保存數據在服務器上,所以會按期把舊備份刪除。可是我異地的服務器的本地硬盤又比較大,我又想差別化的把歷史數據最大限度的在異地服務器上進行保存。服務器

解決方案:ide


    1. File Server Resource Manager 的File Managerment Task 功能能夠針對文件的建立日期、修改日期或者自定義分類屬性進行過濾,而且執行自定義action. 這裏的自定義action 咱們使用powershell 腳本。spa

    2. powershell 腳本針對過濾出的文件在另一個目錄對源文件執行mklink /H 操做(建立硬連接,硬連接的好處是若是DFS目錄下的文件刪除了,實際的文件是不會刪除的,由於還有一個硬連接引用實際的文件數據,硬連接會比Copy更快,IO會很是小)。3d

    3. File Server Resource Manager 自帶報告功能,所以能夠對操做過的文件進行報告生成。server

建立一個分類屬性IsHardLinkCreated ,這個屬性咱們用來過濾篩選沒有hardlink 過的文件(powershell 腳本會在hardlink 文件後,設置這個屬性)。blog

image

在c:\scripts\makelink.ps1 中作下面文件內容ip

param([string]$FileSource, [string]$FolderDestination)

# Capture source folder name and filename
$SourceFileName = (Get-Item $FileSource).Name
$SourceFolder = (Get-Item $FileSource).DirectoryName

# Destination Path
$DestinationPath = $FolderDestination + "\" + $SourceFolder.Substring(3, $SourceFolder.Length-3)

# Check Destination Path, create if doesn't exist
$CheckedPath = Get-Item $DestinationPath -ErrorAction SilentlyContinue
if ($CheckedPath -eq $null) {
    New-Item -Path $DestinationPath -ItemType Directory |Out-Null
}

$destFile=$DestinationPath + "\" + $SourceFileName

# test whether dest file exist ,if exist delete it .
if(test-path $destFile){
    Remove-Item -Path $destFile -Force
}

# Move original file
# Move-Item -Path $FileSource -Destination $DestinationPath -Force

# Create Hard link to origin file in destation folder
$expr = Invoke-Expression -Command ("cmd /c mklink /H `"" + $destFile + "`" `"" +$FileSource  + "`"")


# Please create Classification properties in File server resource manager named IsHardLinkCreated first

$cls = New-Object -com Fsrm.FsrmClassificationManager
$cls.setFileProperty($FileSource,"IsHardLinkCreated",1)
$cls=$nothing

建立File Management Tasks 的powershell 腳本。c:\scripts\CreateFSRMTask.ps1get

$Command = "C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe"
$CommandParameters = "`"C:\scripts\MakeLink.ps1 -FileSource '[Source File Path]' -FolderDestination 'E:\Expired'`""
$Action = New-FSRMFmjAction -Type Custom -Command $Command -CommandParameters $CommandParameters -SecurityLevel LocalSystem -WorkingDirectory "C:\Windows\System32\WindowsPowerShell\v1.0\"

$Condition = New-FsrmFmjCondition -Property "File.DateCreated" -Condition LessThan -Value "Date.Now" -DateOffset -5
$condition2=New-FsrmFmjCondition -Property "IsHardLinkCreated" -Condition NotExist

$Schedule = New-FsrmScheduledTask -Time (Get-Date) -Weekly Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday

New-FsrmFileManagementJob -Name "Make Hard Link of old files" -Namespace "E:\DR_FromSZ" -Action $Action -Condition $Condition,$condition2 -Schedule $Schedule




image

另外已經作過hardlink的文件會標記分類


image


這個時候即便DFS目錄的源文件被刪除,作了hardlink 的Expired 子目錄下的文件也還存在。


參考信息:

https://blogs.technet.microsoft.com/filecab/2009/05/11/customizing-file-management-tasks/

https://msdn.microsoft.com/en-us/library/windows/desktop/microsoft.storage.fsrmclassificationmanagerclass.setfileproperty(v=vs.85).aspx



總結:

  1. hardlink 若是建立後,源文件刪除,再在源位置建立同名文件,目標文件不會更新,這個時候若是要再建立hardlink ,需先刪除目標。

  2. 因爲這個powershell 的自定義任務我感受是順序執行,並且針對每一個文件調用一次powershell腳本,因此效率不是很高,可是對於備份後的大文件應該問題不大(由於大部分系統一天也就幾個大的備份文件),若是是不少小文件,執行時間就估計會比較慢。

相關文章
相關標籤/搜索