Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / operating-systems / Windows

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

5.00/5 (1 vote)
15 Feb 2014CPOL2 min read 24.3K   639  
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)