Login Register
Code2night
  • Home
  • Guest Posts
  • Blog Archive
  • Tutorial
  • Languages
    • Angular
    • C
    • c#
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • Security
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
    • SEO Analyzer
  1. Home
  2. Blog
  3. Batch Script for Creating Database backups from Sql server

Batch Script for Creating Database backups from Sql server

Date- Apr 30,2022

11123

Batch Script Database backup

Batch File (.bat)

We can use a .bat file to write script that can automate database backup from server.You can take one text file and write this script

@ECHO OFF
CLS
REM SET VARIABLES VALUES 

SET SqlServer=Code2night\SQL2017
SET InstanceName=Code2night\SQL2017
SET Username=sa
SET Password=Admin123

SET BaseFolderPath=C:\DatabaseScript\DatabaseScriptBackup
SET TodayDate=%DATE:/=%
SET SubFolder=%Backup%
SET LocalFolder=%BaseFolderPath%\%SubFolder%
SET SourceFile=C:\DatabaseScript\Backup
SET limit = (Get-Date).AddMinutes(-1)
SET Databases=%LocalFolder%\Databases.txt
IF NOT EXIST %LocalFolder% mkdir %LocalFolder%

:: *****************************************************************************

REM date
set mydate=%date:~7,2%%date:~4,2%%date:~10,4%

REM hour
set hour=%time:~0,2%
if "%hour:~0,1%" == " " set hour=0%hour:~1,1%

REM min
set min=%time:~3,2%
if "%min:~0,1%" == " " set min=0%min:~1,1%

REM secs
set secs=%time:~6,2%
if "%secs:~0,1%" == " " set secs=0%secs:~1,1%

ECHO DB Backup finished...

REM SETLOCAL
setlocal EnableDelayedExpansion

REM SAVE DATABASES LIST IN TEMP FILE
SqlCmd -S %SqlServer% -U %Username% -P %Password% -Q "SET NoCount ON; SELECT Name FROM master.dbo.sysDatabases WHERE [Name]  IN ('ALISTrucking','ALISTruckingClean','ALISTruckingPreProd')" > "%Databases%"

REM SAVE BACKUP OF EACH DATABASE

FOR /F "tokens=* delims= " %%I IN (%Databases%) DO (
 
ECHO Backing up database: %%~nI 

REM SAVE BACKUP OF EACH DATABASE into folder with DDMMYYHHMMSS
sqlcmd -S %SqlServer% -U %Username% -P %Password%  -Q "BACKUP DATABASE %%~nI TO  DISK = '%LocalFolder%\%%~nI_%mydate%%hour%%min%%secs%.bak' WITH NOFORMAT, INIT, SKIP, NOREWIND, NOUNLOAD,  STATS = 10;"

ECHO.)

REM DELETE TEMP DATABASES LIST
IF EXIST "%Databases%" DEL /F /Q "%Databases%"

ENDLOCAL

ECHO DB Backup finished...

 

SqlServer: This must be the sql server instance name to connect to your sql server.

UserName: This must be the sql server Login usernname to connect to your sql server database.

Password: This must be the sql server login password to connect to your sql server.

BaseFolderPath: This must be the location of folder where you want your database backups to be saved.

Executing Batch Script

AFter adding the script you have to save the file as .bat extension. And then you have to double click on the file to save the script. After that go to the location and check if you have your database backups there.

So, this is how you can create Batch Script for Creating Database backups from Sql server

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

How to refund payment using Paypal in Asp.Net MVC
Jan 30, 2024
How to Use Stored Procedures with Parameters in Dapper
Jan 23, 2024
Repopulating and Reselecting same Row in DevExpress Grid
Jan 19, 2024
Get random number in asp.net C#
Dec 23, 2023

Comments

Contents

More in SQL Server

  • How to create a read-only MySQL user 10189 views
  • How to Connect to a Database with MySQL Workbench 7544 views
  • How to find all procedures having table reference in Sql ser… 6886 views
  • How to find all tables by column name 6483 views
  • Converting commas or other delimiters to a Table or List in … 6384 views
View all SQL Server posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
  • SEO Analyzer
By Language
  • Angular
  • C
  • c#
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page