![集中式 powershell 备份脚本](https://linux22.com/image/754960/%E9%9B%86%E4%B8%AD%E5%BC%8F%20powershell%20%E5%A4%87%E4%BB%BD%E8%84%9A%E6%9C%AC.png)
我们需要一个 powershell 脚本来从 3 个远程服务器备份文件夹:
服务器1 服务器2 服务器3
每个远程服务器上的源路径相同:c:\backupfiles
发送到包含每个文件夹当天日期的中央本地文件夹:c:\allbackups\server1<DATE> c:\allbackups\server2<DATE> c:\allbackups\server3<DATE>
最干净的方法是什么?谢谢,
答案1
这是完成这项工作的方法。[咧嘴笑] 它比所要求的要多一点,但由于缺乏适当的错误处理,因此尚未完全填写。无论如何......这是它的作用......
- 创建服务器计算机列表
- 设置各种常量
- 创建目标根目录
这里是一个“缺乏错误处理”的点 - 没有检查确保该目录已创建并且可写。 - 遍历计算机列表
- 检查远程源目录
- 如果不存在,则在屏幕上
另一个缺少的地方写入警告。您可能需要将缺少的源添加到 $Var 或错误文件中。 - 如果是的话...
- 建立完整备份目标目录
- 创建它并忽略任何错误,
这又是一个糟糕的错误处理。[叹息……] - 将参数构建
robocopy
到数组中 - 使用上述参数 splat 调用 RC
来查找Get-Help about_Splatting
真正巧妙的功能。[咧嘴笑] - 完成对计算机列表的迭代
代码 ...
$ComputerList = @(
'LocalHost'
'127.0.0.1'
$env:COMPUTERNAME
)
# swap the `#` on these two lines to test "no source found"
$SourceDir = 'd$\Temp'
#$SourceDir = 'd$\Temp\NotThere'
$RootDestDir = "$env:TEMP\Backups"
if (-not (Test-Path -LiteralPath $RootDestDir))
{
$Null = New-Item -Path $RootDestDir -ItemType 'Directory'
}
$FileSpec = '*.*'
$TimeStamp = (Get-Date).ToString('yyyy-MM-dd')
$Subject = 'RC_Backup'
$LogFileName = -join ($Subject, '_-_', $TimeStamp, '.log')
$FullLogFileName = Join-Path -Path $env:TEMP -ChildPath $LogFileName
foreach ($CL_Item in $ComputerList)
{
$RemoteSourceDir = '\\{0}\{1}' -f $CL_Item, $SourceDir
if (Test-Path -LiteralPath $RemoteSourceDir)
{
$FullDestDir = Join-Path -Path $RootDestDir -ChildPath ('{0}_-_{1}' -f $CL_Item, $TimeStamp)
$Null = mkdir -Path $FullDestDir -ErrorAction 'SilentlyContinue'
$RC_Params = @(
$RemoteSourceDir
$FullDestDir
$FileSpec
# put your current options below
"/Log:$FullLogFileName"
'/NP'
'/E'
# comment out the next line to suppress on-screen output
'/TEE'
)
robocopy $RC_Params
}
else
{
Write-Warning (' [ {0} ] was not found on [ {1} ].' -f $RemoteSourceDir, $CL_Item)
}
} # end >>> foreach ($CL_Item in $ComputerList)
如果源不可达则输出...
WARNING: [ \\LocalHost\d$\Temp\NotThere ] was not found on [ LocalHost ].
WARNING: [ \\127.0.0.1\d$\Temp\NotThere ] was not found on [ 127.0.0.1 ].
WARNING: [ \\MySysName\d$\Temp\NotThere ] was not found on [ ZK_01 ].
成功运行的截断输出......
Log File : C:\Temp\RC_Backup_-_2020-07-13.log
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Mon Jul 13 16:11:34 2020
Source : \\LocalHost\d$\Temp\
Dest : C:\Temp\Backups\LocalHost_-_2020-07-13\
Files : *.*
Options : *.* /TEE /S /E /COPY:DAT /NP /R:1000000 /W:30
------------------------------------------------------------------------------
1 \\LocalHost\d$\Temp\
New File 7.7 m testing.zip
New Dir 2 \\LocalHost\d$\Temp\1\
New File 422446 Lee_CHOICES_2017-07-07
New File 89854 Lee_CHOICES_2017-07-07.H3M
New Dir 23 \\LocalHost\d$\Temp\2\
New File 549 Archive_Backup_to_NAS_-_Update_One_Way_[daily] 2016-03-07 010000.log
New File 5119 Data_-_Update_One_Way_[daily] 2016-03-07 020000.log
New File 322186 Documents_-_Update_One_Way_[monthly] 2016-02-01 030000 [Error].log
New File 367981 Documents_-_Update_One_Way_[monthly] 2016-03-01 030000 [Error].log
New File 58906 Email_-_Update_One_Way_[monthly] 2016-02-01 040000 [Error].log
New File 59151 Email_-_Update_One_Way_[monthly] 2016-03-01 040004 [Error].log
New File 75434 Generic_Backup_to_NAS_-_Mirror_One_Way_[daily] 2016-03-07 050000 [Warning].log
[*...snip...*]
New File 326 desktop.ini
New File 25.0 m Wildlife.wmv
------------------------------------------------------------------------------
Total Copied Skipped Mismatch FAILED Extras
Dirs : 234 233 1 0 0 0
Files : 320 320 0 0 0 0
Bytes : 290.15 m 290.15 m 0 0 0 0
Times : 0:00:06 0:00:06 0:00:00 0:00:00
Speed : 49624024 Bytes/sec.
Speed : 2839.509 MegaBytes/min.
Ended : Mon Jul 13 16:11:40 2020
答案2
这就是我的简单的想法。
$ServerList = @( Server1 Server2 Server3 )
$AllBackups = "c:\allbackups"
$TimeStamp = (Get-Date).ToString('yyyyMMdd')
$SourceFolder = "C$\backupfiles"
foreach ($server in ServerList) {
Copy-Item -Path "\\$server\$SourceFolder" -Destination "$AllBackups\$server-$Timestamp" -Recurse
}