背景信息:
我们的数据存放位置有三个,本地、同城、异地。备份数据会放本地,然后常规通过DFS同步到同城、异地两个地方。由于DFS的特性是双向同步的,所以你如果在一个地方删除、更新文件,那么三个地方是会同步更新成一致的。
由于数据量的大小的限制,我没有办法无限制的保存数据在服务器上,因此会定期把旧备份删除。但是我异地的服务器的本地硬盘又比较大,我又想差异化的把历史数据最大限度的在异地服务器上进行保存。
解决方案:
File Server Resource Manager 的File Managerment Task 功能可以针对文件的创建日期、修改日期或者自定义分类属性进行过滤,并且执行自定义action. 这里的自定义action 我们使用powershell 脚本。
powershell 脚本针对过滤出的文件在另外一个目录对源文件执行mklink /H 操作(创建硬链接,硬链接的好处是如果DFS目录下的文件删除了,实际的文件是不会删除的,因为还有一个硬链接引用实际的文件数据,硬链接会比Copy更快,IO会非常小)。
File Server Resource Manager 自带报告功能,因此可以对操作过的文件进行报告生成。
创建一个分类属性IsHardLinkCreated ,这个属性我们用来过滤筛选没有hardlink 过的文件(powershell 脚本会在hardlink 文件后,设置这个属性)。
在c:\scripts\makelink.ps1 中做下面文件内容
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.ps1
$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
另外已经做过hardlink的文件会标记分类
这个时候即使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
总结:
hardlink 如果创建后,源文件删除,再在源位置创建同名文件,目标文件不会更新,这个时候如果要再创建hardlink ,需先删除目标。
由于这个powershell 的自定义任务我感觉是顺序执行,而且针对每个文件调用一次powershell脚本,所以效率不是很高,但是对于备份后的大文件应该问题不大(因为大部分系统一天也就几个大的备份文件),如果是很多小文件,执行时间就估计会比较慢。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。