Getting Started with Web applications development with servlets and JSP - Part 5: Filters
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.


Upload Image Close it
Select File

Learned something today? Share it, or learn from what others have learned today

Perform database backup using SQLCMD utility in SQL Server

Nov 10 2011 2:22AM by Paresh Prajapati   

I just went through the SQLCMD command line utility to perform database backup in sql server where it does not have sql agent. Below is the command which i placed in batch file and schedule with windows task scheduler.

<pre class="brush: plain">
Sqlcmd -UUserName -PPassword -SServerName -dDatabaseName -q"DECLARE @backupFilePath varchar(100); SET @backupFilePath = 'D:\BackupPath\DBName\'+'DBName_'+REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR,GETDATE(),120),'-',''),':',''),' ','_') + '.bak'; Backup database DBName to disk = @backupFilePath "
</pre>

Above command perform full database backup but it does not quit from utility after perform it. So i have changed with slight change and placed -Q in place of -q for cmdline query. Otherwise next schedule will be skipped as of current schedule never come out from sqlcmd utility and keep running.

<pre class="brush: plain">
Sqlcmd -UUserName -PPassword -SServerName -dDatabaseName -Q"DECLARE @backupFilePath varchar(100); SET @backupFilePath = 'D:\BackupPath\DBName\'+'DBName_'+REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR,GETDATE(),120),'-',''),':',''),' ','_') + '.bak'; Backup database DBName to disk = @backupFilePath "
</pre>

This will perform backup and exit from the sqlcmd utility.

Read More..  [8 clicks]


Paresh Prajapati
7 · 24% · 5511
10
 
4
 
 
0
Incorrect
 
0
Interesting
 
0
Forgotten
 
0
Move



Submit

3  Comments  

  • Instead of writing the query in the command line, I would prefer to put it into a stored procedure or in a .sql file and pass it into sqlcmd

    commented on Nov 9 2011 10:24AM  .  Report Abuse This post is not formatted correctly
    Jacob Sebastian
    1 · 100% · 22500
  • In a more modern way, it is possible to use a script inside PowerShell, with 2 problems : - the requirement to know SMO ( SQL Server Management Objects ) - the obligation to check whether the classes have not been moved from a namespace to another one ( it is what has happened for backup/restore ) An advantage, you can script the generated statements ( to be sure that you have done no errors ) PowerShell seems to be used mainly by System Administrators since the release of Windows Server 2003.Be careful the V2 version is really pleasure in comparison of the 1.0 version. But this article was extremely interesting, it permits me to review some T-SQL keywords i am using scarcely ( the q parameter or Replace )

    commented on Nov 10 2011 4:43PM  .  Report Abuse This post is not formatted correctly
    Patrick Lambin
    241 · 1% · 129
  • I find that it's better to split this into two parts. Firstly a batch file that is called by the scheduler and secondly a .sql file that is processed by the batch file.

    The reason for this is that it gives you a lot more flexibility, it's easier to read, and you can make the .sql file back up multiple databases easily without running up against the command line character limit.

    I also recommend that you put 'with init' into the SQL command. Without this, the backups append to the file and it can quickly grow very large.

    I create a SQL Login with only the db_backupoperator role mapped to each database to be backed up.

    Here's an example:


    Sqlbackup.bat file contents:

    @echo off

    Rem Backup batch file created by Phil Haddock March 3rd 2008

    Rem Change the variables below to suit your database

    Rem Also need to change the variables in the sqlbackup.sql file

    Rem Enter SQL Server name on next line nb. Server name is CASE SENSITIVE

    set server=server\InstanceName

    Rem Enter location of sqlbackup.sql script file on next line

    set BackScript=C:\temp\sqlbackup.sql

    Rem Enter Username and password with rights on the sql server.

    Rem If someone sees this batch file they will have a sql login and password so recommend creating a limited login

    set UserName=TESTBackupUser

    set PWD=amt

    echo Server = "%server%"

    echo BackScript = "%backscript%"

    echo Username = "%username%"

    echo PWD = "%pwd%"

    @echo on

    sqlcmd -S %server% -U %UserName% -P %PWD% -i %BackScript%


    sqlbackup.sql contents:

    declare @DBName as varchar(80) declare @BackupPath as varchar(80)

    -- Enter the database name to be backed up and the location to store the backup files

    -- Repeat the next three lines for each database if there is more than one to be backed up.

    set @DBName = 'TestDatabase'

    set @BackupPath = 'c:\temp\SQLBackup\TestBackup.bak'

    backup database @dbname to disk = @backuppath with init


    commented on Nov 17 2011 1:02AM  .  Report Abuse This post is not formatted correctly
    PhilHaddock
    2304 · 0% · 2

Your Comment


Sign Up or Login to post a comment.

"Perform database backup using SQLCMD utility in SQL Server" rated 5 out of 5 by 10 readers
Perform database backup using SQLCMD utility in SQL Server , 5.0 out of 5 based on 10 ratings
    Copyright © Beyondrelational.com Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising