在我的备份脚本下面,我想提出一些意见、提示和建议。它已经成为一个相当大且功能多样的脚本,这不是我的第一个脚本,但我正在寻找增强它的方法。尤其是记录的方式和复制文件的方式
$LogFile = "z:\backup_ms01.log"
$before=get-date
"Start backup today $before">> $logfile
Write-Host `n
Write-Host ` ` "============ Start Backup MS01 Exchange databases ============" -foregroundcolor Cyan
write-host ` ` "Datum/tijd begin backup: $before" -foregroundcolor Cyan
write-host `n
Write-Host ` ` ` ` ` ` "opruimen oude backups" -foregroundcolor Cyan
$FolderPath="z:\backup"
$threeweeksAgo = (Get-Date).AddDays(-21)
"check old backups $threeweeksago">> $logfile
Get-ChildItem -Path $FolderPath | Where-Object {$_.mode -match "d" -and $_.CreationTime -lt $threeweeksAgo} | Select-Object Name, CreationTime >> $logfile
Get-ChildItem -Path $FolderPath | Where-Object {$_.mode -match "d" -and $_.CreationTime -lt $threeweeksAgo} | Remove-Item -recurse -force
"Oude backups zijn verwijderd" >> $logfile
$warningpreference="SilentlyContineu"
$server1=gc env:computername
if ($server1 -eq "ms01")
{
"De Server is idd $server1, folders worden aangemaakt...">> $logfile
$foldername=get-date -uformat "%Y-%m-%d"
New-Item "z:\backup\$foldername" -type directory
New-Item "z:\backup\$foldername\l" -type directory
New-Item "z:\backup\$foldername\p" -type directory
New-Item "z:\backup\$foldername\u" -type directory
$source1= "l:\services\*"
$source2= "p:\services\*"
$source3= "u:\services\*"
$dest1= "Z:\backup\$foldername\l"
$dest2= "Z:\backup\$foldername\p"
$dest3= "Z:\backup\$foldername\u"
"getting date and create destination folder date is z:\backup\$foldername">> $logfile
write-host ` ` ` ` ` ` "Stoppen services" -foregroundcolor Cyan
Get-Service | where-Object { $_.name -like '*Exchange*'} | Stop-Service -Force
$services = Get-Service MSExchange*
"====================================================">>$logfile
"Gestopte services:">> $logfile
foreach ($service in $services)
{$service | format-table Name, Status -HideTableHeader >> $logfile}
"====================================================">> $logfile
write-host ` ` ` ` ` ` "Services stoppen" -foregroundcolor Cyan
write-host ` ` ` ` ` ` "Starten Kopieren" -foregroundcolor Cyan
#L: schijf
$Folder1 = Get-childitem $source1
Copy-Item $source1 $dest1 -Force -recurse
"copy items from $source1 to $dest1">> $logfile
write-host ` ` ` ` ` ` ` ` "Kopieren L: klaar" -foregroundcolor Cyan
$Folder2 = Get-childitem $dest1
Compare-Object $Folder1 $Folder2 -Property Name, Length | Where-Object {$_.SideIndicator -eq "<="} >> $logfile
#P: schijf
$Folder3 = Get-childitem $source2
Copy-Item $source2 $dest2 -Force -recurse
"copy items from $source2 to $dest2">> $logfile
write-host ` ` ` ` ` ` ` ` "Kopieren P: klaar" -foregroundcolor Cyan
$Folder4 = Get-childitem $dest2
Compare-Object $Folder3 $Folder4 -Property Name, Length | Where-Object {$_.SideIndicator -eq "<="} >> $logfile
#U: schijf
$Folder5 = Get-childitem $source3
Copy-Item $source3 $dest3 -Force -recurse
"copy items from $source3 to $dest3">> $logfile
write-host ` ` ` ` ` ` ` ` "Kopieren U: klaar" -foregroundcolor Cyan
$Folder6 = Get-childitem $dest3
Compare-Object $Folder5 $Folder6 -Property Name, Length | Where-Object {$_.SideIndicator -eq "<="} >> $logfile
write-host ` ` ` ` ` ` "Kopieren klaar" -foregroundcolor Cyan
write-host ` ` ` ` ` ` "Services starten" -foregroundcolor Cyan
Get-Service | where-Object { $_.name -like '*Exchange*'} | Start-Service | out-null
$services1 = Get-Service MSExchange*
"====================================================">> $logfile
"Gestarte services:">> $logfile
foreach ($service1 in $services1)
{$service1 | format-table Name, Status -HideTableHeaders >> $logfile}
"====================================================">> $logfile
"ending backup">> $logfile
}
$after=get-date
$verstreken=($after - $before)
write-host `n
Write-host ` ` ` ` ` ` ` "Overige informatie:" -foregroundcolor Cyan
write-host ` ` ` ` ` ` ` "Duur van de backup: $verstreken" -foregroundcolor Cyan
"Runtime = $verstreken" >>$logfile
Get-date >$logfile
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($logfile)
$smtp = new-object Net.Mail.SmtpClient($server1)
$msg.From = “...@.....”
$msg.To.Add(”...@.....”)
$msg.To.Add("...@.....”)
$msg.To.Add("...@.....")
$msg.Subject= “Backup MS01 mail database”
$msg.Body = “Logfile is bijgevoegd” + $after
$msg.Attachments.Add($att)
$smtp.Send($msg)
$einde= Get-date
write-host `n
write-host ` ` "Datum/tijd einde backup: $einde" -foregroundcolor Cyan
Write-Host ` ` "============ Einde Backup MS01 Exchange databases ============" -foregroundcolor Cyan
答案1
总结
您征求意见,简而言之,转储脚本并使用内置的 Windows Server Backup 实用程序。它将比您的脚本更具弹性,并且可以进行正确的 Exchange 感知备份。
我还没有尝试完全理解您的脚本,但它看起来就像您正在停止 Exchange 服务,复制数据库文件,然后重新启动 Exchange 服务。这不是你应该备份 Exchange 的方式。
Exchange 的设计目标是始终运行,因此您可以在线备份。这意味着您可以在备份 Exchange 时继续使用,并且仍能获得一致的备份。此外,如果您的脚本运行到一半时出现故障,您的所有服务都可能停止运行,而适当的 Exchange 感知备份实用程序则不会出现这种情况。
此外,除非您启用了循环日志记录(顺便说一句,这很糟糕),否则您手中就有一颗定时炸弹。当 Exchange 使用正确的 VSS API 进行备份时,事务日志会被刷新。经常刷新这些日志很重要,因为如果日志所在的卷已满,数据库将卸载。
有许多解决方案可用于正确备份 Exchange,有些成本高昂,有些则不那么昂贵。但是,如果您正在寻找完全免费的解决方案,Windows Server Backup 已集成到操作系统中,并且能够执行 Exchange 感知备份。
答案2
首先,您似乎有很多冗余代码,只是复制/粘贴了一些不同的值。我会修改您的脚本以使用函数,然后在调用函数时传入这些不同的值。这将使您和/或其他人更容易理解和维护。