Technical Article

Creating BCH Files for Veritas Direct to Tape

,

This script was written to solve a problem where we needed to dynamically create bch files for Veritas Backups direct to tape backups.  We needed to parse the sysdatabases daily and create the bch files that veritas would use to backup each server and database.  This SQL Script uses xp_cmdshell to call a windows shell script called runit.cmd.  I will include both scripts.
The interesting thing for me was passing the @dataname variable to the external script as the first argument in the command line.  I can think of many other applications that I can use this method for.  I also know that in SQL 2005 xp_cmdshell is locked down so there are steps there that need to be done in order to use this set of scripts in SQL 2005.

SQL SCRIPT
--*******************************************
SET QUOTED_IDENTIFIER ON

DECLARE @fillfactor varchar(2)
DECLARE @tablename varchar(30)
DECLARE @tablename_header varchar(75)
DECLARE @dataname varchar(30)
DECLARE @dataname_header varchar(75)
DECLARE @statement varchar(75)
DECLARE datanames_cursor CURSOR FOR SELECT name FROM sysdatabases
        WHERE name not in ('master', 'pubs', 'tempdb', 'model', 'northwind')
DECLARE @instance varchar(20)
DECLARE @sInstance varchar(75)
DECLARE @CleanBCH varchar(20)

set @instance = 'jf4csql202'

set @CleanBCH = 'del /q c:\temp\bch\*.*'
exec xp_cmdshell @CleanBCH, no_output

OPEN datanames_cursor
FETCH NEXT FROM datanames_cursor INTO @dataname
WHILE (@@FETCH_STATUS <> -1)
BEGIN
   IF (@@FETCH_STATUS <> -2)
   BEGIN 
       PRINT @dataname
      --*****************************************************
      --PUT YOUR WORKING CODE HERE TO CREATE THE BACKUP FILE
set @statement = 'touch.exe >> c:\temp\bch\' + @dataname +  '.bch'
exec xp_cmdshell @statement , no_output
set @sInstance = 'c:\temp\bchconfig\runit.cmd ' + @dataname 
exec xp_cmdshell @sInstance , no_output

      --*****************************************************     
   END
      FETCH NEXT FROM datanames_cursor INTO @dataname
  END

CLOSE datanames_cursor
DEALLOCATE datanames_cursor

RUNIT.CMD
--*****************************************
echo OPERATION BACKUP >> c:\temp\bch\%1.bch
echo DATABASE %1 >> c:\temp\bch\%1.bch
echo SQLHOST "jf4csql202" >> c:\temp\bch\%1.bch
echo NBSERVER "JF4SBAK0200" >> c:\temp\bch\%1.bch
echo SQLINSTANCE "jf4csql202" >> c:\temp\bch\%1.bch
echo MAXTRANSFERSIZE 0 >> c:\temp\bch\%1.bch
echo BLOCKSIZE 7 >> c:\temp\bch\%1.bch
echo POLICY jf4csql202 >> c:\temp\bch\%1.bch
echo BROWSECLIENT "jf4csql202nb" >> c:\temp\bch\%1.bch
echo ENDOPER TRUE >> c:\temp\bch\%1.bch

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating