Red Gate forums :: View topic - Backup a database in a line of code from a web server.
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation

Deployment Manager PowerShell Scripts forum

Backup a database in a line of code from a web server.

Search in Deployment Manager PowerShell Scripts forum
Post new topic   Reply to topic
Jump to:  
Author Message
jtucker@inflexxion.com



Joined: 27 Jun 2013
Posts: 12
Location: Boston

PostPosted: Mon Jan 13, 2014 1:58 pm    Post subject: Backup a database in a line of code from a web server. Reply with quote

Prerequisite is installing SQLPS on a web server.
To install SQLPS on a web server...

First download and install the SQLSysClrTypes, SharedManagementObject.msi, and PowerShellTools.msi
http://blog.smu.edu/wis/2012/11/26/sql-server-powershell-module-sqlps/

Use the default locations for all installs.
At this point you will have the SQLPS module available in the path:
"c:\program files\microsoft sql server\110\tools\Powershell\modules\SQLPS\sqlps"

Upgrade to Powershell 4. using the Windows update script. Windows6.1-KB2819745-x64-MultiPkg
(likely will require a reboot)
In a powershell script...

if ([string]::IsNullOrEmpty($SQLPowershellPath))
{
$SQLPowershellPath = "c:\program files\microsoft sql server\110\tools\Powershell\modules\SQLPS\sqlps"
}
$cur = Get-Location
import-module $SQLPowershellPath -DisableNameChecking
set-location $cur
# now for the powerful part.


Here is a sample backup database script - note we change the Red Gate Agent's service account to a windows account that has rights to perform
database backups.
# $ServerInstance is the address of the SQL Server instance such as 10.240.20.25\Sql2012
# $Database is the actual name of the database like "DynamicDeployment"
# $BackupDirectory is the path to the SQL Server's machine directory D:\RedGateDatabaseBackups (on 10.240.20.25)

function BackupDatabase($ServerInstance, $Database, $BackupDirectory)
{

$backupFile = "$BackupDirectory\$($Database).bak"
"Entering Powershell Library: BackupDatabase2012 with ServerInstance " + $ServerInstance + " Database " + $Database + " Backup directory " + $BackupDirectory + " Backup file " + $backupFile | Write-Host

"Query is" + "BACKUP DATABASE $Database to DISK='$backupFile' with INIT" | Write-Host

if ([string]::IsNullOrEmpty($SQLPowershellPath))
{
$SQLPowershellPath = "c:\program files\microsoft sql server\110\tools\Powershell\modules\SQLPS\sqlps"
}
$cur = Get-Location
import-module $SQLPowershellPath -DisableNameChecking
set-location $cur



Invoke-Sqlcmd -QueryTimeout 800 -Query "BACKUP DATABASE $Database to DISK='$backupFile' with INIT" -ServerInstance $ServerInstance
}
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group