Click here to Skip to main content
15,892,809 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 
http://www.jfitz.com/dos/#DOFF

DOFF

DOFF prints a formatted date and time, with an optional date offset, (e.g -1 prints yesterday's date, +1 prints tomorrow's date). To view all the options available, execute "doff -h". I typically use this utility for renaming log files so that they include a timestamp, (see the third example below). This code should compile under Unix/Linux, as well as DOS.

Sample commands:

C:\>doff
19991108131135

With no parameters the output is the current date/time in the following format: yyyymmddhhmiss

C:\>doff mm/dd/yyyy
11/08/1999

In the above example a date format specification is given.

@echooff
for /f "tokens=1-3 delims=/ " %%a in ('doff mm/dd/yyyy -1') do (
set mm=%%a
set dd=%%b
set yyyy=%%c)
rename httpd-access.log httpd-access-%yyyy%%mm%%dd%.log

The sample batch file above shows a neat way to rename a log file based on yesterday's date. The "for" command executes doff to print yesterday's date, (the "-1" parameter specifies yesterday), then extracts each component of the date into DOS batch file variables. The "rename" command renames "httpd-access.log" to "httpd-access-[yesterday's date].log"

DOFF HELP

c:\> doff -help

doff - Prints a formatted date string offset by a specified time interval
syntax is: doff [format_str [offset_str [exclude_format_str]]]
or: doff -h (displays this help screen)

format_str, (case sensitive), may include any of the following:
yyyy - replaced by four digit year in output string
yy - replaced by two digit year in output string
mm - replaced by month (1 - 12) in output string
dd - replaced by day (1 - 31) in output string
hh - replaced by hour (0 - 23) in output string
mi - replaced by minute (0 - 59) in output string
ss - replaced by second (0 - 59) in output string (leap seconds = 59)
Other characters in format_str are returned as-is

offset_str, (case sensitive), is specified as [+/-]n[smhd]
"n" is the offset value
offset qualifiers are: s = seconds, m = minutes, h = hours, d = days

exclude_format_str has the same format as format str.
Patterns in exclude_format_str that would normally be translated are ignored.
For example, including "ss" in exclude_format_str means "ss" will appear
untranslated in the output string.

Default offset value is zero, default offset qualifier is days
Bad offset values default to zero, bad offset qualifiers default to days
Note: -10xd will evaluate to "10 days ago", (because atoi("-10x") = -10)
but -x10d will evaluate to "now", (because atoi("-x10") = 0)

When both format_str and offset_str are omitted the output format is:
yyyymmddhhmiss
AnswerRe: Or simpli use DOFF.EXE for getting formatted datetime Pin
Charitha Athukroala18-Feb-14 18:03
Charitha Athukroala18-Feb-14 18:03 

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.