Last active
May 2, 2019 07:44
-
-
Save asvignesh/9c2f275c9418b038022ceffa94ba2152 to your computer and use it in GitHub Desktop.
Powershell script to backup all database, to run .\BackupAllDatabases.ps1 -serverName NW\CLUST -backupDirectory c:\backupdir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
$serverName, | |
$backupDirectory | |
) | |
[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 | |
$server = New-Object ("Microsoft.SqlServer.Management.Smo.Server") $serverName | |
$dbs = $server.Databases | |
foreach ($database in $dbs | where { $_.IsSystemObject -eq $False }) | |
{ | |
$dbName = $database.Name | |
$timestamp = Get-Date -format yyyy-MM-dd-HHmmss | |
$targetPath = $backupDirectory + "\" + $dbName + "_" + $timestamp + ".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" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment