【发布时间】:2016-06-13 00:00:54
【问题描述】:
我在服务器上有一个文件夹,其中包含数千个日志文件。每秒都在填充新文件,并且文件夹继续增长。每周一次,我想获取这些文件,将它们复制并粘贴到另一个文件夹中。然后我将运行一个 python 脚本来处理新文件夹中的日志。我的脚本每周处理大约 70K 日志,需要一个多小时。
我怎样才能让它更高效/更快?
$ScriptProperties = @{
"FolderDir" = "\\server\folder1\Temp";
"FolderName" = "Orignal_Folder";
"OldFolderName" = "New_Folder";
"TempFolderName" = "Temp_Folder";
}
#$DateStamp = get-date -uformat "%Y-%m-%d@%H-%M-%S"
# if (Test-Path "$($ScriptProperties.FolderDir)\$($ScriptProperties.FolderName)"){
# #write-host "$($ScriptProperties.FolderDir)\$($ScriptProperties.FolderName) folder folder"
# Copy-Item -Path "$($ScriptProperties.FolderDir)\$($ScriptProperties.FolderName)" -Destination "$($ScriptProperties.FolderDir)\$($ScriptProperties.OldFolderName)-$($DateStamp)" -force -recurse
# #New-Item -Path "$($ScriptProperties.FolderDir)\$($ScriptProperties.FolderName)" -type directory -force
# }#else{
# #write-host "folder not found."
# New-Item -Path "$($ScriptProperties.FolderDir)\$($ScriptProperties.FolderName)" -type directory
#}
write-host "Start"
if (Test-Path "$($ScriptProperties.FolderDir)\$($ScriptProperties.FolderName)"){
#Renaming folder to Temp directory
Rename-Item "$($ScriptProperties.FolderDir)\$($ScriptProperties.FolderName)" -NewName "$($ScriptProperties.FolderDir)\$($ScriptProperties.TempFolderName)"
#Creating new Log directory
New-Item -Path "$($ScriptProperties.FolderDir)\$($ScriptProperties.FolderName)" -type directory
#Creating new Move folder if not found
if (-Not (Test-Path "$($ScriptProperties.FolderDir)\$($ScriptProperties.OldFolderName)")) {New-Item -Path "$($ScriptProperties.FolderDir)\$($ScriptProperties.OldFolderName)" -type directory -force}
#Moving content to move directory
Move-Item -Path "$($ScriptProperties.FolderDir)\$($ScriptProperties.TempFolderName)\*.*" -Destination "$($ScriptProperties.FolderDir)\$($ScriptProperties.OldFolderName)" -force
#Removing temp directory
Remove-Item "$($ScriptProperties.FolderDir)\$($ScriptProperties.TempFolderName)" -Force
}
write-host "Complete"
【问题讨论】:
-
您正在寻找
robocopy。 -
这取决于你的瓶颈在哪里。如果您的瓶颈是网络,那么传输更少的数据或以更少的开销传输数据会有所帮助。如果瓶颈是源或目标 CPU、内存或 I/O(即 PCIe)通道,那么您需要改善服务器上的情况(减少负载或增加服务器容量)。假设它是网络,那么压缩日志文件将显着减少要传输的数据量(日志文件是高度可压缩的)。为了最有效,服务器不能太接近 CPU 的全部利用率。
-
Ansgar- 我一直在测试 robocopy。我测试了移动 600 个文件,大约需要 1:00 分钟。在生产中,我将移动大约 70K-80K 文件,根据我的测试,这大约需要 2 小时。速度很重要,但也许这是我能做的最好的了。
-
Xpw- 我认为两者兼而有之。目的地的来源可能没有足够的容量,但增加它不是一个有成本的选择。网络也可能很慢。我试过压缩文件夹,但也需要一个多小时。
-
传输数以万计的文件需要时间,无论您采用哪种方式。
标签: python powershell logging copy-paste large-data-volumes