Archive for the ‘Scripts’ Category

This script was written for one of our daily offsite backup. I have used some web help for part of the script.

This script works in following steps:

  • Backup Files.
  • Copy the Files to a temp location.
  • Zip those files into a single zip.
  • Transfer those files to remote off-site location.
  • Notification Email
  • Clean up temp files.

Following paths need to be configured as per requirement:

Location and name of log files
C:\Logs\Filename-Backup-%ds%.log

Location of files to be backed up and transferred
C:\ImportantFiles\

Location of temp files
C:\TempBackup\tmpBck\

Location of temp zipped backup
C:\TempBackup\ImportantFiles\

Location of remote off-site location
\\IP_Address_Remote_Location\ImportantFiles\

Name of the zipped files to be transferred
DailyBackup.zip

Notification script for backup status.
Notifier.vbs

Note 1

I have used a VB script downloaded from web. You can use any notification method you like. My script calls a second script which has to be placed in the same folder as this script

Note 2

I have used 7-zip as compression tool which is a free download

http://www.7-zip.org/download.html

@Echo Off
::  Backup Files. 
::  Copy the Files to a temp location,
::  Zip those files into a single zip,
::  Transfer those files to remote off-site location.  
::  Clean up temp files.

:: Set Date vars
Set mm=%DATE:~4,2%&Set dd=%DATE:~7,2%&Set yyyy=%DATE:~10,4%
Set ds=%yyyy%-%mm%-%dd%

:: Define Log File
Set elf=C:\Logs\Filename-Backup-%ds%.log

:: Create Temp copy of files
echo ###################################################################>>%elf%
echo --- Backup - Start at %date% - %time% -- >>%elf%
echo ###################################################################>>%elf%

echo --- Creating Temp Folder: C:\TempBackup\tmpBck -- >>%elf%
rmdir /S /Q "C:\TempBackup\tmpBck" >>%elf%
mkdir "C:\TempBackup\tmpBck" >>%elf%

echo --- Copying Files... >>%elf%
copy "C:\ImportantFiles\*.*" "C:\TempBackup\tmpBck" >>%elf%
If %errorlevel% NEQ 0 (
  set errCode=Temp_Copy_Of_Files_Failed
  goto errOut
  )

:: Zip files
echo --- Compressing Files... >>%elf%
"C:\Program Files\7-zip\7z.exe" a -tzip "C:\TempBackup\ImportantFiles\DailyBackup.zip" "C:\TempBackup\tmpBck\*.*" >>%elf%
If %errorlevel% NEQ 0 (
  set errCode=zip_process_failed
  goto errOut
  )
echo --- Compress Successful... >>%elf%

:: Delete Yesterday file from remote location
If exist "\\IP_Address_Remote_Location\ImportantFiles\DailyBackup.zip" (
  echo --- Deleting Remote copy of previous backup... >>%elf%
  del "\\IP_Address_Remote_Location\ImportantFiles\DailyBackup.zip" >>%elf%
  )

:: Transfer files
echo --- Transfering new zip file to remote location... >>%elf%
robocopy "C:\TempBackup\ImportantFiles" "\\IP_Address_Remote_Location\ImportantFiles" /E /XO /B /NP /R:0 >>%elf%
echo --- Transfer Return code - %errorlevel% >>%elf%
If %errorlevel% GEQ 8 (
  set errCode=Transfer_to_remote_failed
  goto errOut
  )
echo --- Transfer Successful... >>%elf%

:: Clean up local work files
echo --- Deleting local temporary work files... >>%elf%
if exist "\\IP_Address_Remote_Location\ImportantFiles\DailyBackup.zip" (
  del "C:\TempBackup\ImportantFiles\DailyBackup.zip" >>%elf%
  rmdir /S /Q "C:\TempBackup\tmpBck" >>%elf% 
  )
If %errorlevel% NEQ 0 (
  set errCode=Clean_up_failed
  goto errOut
  )

echo ###################################################################>>%elf%
echo --- Backup - Done at %date% - %time% -- >>%elf%
echo ###################################################################>>%elf%

:: Send Notification Email
call cscript //nologo Notifier.vbs -b%elf% -fDailyBackup.zip -iDaily -rok
GOTO :EOF

:errOut
echo --- Error Processing Backup Files: >>%elf%
echo --- %errCode% >>%elf%

echo ###################################################################>>%elf%
echo --- Backup - INCOMPLETE!!! %date% - %time% -- >>%elf%
echo ###################################################################>>%elf%

:: Send Notification Email
cscript //nologo Notifier.vbs -b%elf% -fDailyBackup.zip -iDaily -rbad
GOTO :EOF
Advertisements