Automated Database Backups and Restoration
database server is the heart of information storage for the enterprise, so
special care must be taken in managing this critical resource. In case of a
problem, it must be addressed quickly, efficiently, and correctly, or an
organization stands to suffer critical losses.
the environments I have worked in that I might have taken lightly, backups were
obviously not it. In my opinion, database backups hold second place in
importance only to the actual data itself. With this mindset, I am adamant
about making sure that backups are done properly and on a regular schedule.
not interesting, but as a DBA, performing backups is one of the most important
tasks. To wipe off this load of daily manual backups and restoration from
multiple servers to the Data Recovery server, I created an automated process
for the same and the sole purpose of writing this tutorial is to help all you
people who daily bang their heads in this arena.
the most interesting things is that you can run the DOS commands from within
your SQL Server. When I say running DOS commands, it obviously includes running
batch files too.
For the process of automatic backups and restorations, create two folders
on your “C” drive and name them “DataBackup” and
“DBRepository”. The “DataBackup”
folder will be used as a folder where initial backup comes, whereas, the “DBRepository” folder will be used for storing the
secondary backup. The “DBRepository” folder can
also be created on another drive also, if exists.
In my case I created the “DBRepository” folder
in my “D” drive.
- Winzip Application
of “Winzip 10.0” or higher is required if the
requirement is such that you need to restore the database on some other
server (such as a Fall Back Server). Though you can simply FTP the backup
file on the DataRecovery server, but if the size
of the backup file is large, you should zip the backup file before FTP’ing it to the other server. It will always be
above mentioned file and install it. This file will enable running the zip
command from the command prompt and so from the batch file. Remember that
you must have the licensed version of winzip
10.0 or higher installed on your server or otherwise it will not work.
You can download this file from the following location:
need to create a batch file with the following lines of code in it:
"c:\program files\winzip\wzzip.exe" DBFull.zip
"c:\program files\winzip\wzzip.exe" DBDiff.zip
move *.bak D:\DBRepository
Name the batch file as “Push.bat” and save it in
the “DataBackup” folder. This is the folder
where the automated backup initially comes.
The above written lines of codes are simply the normal DOS commands, which
you must be familiar with.
In my case, I have named the complete backup as “sg_complete_backup.bak”
and the differential backup as “sg_differential_backup.bak”.
Also, the complete backup will be zipped as “DBFull.zip”
and the differential backup will be zipped as “DBDiff.zip”.
I have created a single batch file that works for both the complete as
well as the differential backup sets.
Note: You need to be cautious
while naming the files as this is an automated process and changing the
names at one would require changes
at all the places where the
If you look
at the code above in the batch file you just created, you will see that
the batch file opens the “AccessFTP.txt” file for FTP’ing
the zip file to the DataRecovery server. So, now
create a text file and save it as “AccessFTP.txt” in the “DataBackup” folder along with “Push.bat”.
Write the following lines inside this file:
<<ip address of the Data Recovery Server>>
<<Windows logon UserName of the Data
Recovery Server >>
<<Windows logon Password of the Data Recovery Server >>
cd <<folder where you want to FTP the zip
file on the Data Recovery Server >>
Let me explain the above lines one by one.
- open – This will ask you the ip address of the machine to connect.
- <<ip address of the Data Recovery Server>>
- This is the IP of the Data Recovery Server.
- <<Windows logon UserName of the Data Recovery Server >> -
Windows Username of the data Recovery Server.
- <<Windows logon
Password of the Data Recovery Server >> - Windowws
Password for the Data Recovery Server.
- cd <<folder where you want to FTP the zip file on the Data
Recovery Server >> - This may be the FTP root folder or a folder
within the configured FTP folder on the Data Recovery Server.
- Put "C:/DataBackup/DBFull.zip" – This will put the
complete backup set from the current server to the folder mentioned above
on the Data Recovery Server.
- Put "C:/DataBackup/DBDiff.zip" – This is the same as
above, but is valid for the differential backup set.
- Quit – This will quit the
Now, after all
the above mentioned pre-requisites have been met, it is time for us to really
dig deep into the implementation part. All must be aware that for the
schedulers to run, the SQL Server Agent must be in the Start state, i.e., it
must be running.
need to make an automated process; we will schedule the complete process as a
job, so as to run it as per the time set by us. For this, the SQL Server Agent
must be running.
the steps below to create a job that takes the complete backup, zips the backup
file and FTPies it to the Data Recovery Server. Also,
the steps include the commands that run the Job created on the Data Recovery
- Step 1
- Expand the SQL Server Agent to
see the Jobs.
- In the wizard that appears,
give the name to the scheduler by writing the name inside the text box in
front of the Name.
- Step 2
- Click on the steps Tab and
then click on the New button to create a new
- Enter the step name in the
text box. In the Database name drop down list box, leave the database
name as master.
The sole purpose of leaving the database name as master is that we are
going to use the extended procedures provided in the master database for
our entire task.
- Write the code in the text
box provided in front of the label “Command”. Note that the path is
mentioned as “c:\DataBackup\sg_complete_backup.back” for taking the
backup. It is the same path in which we had created the batch file as
well as the text file.
- Replace the name of the
database from SG with the name of your database. Also, you need to change
the name of the backup file to the name you have changed in the batch
file created above.
- Click on the Parse button to
ensure that the command written has no errors. Click on the Apply button
and then click on OK to exit this dialog box.
- It is now time to create the
second step of the job. This step will utilize the extended procedure “xp_cmdshell” of the master database to run the DOS
commands written within the batch file created above.
This step is mainly responsible for creating the zip file of the backup
and also to do FTP to the desired Data Recovery Server.
Note that again I have kept the default name of the Database server as
- Parse to check out for
errors, apply and click OK to exit this step.
- Now create a new step, which
starts the job of unzipping and restoring the database on the remote Data
Since there are a few more lines of code in this step, the screen shot
does not show the complete code. The complete code for the same is,
therefore, written below the image.
declare @query varchar(8000)
declare @cmd varchar(8000)
= '<<This is the job name of the Data Recovery Server, which we want to
run for unzipping and Restoring the database>>' ------------------Job
name goes here.
= '<<IP address of the Data Recovery Server>>'
------------------Server name goes here.
set @query = 'exec msdb.dbo.sp_start_job @job_name =
''' + @job_name + ''''
set @cmd =
'osql /U <<UserName
of the Database>> /P <<Password for the database>> -S ' + @server_name + ' -Q "' + @query + '"'
= master.dbo.xp_cmdshell @cmd