Powershell备份脚本不进行每周备份

时间:2017-06-12 23:41:48

标签: sql-server powershell

所以我有以下代码应该每天,每周和每月备份每个数据库。每日备份工作正常,但它似乎不会创建每周或每月备份。

脚本的数据库连接部分如下所示:

$serverName = "."
$backupDirectory = "\\SERVER\BACKUP"
$daysToStoreDailyBackups = 7
$daysToStoreWeeklyBackups = 28
$monthsToStoreMonthlyBackups = 3

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null

$mySrvConn = new-object Microsoft.SqlServer.Management.Common.ServerConnection
$mySrvConn.ServerInstance=$serverName
$mySrvConn.LoginSecure = $false
$mySrvConn.Login = "sa"
$mySrvConn.Password = "myPAssword"

$server = new-object Microsoft.SqlServer.Management.SMO.Server($mySrvConn)

$dbs = $server.Databases
$startDate = (Get-Date)
"$startDate" 

脚本的每日和每周部分如下:

foreach ($database in $dbs | where {$_.IsSystemObject -eq $False})
{
    $dbName = $database.Name      

    if ($dbName -ne "ReportServer" -and $dbName -ne "ReportServerTempDB")
    {
        $timestamp = Get-Date -format yyyy-MM-dd-HHmmss
        $targetPath = $backupDirectory + "\" + $dbName + "_" + $timestamp + "_daily.bak"

        $smoBackup = New-Object ("Microsoft.SqlServer.Management.Smo.Backup")
        $smoBackup.Action = "Database"
        $smoBackup.BackupSetDescription = "Full Backup of " + $dbName
        $smoBackup.BackupSetName = $dbName + " Backup"
        $smoBackup.Database = $dbName
        $smoBackup.MediaDescription = "Disk"
        $smoBackup.Devices.AddDevice($targetPath, "File")
        $smoBackup.SqlBackup($server) 
        "backed up $dbName ($serverName) to $targetPath"    
    }
    else
    {
        "$dbName backup skipped"
    }
}

if([Int] (Get-Date).DayOfWeek -eq 0)
{
    Get-ChildItem "$backupDirectory\*_weekly.bak" |? { $_.lastwritetime -le (Get-Date).AddDays(-$daysToStoreWeeklyBackups)} |% {Remove-Item $_ -force }
    "removed all previous daily backups older than $daysToStoreWeeklyBackups days"

    foreach ($database in $dbs | where { $_.IsSystemObject -eq $False})
    {
        if ($dbName -ne "ReportServer" -and $dbName -ne "ReportServerTempDB")
        {
            $dbName = $database.Name      

            $timestamp = Get-Date -format yyyy-MM-dd-HHmmss
            $targetPath = $backupDirectory + "\" + $dbName + "_" + $timestamp + "_weekly.bak"

            $smoBackup = New-Object ("Microsoft.SqlServer.Management.Smo.Backup")
            $smoBackup.Action = "Database"
            $smoBackup.BackupSetDescription = "Full Backup of " + $dbName
            $smoBackup.BackupSetName = $dbName + " Backup"
            $smoBackup.Database = $dbName
            $smoBackup.MediaDescription = "Disk"
            $smoBackup.Devices.AddDevice($targetPath, "File")
            $smoBackup.SqlBackup($server) 
            "backed up $dbName ($serverName) to $targetPath"
        } 
        else
        {
            "$dbName backup skipped"
        }                   
    }
}

我认为它会在周日创建每周备份,但这并没有发生。

1 个答案:

答案 0 :(得分:2)

在每日区块中

$dbName = $database.Name      

if ($dbName -ne "ReportServer" -and $dbName -ne "ReportServerTempDB")
{

但在每周一栏中,你有

if ($dbName -ne "ReportServer" -and $dbName -ne "ReportServerTempDB")
{
    $dbName = $database.Name      

在设置之前检查dbName。如果检查失败,则永远不会为任何数据库设置。

如果您的代码来自作为“ReportServerTempDB”的每日块值,它将永远不会在每周块中传递if测试,并且dbName不会更改,因此不会备份任何内容。

a)将$dbName = $database.Name作业移到if

之上

b)或使用$database.Name directly

c)或者没有大块的复制粘贴代码必须是相同的,但可能会以某种方式结束不同。而是将该重复块移动到一个函数并从两个位置调用它。