Quantcast
Channel: SQL Server High Availability and Disaster Recovery forum
Viewing all articles
Browse latest Browse all 4689

SQL Server backup and powershell

$
0
0

My scritp works fine on backupfolder  but when i try to put subfolder it doesn't work company requeried each database have their owen folder 

$SQLInstance = "databaseserver"  ## SQL Server name
$BackupFolder = "D:\DB_BACKUp\DailyDatabaseBackup" ## Backup path
#$BackupFolder = "\\servershared\DailyDatabaseBackup" ## Backup path

$timeStamp = Get-Date -format MMddyyyy_HHmmss 

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

$Server = New-Object ("Microsoft.SqlServer.Management.Smo.Server") $SQLInstance


$Databases = New-Object Microsoft.SqlServer.Management.Smo.Database 
$Databases = $Server.Databases 

foreach ($Database in $Databases) 

$DBname = $Database.name
$DBStatus = $Database.IsAccessible
$RecoveryModel = $Database.RecoveryModel

if( $DBStatus -eq $True -and $DBName -ne "TempDB")
{

$Backup = New-Object ("Microsoft.SqlServer.Management.Smo.backup") 


$Backup.Action = "Database"

$Backup.Database = $Database.Name 
$Backup.MediaDescription = "Disk"
$Backup.Devices.AddDevice($Backupfolder+ "\" + $DBname +"\full\"+ $DBname +"_" + $timeStamp + ".bak", "File") 

$Backup.SqlBackup($Server)


}

I am getting below error

Exception calling "SqlBackup" with "1" argument(s): "Backup failed for Server 'col-webdb-01'. "
At line:15 char:26
+         $Backup.SqlBackup <<<< ($Server)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException



Viewing all articles
Browse latest Browse all 4689

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>