Skip to content

Instantly share code, notes, and snippets.

@PCfromDCSnippets
Last active January 29, 2016 02:02
Show Gist options
  • Save PCfromDCSnippets/86b6609693d411300ba8 to your computer and use it in GitHub Desktop.
Save PCfromDCSnippets/86b6609693d411300ba8 to your computer and use it in GitHub Desktop.
backupDatabases_Orig
$bkdir = "\\SQL2012B\Shared\Temp" # Set Backup Path!
# SSMS needed to be installed to load the SQL Assemblies
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | out-null
$s = new-object ("Microsoft.SqlServer.Management.Smo.Server") $instance
# Grabs ALL of the databases
$dbs = $s.Databases
foreach ($db in $dbs)
{
if(($db.Name -ne "tempdb") -and ($db.Name -ne "master") -and ($db.Name -ne "model") -and ($db.Name -ne "msdb"))
{
$dbname = $db.Name
$dbBackup = new-object ("Microsoft.SqlServer.Management.Smo.Backup")
$dbBackup.Action = "Database"
$dbBackup.Database = $dbname
$dbBackup.Devices.AddDevice($bkdir + "\" + $dbname + ".bak", "File")
$dbBackup.SqlBackup($s)
write-host($db.name + " has been backed up.")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment