Click here to Skip to main content
15,887,930 members
Articles / Operating Systems / Windows
Tip/Trick

SQL Server Database Backup by using Batch File (Updated Version)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
15 Feb 2014CPOL2 min read 23.6K   630   4   3
This tip briefly explains how to use batch file to do SQL data backups.

Introduction

I’m writing this tip to explain a simple way to make a SQL Database Backup Batch File. I already posted 2 articles to do SQL Server Database backups by using Batch file. But I could encountered a few difficulties. Therefore, now I’m coming up with a new solution. And also, this is a simple solution too.

My Previous Articles  

Background

My last 2 articles (check the URLs of the instructions) are a little bit complex and sometimes, they won’t work well (they have dependencies). Therefore I came up with a new solution.

Using the Code

Before you wish to use my database backup batch file, please run (double click on) Date_Time.bat file. This batch file will help you to look into your computer (Server computer of the database) date and time formats. This is a very important thing, therefore please check your date and time format and keep a note about its’ formats.

Image 1

Figure 01

Please check Figure 01, you can see Date and Time Formats there. Please note it, because these formats are using by SQL Database Backup Script Batch File.

Let’s go to the SQL Database Backup Batch Script.

Image 2

Figure 02

In Figure 02, the code segment I’m trying to create TestDB_Data_Backup folder is in E:\ drive of the computer. You can change the drive.

Set Date Format:

Image 3

Figure 03

Let’s refer to Figure 03, you can set Date format of the backup file name.

Image 4

Figure 04

Now look at Figure 04.

Please check your Date Format and change the code if it has a different format.

Set Time Format:

Image 5

Figure 05

Let’s refer to Figure 05, you can set the Time format of the backup file name.

Image 6

Figure 06

Now look at Figure 06. Time format is “HH:MM:SS.milsec”

Please check your Time Format and change the code if it has a different format.

Coding again!!!

Image 7

Figure 07 

In Figure 07, I set backup file folder path.

Image 8

Figure 08

I already commented everything in the code. Therefore, I have nothing to explain here.

I hope this tip will help you to do a simple backup process.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems / Hardware Administrator
Sri Lanka Sri Lanka
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questiongood working Pin
warzer23-Jan-20 10:59
warzer23-Jan-20 10:59 
QuestionOr simpli use DOFF.EXE for getting formatted datetime Pin
Member 399871318-Feb-14 5:25
Member 399871318-Feb-14 5:25 
AnswerRe: Or simpli use DOFF.EXE for getting formatted datetime Pin
Charitha Athukroala18-Feb-14 18:03
Charitha Athukroala18-Feb-14 18:03 
Thanks for the comment, actually I didn't know about doff command.Thumbs Up | :thumbsup: And I used Date_Time.bat to view the date & time formats of the server computer. That format is very useful to execute the backup batch file without errors.

Because the final backup file is using "database name_date format(DD-mm-yyyy)_time format(hh-mm-sec).bak".

Example :-
The date format can be different from PC to PC. As a result of this some of them will give "\" sign instead of the DD-mm-yyyy formatted value. If that happens the system will get this "\" as a path, as a result of that you can see an error. To avoid this problem I used the simple batch file(Date_Time.bat) to get the correct date & time format.

Thanks
Charitha

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.