Skip to content

Basic Powershell SQL Backup – 2008

#run using the sqlps minishell
#e.g. sqlps bu_AdHoc.ps1 -db Testing -ServerName DemoServer -dest C:\Backups

param([string]$db ,[string]$ServerName, [string]$dest);

$timestamp = Get-Date -format yyyy_MM_dd_HHmmss;

$srv = New-Object Microsoft.SqlServer.Management.Smo.Server $ServerName;

$backup = New-Object (“Microsoft.SqlServer.Management.Smo.Backup”);

$backup.Action = “Database”;
$backup.Database = $db;
$backup.Devices.AddDevice($Dest + “\” + $db + “_full_” + $timestamp + “.bak”, “File”);
$backup.BackupSetDescription = “Full backup of ” + $db + ” ” + $timestamp;
$backup.CompressionOption = 1;
$backup.CopyOnly = 1;

#write backup command to the console for review if needed
write-host $backup.script($srv);

$backup.SqlBackup($srv);

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s