Automatic SQL Server backup

Automatic SQL Server backup solution and also how to back it up online with SecureBackup. I am using Microsoft SQL Server 2008 Express but you should be able to use these instructions for all versions of Microsoft SQL Server 2005 and Microsoft SQL Server 2008. In my machine, the database files are located in the following directory:

D:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA

As you may already know, you cannot backup the *.MDF and *.LDF database files directly because they are attached to the SQL Server. You could detach them, back them up, and then re-attach them but then your database will be offline for a period of time.  To solve this problem, we first need to create a SQL file that can be used later to automate our database backup. Here’s how we can do that:

* Create an SQL file here:-
1. Login to Microsoft SQL Server Management Studio.
2. Right click on your database from the “Databases” node.
3. Select “Tasks” -> “Back up…”
4. You are now presented with the “Back up Database” window. In this window you can specify your backup settings. At a minimum, you need to create a destination for your backup. Click the “Add” button and specify a full path name for your database backup in the “File name” field. You will need to use this path name later, so let’s take note of it.
5. You can choose any other backup options that are important to you. Over on the left hand side is “Options”. I would click on that and decide whether you want to use “append to an existing backup set” or “overwrite all existing backup sets”.
6. Once you are finished with your settings, you need to create an SQL file which will be used later for the automated database backup. To create that SQL file, click on the arrow next to “Script” at the top of the window. There will be an option for “Script action to File”. Go ahead and select that option. You will then be able to save your SQL Server Script File. Take note of where you have saved this file as well.

* Create a batch file here:-
Now that we have created the SQL file, we need to create a batch file that can be automatically executed by the Task Scheduler. To create this file, do the following:

1. Now here Open Notepad and enter the following:
sqlcmd -S .\SQLEXPRESS -i “C:\Users\Administrator\Documents\Backup.sql” Of course, substitute the pathname with the pathname of where you saved your SQL file.
2. Save this file and take note of where you saved it.
3. Now, using Windows Explorer, go to the directory of where you created this file and rename it to have a .bat filename extension instead of .txt.

* Set Batch file to run in Windows Task Scheduler:-
We are now ready to add the batch file to the Task Scheduler. Rather than using the Task Scheduler GUI, I prefer to do it on the command line. You can open a command prompt as Administrator or if you are already running as Administrator, you can use Start -> Run. Either way, enter the following:

schtasks /create /sc Daily /st 011:00:00 /tn “Addarr_Task” /tr “cmd /c E:\Users\Administrator\Desktop\Backup.bat”

Again, substitute the pathname above with that of your batch file. Also, set the time specified to something that is desirable for you. In the above example, we run this task at 11:00 AM.

Set SecureBackup to Backup your Database:-

The time specified in the Task Scheduler step above is important because you need have SecureBackup automatically backup the database file after the task is complete. Since the backup took place at 11:00, you can setup SecureBackup to run the backup at say, 3:00 AM. This way you will have the most recent backup.

From within the SecureBackup software, you need to create or edit an existing  Backup Job and include the location of the backup file that you chose in step 4 of “Create and SQL File” above. If the file isn’t listed and you can’t select it from within SecureBackup, it’s because Windows Task Scheduler has not run your batch file yet. To run the batch file, simply double click on it from within Windows Explorer. It may take anywhere from 10-20 seconds to, possibly, a few hours depending on how large your database is. Once complete, you will have your database backup file and can select that file from within SecureBackup. You will also be able to schedule the Backup Job around how long it took for that batch file to complete.

You now have an automated backup solution for your Microsoft SQL database complete with a secure online storage solution.

Note: You can be able to use these instructions for all versions of Microsoft SQL Server 2005 and Microsoft SQL Server 2008.


SQL & SharePoint – MCTS




Tagged: , , , , ,

%d bloggers like this: