Daily database backup.bat

@echo off

if %USERNAME%==Administrator echo you are administrator
set RAREXEC="C:\Program Files\WinRAR\WinRar.exe"
set RAREXEC="C:\Program Files\WinRAR\Rar.exe"
set LOGFILE="c:\sys\backup.log"

SET ARG1=%1

IF EXIST D:\ (
SET DRIVE=D:
) ELSE (
SET DRIVE=H:
)

REM -------------------------------------------------------
REM PARSE THE DATE CODE
REM -------------------------------------------------------
for /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set weekday=%%a& set month=%%b& set day=%%c& set year=%%d)
echo The day is %WEEKDAY% %DAY%
echo The month is %MONTH%
set DCODE=%MONTH%.%DAY%.%YEAR%.rar
ECHO %DCODE%
ECHO %WEEKDAY%

REM -----------------------------------------------------------
REM GET THE DATE, AND MAKE A DIRECTORY IN THE BACKUP TREE
REM -----------------------------------------------------------
for /f "tokens=2-4 delims=/ " %%a in ('DATE/T') do (set cdate=%%a.%%c)
ECHO %cdate%


IF NOT EXIST %DRIVE%\backup (
ECHO "BACKUP DIR"
MKDIR %DRIVE%\backup
)

IF NOT EXIST %DRIVE%\backup\%cdate% (
ECHO "BACKUP DIR"
MKDIR %DRIVE%\backup\%cdate%
)

rem GOTO FINISH
if %ERRORLEVEL% EQU 0 (

REM ----------------------------------------------------------
REM -- UNMOUNT STORES
REM ----------------------------------------------------------
net stop "MSCRMAsyncService"
net stop "MSCRMAsyncService$maintenance"
net stop "SQLSERVERAGENT"
net stop "MSSQLSERVER"
timeout 5

del /f %DRIVE%\backup\Daily\crm.%WEEKDAY%.rar
%RAREXEC% u -r -m0 -msrar;zip;jpg;iso;cab -ilogABACKUP.log %DRIVE%\backup\Daily\crm.%WEEKDAY%.rar @c:\sys\dblist.txt

REM ----------------------------------------------------------
REM -- REMOUNNT STORES
REM ----------------------------------------------------------

net start "MSSQLSERVER"
net start "SQLSERVERAGENT"
net start "MSCRMAsyncService"
net start "MSCRMAsyncService$maintenance"

)


:FINISH
REM -------------------------------------------------------
REM - CONNECT TO MYBOOKLIVE AND MAP NETWORK DRIVES
REM -------------------------------------------------------
net use v: \\FILESERVER01\backup /persistent:yes

IF %ERRORLEVEL% EQU 2 (
echo "PUNT... FAILED TO CONNECT TO MYBOOKLIVE1." >> %LOGFILE%
net use v: \\FILESERVER01\backup /persistent:yes
) else (
echo "successfully connected to mybooklive1"
)

IF %ERRORLEVEL% EQU 2 (
echo "PUNT... FAILED TO CONNECT TO MYBOOKLIVE2. FAILOVER TO DC-01." >> %LOGFILE%
exit
)

if %errorlevel% neq 0 goto END_PROGRAM
REM -------------------------------------------------------
REM - FINISHED CONNECTING NETWORK DRIVES
REM -------------------------------------------------------
IF NOT EXIST V:\%CDATE% (
MKDIR V:\%CDATE%
)

echo "copy files to server..."
IF NOT EXIST V:\%cdate% (
ECHO "MAKING DIRECTORY..."
MKDIR V:\%cdate%
)

ECHO "COPY " %DRIVE%\backup\Daily\crm.%WEEKDAY%
DIR %DRIVE%\backup\Daily\crm.%WEEKDAY%.rar

copy %DRIVE%\backup\Daily\crm.%WEEKDAY%.rar v:\%CDATE%
net use v: /delete /yes

rem PAUSE
:END_PROGRAM


echo "finished with crm backup " >> %LOGFILE%
date /t >> %LOGFILE%
echo "------" >> %LOGFILE%

Leave a Reply

Your email address will not be published. Required fields are marked *