Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Backup MSSQL databases and verify backups

$
0
0

Hello all, 

i have a simple script that backups all none system DBs on instance which works fine.

I want to add something like mssql "verify-only" to check if backups are OK.

Bellow is the script that i have so far, but is is not workingn as expected.

 

$date = get-date -format  "dd.MM.yyyy"
$dir = "C:\SQLbackups\$date"
New-Item -type Directory -path $dir




$ServerName = "localhost\Instace1"
$BackupDirectory = $dir
$BackupDate = get-date -format yyyyMMdd_HHmmss
write-host "Creating database backups new folder" 

[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
#[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null

$ServerSMO = new-object ("Microsoft.SqlServer.Management.Smo.Server") $ServerName
$DatabaseList = $ServerSMO.Databases

foreach ($Database in $DatabaseList)
{
if($Database.IsSystemObject -ne $True -and  $Database.IsAccessible -eq $True)

{
$DatabaseName = $Database.Name
write-host "##########################################################"
write-host "##### Database" $Database.Name "backup in progress #######"
write-host "##########################################################"
$DatabaseBackup = new-object ("Microsoft.SqlServer.Management.Smo.Backup")
$DatabaseBackup.Database = $DatabaseName
$DatabaseBackup.Action = "Database"
$DatabaseBackup.Action = [Microsoft.SqlServer.Management.Smo.BackupActionType]::Database
$DatabaseBackup.Initialize = $TRUE
$DatabaseBackup.Devices.AddDevice($BackupDirectory + "\" + $DatabaseName + "_" + $BackupDate + ".BAK", "File")
$DatabaseBackup.SqlBackup($ServerSMO)
#$DatabaseRestore = new-object ("Microsoft.SqlServer.Management.Smo.Restore")
#$DatabaseRestore.Devices.AddDevice($DatabaseBackup, [Microsoft.SqlServer.Management.Smo.DeviceType]::File)
#$DatabaseRestore.SqlVerify($ServerSMO)
#write-host "Backed up file" $Database.Name  " has been verified. Health status OK"
}
}


set-location $BackupDirectory
$DBBackups = Get-content backups.txt

foreach ($DBBackup in $DBBackups)
{
$DatabaseRestore = new-object ("Microsoft.SqlServer.Management.Smo.Restore")

$DBRestore.Devices.AddDevice($DBBackup, [Microsoft.SqlServer.Management.Smo.DeviceType]::File)
if (!($DBRestore.SqlVerify($ServerSMO))){

write-host "Backed up file" $Database.Name " has been verified. Health status OK"
}
}

 

So here, backups are done, and second foraech  should go through txt file in which all backups are listed and "Microsoft.SqlServer.Management.Smo.Restore" should run SQLVerify against each database .bak file.

What i have in output in ISE is not what i want. It is like it ran multiple times against the last file in txt. instead of checking each file.


Backed up file testDB1  has been verified. Health status OK
Backed up file testDB1  has been verified. Health status OK
Backed up file testDB1  has been verified. Health status OK
Backed up file testDB1  has been verified. Health status OK

 

This os one case.

The second case is If i remove the second foreach in the script and put  ("Microsoft.SqlServer.Management.Smo.Restore" in the first foreach loop where i am backing up each DB ( it is commented in the code here)  it is working but returns one strange "false" in output like:

 

##########################################################
##### Database Test1_database backup in progress #######
##########################################################
False
Backed up file Test1_database  has been verified. Health status OK
##########################################################
##### Database testDB1 backup in progress #######
##########################################################
False
Backed up file testDB1  has been verified. Health status OK

Can someone help me out here.


Viewing all articles
Browse latest Browse all 6937

Trending Articles