Powershell:检查今天的备份是否存在

时间:2012-04-06 16:31:13

标签: powershell powershell-v2.0

我已经将不同服务器上的各种SQL数据库(共18个)的每日自动副本设置为NAS上的一组文件夹,每个文件夹包含每日.bak备份(服务器A - > C:\ Backups \ A \,服务器B - > C:\ Backups \ B \等)。我需要一个powershell脚本来检查(每天)每个文件夹以查看副本是否成功。我希望它能够在日志中写入复制的文件的名称,以及它们的大小,以及没有复制的文件的目录(即当天的.bak本应该存在的文件夹)。我有一个列出目标文件夹的数组。我已复制到目前为止我所拥有的内容,但我不确定如何检查'if object的lastwritetime = todaysdate'并记录是否确实如此。对不起,如果这令人困惑,这是我与Powershell合作的第一周。任何见解将不胜感激。

foreach ($folder in $array.destination)
  {gci | sort-object lastwritetime -descending |select -first 1
If
   ($object lastwritetime.Date -eq (Get-Date).Date  #this is wrong, but generally what I want to do
        | Out-File "C:\Script\.log" | ft * -autosize)
Else
  (
  **WRITE DESTINATION OF TODAY'S MISSING .bak TO LOG***
  )
    }

2 个答案:

答案 0 :(得分:0)

没有必要格式化,如果你这样做,它必须在管道输出到Out-File之前。看看这是否有助于您开始朝着正确的方向前进:

$file=dir -path c:\scripts\ | sort LastWriteTime | select -last 1

if ($file.LastWriteTime.ToShortDateString() -eq (get-date).ToShortDatesTring()) {
 #file dates match
 $file | Out-File Log.txt -append
}
else {
 #dates do not match
 Write "destination missing backup log"
}

答案 1 :(得分:0)

此脚本检查数据库并标记是否有任何备份失败。它检查完整,差异和日志备份。

#Backup Check Version 1.0.
#List Server Name from 
$ServerList = Get-Content "D:\PSScripts\BackupScript\ServerList.txt" 
#Output file Location : Put is IIS if you want to publish 
$OutputFile = "c:\IIS\BackupReport.htm" 
 $HTMLHDR=""
 $BODYRED =""
 $BODYGRN=""
 $now=get-date
 $FOOTER=""

$HTMLHDR += '<style type="text/css"> 
    #Header{font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;width:700;border-collapse:collapse;} 
    #Header td, #Header th {font-size:12px;border:1px solid #98bf21;padding:3px 7px 2px 7px;} 
    #Header th {font-size:10px;text-align:center;padding-top:5px;padding-bottom:4px;background-color:#A7C942;color:#fff;} 
    #Header tr.alt td {color:#000;background-color:#EAF2D3;} 
    </Style>' 

$HTMLHDR += "<HTML><HEAD><TITLE>Backup Page</TITLE></HEAD><BODY>
        <p align=Center><font size=4 face=Bodoni MT color=#003333><B>Backup Report :Location</B></font></p>
        <p align=Center><font face=Bodoni MT color=Green size=3>Last Pulled : $now IST</font></P>
        <center><Table border=1 cellpadding=0 cellspacing=0 width=900 id=Header aling ='center'> 
        <TR> 
            <TH><B>Server Name</B></TH> 
            <TH><B>Database Name</B></TH> 
            <TH><B>RecoveryModel</B></TD> 
            <TH><B>Last Full Backup Date</B></TH> 
            <TH><B>Last Differential Backup Date</B></TH> 
            <TH><B>Last Log Backup Date</B></TH> 
        </TR>"
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null 

## Function if you need to skip or include any specific DB
function CheckDBName ($DBName){
$NameFlage ="true"
    if($DBName -like "*DBName*")    {$NameFlage ="false"}   

    return $NameFlage 
}

##Get Backup details 
ForEach ($ServerName in $ServerList) 
{ 
    if($ServerName -ne ""){
    $DT = Get-WmiObject -Class Win32_LocalTime -ComputerName $servername
    $DateTime = (Get-Date -Day $DT.Day -Month $DT.Month -Year $DT.Year -Minute $DT.Minute -Hour $DT.Hour -Second $DT.Second)

    $SQLServer = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $ServerName  
    Foreach($Database in $SQLServer.Databases) 
        {
        $FLAGE="false"
        $color="Green"  
        $model=$($Database.RecoveryModel)
        $DBName=$Database.Name
        $FBKP=($DateTime-$Database.LastBackupDate).days
        $DBKP=($DateTime-$Database.LastDifferentialBackupDate).days
        $LBKP=($DateTime-$Database.LastLogBackupDate).hours
        $WDay=$DateTime.DayOfWeek
            if($(CheckDBName($Database.name)) -eq "true")   {
                            $tmp= "<tr><TD>$($Servername)</TD><TD>$($Database.Name)</TD><TD>$model</TD>"            
                if($FBKP -ge 7){
                        $color="RED"
                        $FLAGE="true"
                        }
                if($WDay -like "mon*"){
                        if($FBKP -ge 4){
                        $color="RED"
                        $FLAGE="true"
                        }}
                            $tmp+="<TD bgcolor='$color'>$($Database.LastBackupDate)</TD> "
                            $color="Green"
                if($DBKP -ge 1 -AND $FBKP -gt 1){

                    $color="RED"
                    $FLAGE="true"
                    }

                            $tmp +="<TD bgcolor='$color'>$($Database.LastDifferentialBackupDate)</TD>"
                            $color="Green"
                if($Model -like "*Full*"){
                if($LBKP -ge 2){
                    $color="RED"
                    $FLAGE="true"
                }   }
                            $tmp +="<TD bgcolor='$color'>$($Database.LastLogBackupDate)</TD> </TR>" 
                            $color="Green"

                    if($FLAGE -eq "TRUE"){$BODYRED +=$tmp}
                    else {$BODYGRN+=$tmp}
                }
        }
    }
    }
$FOOTER+= "</Table></BODY></HTML>" 
$FILE=$HTMLHDR+$BODYRED+$BODYGRN+$FOOTER
$FILE | Out-File $OutputFile

Exit

###########END#########################