Created
May 2, 2019 07:44
-
-
Save asvignesh/b89d3e077aef294f7a59a9d5769cb62a to your computer and use it in GitHub Desktop.
Powershell script to backup all database
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