<

Go Premium for a chance to win a PS4. Enter to Win

x

Batch File to Perform Backup of MSSQL Database

Published on
5,976 Points
2,976 Views
Last Modified:
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and will then append that to the file name.
Background:
Microsoft SQL Server Express editions do not have a built-in mechanism for performing scheduled backups.  By using the command line utility "SqlCmd", it is possible to use this functionality in a Windows batch file.  

By default, the local SQL Server accounts do not have access to network shares.  Therefore, it is necessary to backup to a local folder and then move the backup file to a network share location.  This means that a folder on the local hard drive must exist that has write privileges granted to the user account that runs the SQL Server instance service.  This is usually the "Local System" account or another user account that has been manually configured to run the service.

Prerequisites:  
You will need to know the following information before proceeding.
 
  • SQL Server name
  • Service instance name for SQL Server (usually MSSQLSERVER or SQLEXPRESS)
  • Name of the database to be backed up
  • Username and password with backup privileges in SQL Server
  • Path to an existing local folder to which SQL has write privileges
  • Path to an existing remote folder to which the file will be moved.


Procedure:  
1. Open Notepad (or any text editor)

2. Paste the following code into the text editor
 

@echo off
cls

rem Set these variables to the desired values

set SqlServer=SERVER_NAME (The name of your SQL server)
set InstanceName=MSSQLSERVER (The name of the SQL instance)
set Username=SQL_USERNAME (The username with backup rights)
set Password=SQL_PASSWORD (The password for the above user)
set Database=DATABASE_NAME (The database to be backed up)
set LocalFolder=C:\Temp (Temporary local folder to hold backup, no quotes)
set NetworkFolder="\\remotecomputer\folder" (URL to permanent backup location, include quotes)




rem ************************************
rem * Don't touch anything below here. *
rem ************************************

echo Getting current date and time...
echo.
for /f %%a in ('sqlcmd -S %SqlServer% -U %Username% -P %Password% -Q "SET NOCOUNT ON select ltrim(convert(date, getdate()))" -h -1') do set CurrentDate=%%a
for /f %%a in ('sqlcmd -S %SqlServer% -U %Username% -P %Password% -Q "SET NOCOUNT ON select right('00' + ltrim(datepart(hour, getdate())), 2)" -h -1') do set CurrentHour=%%a
for /f %%a in ('sqlcmd -S %SqlServer% -U %Username% -P %Password% -Q "SET NOCOUNT ON select right('00' + ltrim(datepart(minute, getdate())), 2)" -h -1') do set CurrentMinute=%%a
for /f %%a in ('sqlcmd -S %SqlServer% -U %Username% -P %Password% -Q "SET NOCOUNT ON select right('00' + ltrim(datepart(second, getdate())), 2)" -h -1') do set CurrentSecond=%%a

echo.
echo Backing up database to %LocalFolder%
echo.
SqlCmd -S %SqlServer% -U %Username% -P %Password% -Q "Backup Database %Database% To Disk='%LocalFolder%\%Database%-%CurrentDate%_%CurrentHour%%CurrentMinute%%CurrentSecond%.bak'"

echo.
echo.
echo Copying backup to %NetworkFolder%
echo.
move /Y %LocalFolder%\%Database%-*.bak %NetworkFolder%

Open in new window


3. Save the file with an extension of .bat to ensure that it is a batch file and not a plain text file. 

Note:  The icon should change to a command prompt.  If not, the file extension is probably .bat.txt, which means that you will need to display file extensions to remove the .txt extension.

4. To perform the backup, double-click on the new batch file.  You will see it perform some queries and then it will export the data to a file.  

Note:  The amount of time to perform the backup may take a while depending on the size of the database.

After making sure that the batch file works as expected, you can add it as a scheduled task in the Windows "Task Scheduler" to have it run daily at a desired time.


This procedure is complete.
0
Comment
1 Comment
 

Expert Comment

by:Shaad Parihar
The above code is for one database.
If i have multiple databases and i want to use a single batch file.how i add multiple database in the code.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Join & Write a Comment

This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month