Translate to your Language

Monday, October 4, 2010

DataStageBackup Dos Script

by Unknown  |  in Datastage at  6:01 PM

@echo off
:: -----------------------------------------------------------------
:: DataStageBackup.bat
:: -----------------------------------------------------------------
:: This batch script is used to backup all the projects on a DataStage server.  It
:: must be run from a DataStage client machine and the parameters below should be
:: modified to fit your environment.  Use of parameters was avoided to simplify backup
:: allow the command to be customized to a particular environment.
::
::    Wouldn't it be nice if there is a way to 'recover' from a failed project
::    extraction, record it, and continue to the next project?  Right now, the
::    FOR loop is not capable enough.
::
::    Wouldn't it be nice if the %ERRORLEVEL% within the for loop works?
::
::    Wouldn't it be nice to have a freeware compression program that is
::    command-line specific and is not restricted to the 8.3 DOS naming
::    convention?
:: -----------------------------------------------------------------
:: Required Components:
::     dsjob.exe (Windows Version)
::         (also requires vmdsapi.dll)
::         Advised to place both files under Windows System directory.
:: -----------------------------------------------------------------
:: NOTE:  FOLLOWING FEATURE IS DISABLED
:: -----------------------------------------------------------------
:: Optional Components:
::     Compression Program
::         Suggested:  gzip.exe for MSDOS (Free)
:: -----------------------------------------------------------------
:: Special Note:  As of this time, within Windows NT, it is NOT possible to
:: continue with the next project after a failed extraction unless we are to
:: create another file to handle the logic behind this.  If you know how to
:: get around the requirement of using FOR, PLEASE let us know.
:: Also Using REM instead of :: are required within a FOR loop.
:: -----------------------------------------------------------------
:: Host is server name
:: User is username to use to attach to DataStage
:: Password   is password to use to attach to DataStage
:: BackupDir is the directory to place the backed up project in
:: CompTog (1 = Yes, 0 = No) determine whether to compress DSX
:: DSExportCmd is directory of the export command on client
:: GetProjList is the dsjob command to retrieve the project list
:: ProjectList is temp file to store project names
:: BadProjects stores all bad projects
:: GoodProjects stores all good projects
:: DSLog is the name of the log file accumulated during the backup
:: CompScri is the compression program and its options needed to compress
::     dsx.  Assuming that only the name of the DSX is needed to make
::     a compressed file.
:: -----------------------------------------------------------------
:: Ensure that everything that are set here are not permanent.
:: -----------------------------------------------------------------
    SETLOCAL
:: -----------------------------------------------------------------
:: Test to ensure that command line is provided semi-correctly.
:: -----------------------------------------------------------------
    IF "%1"=="" GOTO Syntax
    IF "%2"=="" GOTO Syntax
    IF "%3"=="" GOTO Syntax
    IF "%4"=="" GOTO Syntax
    IF "%5"=="" GOTO Syntax
    IF NOT ".%6"=="." GOTO Syntax
::    IF NOT ".%5"=="." GOTO Syntax
:: -----------------------------------------------------------------
:: Supply values to paramters.
:: -----------------------------------------------------------------
    SET Host=%1
    SET User=%2
    SET Password=%3
:: kim    SET BackupDir=%4
    SET BaseDir=%4
::    SET CompTog=%5
:: -----------------------------------------------------------------
:: Hard-coded values.  Dependent on each computer.
:: -----------------------------------------------------------------
:: kim    SET BaseDir=c:\DataStage\KimD\Backups
:: kim    SET ProgDir=c:\DataStage\KimD\Scripts
:: C:\Program Files\Ascential\DataStage7.5.1
    SET ProgDir=%BaseDir%\Scripts
    SET BackupDir=%BaseDir%\Backups
::    SET DSExportCmd=C:\Progra~1\Ascential\DataStage7.1\dscmdexport.exe
::    SET DsxJob=C:\Progra~1\Ascential\DataStage7.1\dsjob.exe
    SET DSExportCmd=C:\Progra~1\Ascential\DataStage7.5.1\dscmdexport.exe
    SET DsxJob=C:\Progra~1\Ascential\DataStage7.5.1\dsjob.exe
:: kim    SET ProjectList=c:\Temp\ProjListDev.txt
    SET ProjectList=%ProgDir%\%5
    SET BadProjects=%ProjectList%BAD
    SET GoodProjects=%ProjectList%GOOD
    SET DSLog=BackupLog
::    SET CompScri=gzip --name -3
:: Kim debug
::    ECHO Host=%Host%
::    ECHO User=%user%
::    ECHO DSExportCmd=%DSExportCmd%
    ECHO ProjectList=%ProjectList%
::    ECHO DSLog=%DSLog%
:: -----------------------------------------------------------------
:: Get the current Date
:: -----------------------------------------------------------------
    FOR /f "tokens=2-4 delims=/ " %%a in ('DATE/T') do SET DsxDate=%%c%%a%%b
:: -----------------------------------------------------------------
:: Get the current Time
:: -----------------------------------------------------------------
    FOR /f "tokens=1* delims=:" %%a in ('ECHO.^|TIME^|FINDSTR "[0-9]"') do (SET DsxTime=%%b)
:: -----------------------------------------------------------------
:: Set delimeters so that current time can be broken down into components
:: then execute FOR loop to parse the DsxTime variable into Hr/Min/Sec/Hun.
:: -----------------------------------------------------------------
    SET delim1=%DsxTime:~3,1%
    SET delim2=%DsxTime:~9,1%
    FOR /f "tokens=1-4 delims=%delim1%%delim2% " %%a in ('echo %DsxTime%') do (
        set DsxHr=%%a
        set DsxMin=%%b
        set DsxSec=%%c
        set DsxHun=%%d
    )
:: -----------------------------------------------------------------
:: If provided directory is missing an ending \, append it.
:: Validate %BackupDir%'s existance.
:: -----------------------------------------------------------------
    if exist %BackupDir%\ set BackupDir=%BackupDir%\
    if NOT exist %BackupDir% GOTO BadMain
:: -----------------------------------------------------------------
:: Set the log file name to improve readability of code.
:: -----------------------------------------------------------------
    SET LogFileName=%BackupDir%%DSLog%-%DsxDate%-%DsxHr%_%DsxMin%_%DsxSec%.log
:: -----------------------------------------------------------------
:: Announce to log of this program's run.
:: -----------------------------------------------------------------
    ECHO. > %LogFileName%
    ECHO DataStage Backup ran on %DsxDate% %DsxHr%:%DsxMin%:%DsxSec% with the following parameters >> %LogFileName%
    ECHO Host=%Host% >> %LogFileName%
    ECHO User=%user% >> %LogFileName%
    ECHO BackupDir=%BackupDir%%DsxDate%\ >> %LogFileName%
    ECHO CompTog=%CompTog% >> %LogFileName%
    ECHO CompScri=%CompScri% >> %LogFileName%
    ECHO DSExportCmd=%DSExportCmd% >> %LogFileName%
    ECHO DsxJob=%DsxJob% >> %LogFileName%
    ECHO ProjectList=%ProjectList% >> %LogFileName%
    ECHO DSLog=%DSLog% >> %LogFileName%
    ECHO. >> %LogFileName%
:: -----------------------------------------------------------------
:: Pull project information (and validate parameters).
:: -----------------------------------------------------------------
:: kim     %DsxJob% -server %Host% -user %user% -password %Password% -lprojects > %ProjectList%
:: kim     IF NOT %ERRORLEVEL%==0 GOTO BadParam
:: -----------------------------------------------------------------
:: Report valid projects to log.
:: -----------------------------------------------------------------
    ECHO Projects found on %Host%: >> %LogFileName%
    type %ProjectList% >> %LogFileName%
:: -----------------------------------------------------------------
:: Create subdirectory within Backup Directory
:: Validate ability to create %BackupDir%%DsxDate%\.
:: -----------------------------------------------------------------
    if exist %BackupDir%%DsxDate%\ GOTO DirCont
    ECHO ***  Creating: %BackupDir%%DsxDate%\
    mkdir %BackupDir%%DsxDate%\
    IF NOT %ERRORLEVEL%==0 GOTO BadDir
    ECHO. >> %LogFileName%
    ECHO ***  Created: %BackupDir%%DsxDate%\ >> %LogFileName%
    ECHO. >> %LogFileName%
:: -----------------------------------------------------------------
:DirCont
:: -----------------------------------------------------------------
    ECHO.
    ECHO Backing up %Host% ...
    ECHO.
:: -----------------------------------------------------------------
:: Begin Backup Loop
:: -----------------------------------------------------------------
::
:: NOTE:  %ERRORLEVEL% does not work for some reason.
::
:: -----------------------------------------------------------------
    for /F "tokens=1" %%i in (%ProjectList%) do (
        ECHO Backing up Project %%i
        %DSExportCmd% /H=%Host% /U=%User% /P=%Password% %%i %BackupDir%%DsxDate%\%Host%_%%i.dsx >> %LogFileName%
        IF NOT %ERRORLEVEL%==0 GOTO ProjFail
        ECHO. >> %LogFileName%
        ECHO *** Completed Export for Project: %%i on Host: %Host% >> %LogFileName%
        ECHO     to File: %BackupDir%%DsxDate%\%Host%-%%i.dsx >> %LogFileName%
        ECHO. >> %LogFileName%
    )
    ECHO *** Export completed successfully for projects:
    type %ProjectList%
:: -----------------------------------------------------------------
:: Run Compression on DSX if toggled.
:: DISABLED
:: -----------------------------------------------------------------
::    IF %CompTog%==0 GOTO EXITPT
::    ECHO Compressing DSX files...
::    for /F "tokens=1" %%i in (%ProjectList%) do %CompScri% %BackupDir%%DsxDate%\%Host%-%%i.dsx
    GOTO EXITPT
:: -----------------------------------------------------------------
:: Uhoh, a project failed to be exported.
:: -----------------------------------------------------------------
:ProjFail
    ECHO.
    ECHO *** ERROR:  Failed to Export Project: %%i on Host: %Host%
    ECHO.
    ECHO Please ensure that nobody else is accessing this server while you
    ECHO are running this backup script.
    ECHO.
    ECHO. >> %LogFileName%
    ECHO *** ERROR:  Failed to Export Project: %%i on Host: %Host% >> %LogFileName%
    ECHO. >> %LogFileName%
GOTO EXITPT
:: -----------------------------------------------------------------
:: Report that paramters are not valid on screen and log file.
:: Note: Password are not reported for better security.
:: -----------------------------------------------------------------
:BadParam
    echo.
    echo Invalid parameters - Unable to access Server.
    echo.
    echo. >> %LogFileName%
    echo Invalid parameters - Unable to access Server. >> %LogFileName%
    echo. >> %LogFileName%
GOTO Syntax
:: -----------------------------------------------------------------
:: Report that directory is non-existant.
:: -----------------------------------------------------------------
:BadMain
    echo.
    echo Bad/Non-existing directory: %BackupDir%
    echo.
    echo Please ensure that you have permission to access/create directories
    echo and files.  Also ensure that directory listed exists.
    echo.
    echo. >> %LogFileName%
    echo Bad/Non-existing directory: %BackupDir% >> %LogFileName%
    echo. >> %LogFileName%
GOTO EXITPT
:: -----------------------------------------------------------------
:: Report that program does not have privs to create directory.
:: -----------------------------------------------------------------
:BadDir
    echo.
    echo Unable to create subdirectory: %BackupDir%%DsxDate%\
    echo.
    echo Please ensure that you have permission to access/create directories
    echo and files.
    echo.
    echo. >> %LogFileName%
    echo Unable to create subdirectory: %BackupDir%%DsxDate%\ >> %LogFileName%
    echo. >> %LogFileName%
GOTO EXITPT
:: -----------------------------------------------------------------
:: Report proper syntax usage.
:: -----------------------------------------------------------------
:Syntax
    echo.
    echo DataStage Automated Backup Routine
    echo.
    echo Based on design by Manoli Krinos
    echo.
    echo    Modified by M Ruland to allow iteration through a
    echo    complete server set of projects
    echo.
    echo    Also Modified by T.J. Griesenbrock to utilize command
    echo line options.
    echo.
::    echo Usage: DataStageBackup SERVER USERNAME PASSWORD BACKUP_DIR COMPRESS
    echo Usage: DataStageBackup SERVER USERNAME PASSWORD BASE_DIR PROJECT_LIST
    echo.
    echo SERVER - Hostname
    echo USERNAME - Username to use on SERVER.
    echo PASSWORD - Associated password for USERNAME on SERVER.
    echo BASE_DIR - Scripts, Backup directory below this.  MUST be a pre-existing directory.
    echo PROJECT_LIST - MUST be a pre-existing directory.
::    echo COMPRESS - Compression Toggle - 0 = No, 1 = Yes
    echo.
GOTO ENDPOINT
:: -----------------------------------------------------------------
:EXITPT
    ECHO. >> %LogFileName%
:: -----------------------------------------------------------------
:ENDPOINT
:: kim    del %ProjectList% > NUL 2> NUL
    del %BadProjects% > NUL 2> NUL
    del %GoodProjects% > NUL 2> NUL
    ENDLOCAL

0 comments:

© Copyright © 2015Big Data - DW & BI. by