Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @ECHO OFF
- REM *********************************************************************************
- REM * *
- REM * USE AT YOUR OWN RISK!!! *
- REM * TEST THOROUGHLY BEFORE USING!!! *
- REM * *
- REM * This batch script will use ROBOCOPY /MIR command to copy files from a source *
- REM * to a destination path. Any data on the destination path not on the source *
- REM * will be deleted. *
- REM * *
- REM * This script will analyze the data to be backed up with ROBOCOPY and store *
- REM * the files that have changed or been deleted by copying from the destination/ *
- REM * backup folder to a timestamped folder location specified. *
- REM * *
- REM * Currently network paths are not supported, only drive letters *
- REM * *
- REM * Anywhere there is a "REM *** USER INPUT ***" please read and update values *
- REM * accordingly. *
- REM * *
- REM * VARIABLES: *
- REM * -- "source=" variable to set source drive letter to be backed up *
- REM * Example: "source=D:" - use only drive letter and ":" *
- REM * -- "dest=" variable to set destination drive letter to be backed up *
- REM * Example: "source=F:" - use only drive letter and ":" *
- REM * -- "folder=" variable to set base folder in source location indicated *
- REM * above that robocopy will backup. It will backup all files and subfolders *
- REM * Example: "folder=Data" so this will backup "D:\Data" combining "source=" *
- REM * drive from above and folder location. Destination location will be drive *
- REM * location indicated by "dest=" so in this case data will be backed up to *
- REM * -- "arch_dest=" variable to set drive letter and path where deleted and *
- REM * changed files that were on the source will be stored. *
- REM * Example: "arch_dest=Z:\Backup" *
- REM * A time stamped folder will be generated here for each ROBOCOPY run that *
- REM * will backup the files slated for deletion or have been changed. A log *
- REM * folder will also be generated with a log file named same as the time *
- REM * stamped folder to indicate files that were backed up. So if you set *
- REM * "arch_dest" to "Z:\Backup" there will be a folder called "Z:\Backup\log" *
- REM * with corresponding <timestamp>_archive.log file *
- REM * *
- REM * To summarize, this batch script uses Windows command line ROBOCOPY to: *
- REM * -- Backup files slated to be deleted or that have been changed in source. It *
- REM * copies them from the backup destination to the archive destination in *
- REM * timestamped folders as well as generate a log indicating files backed up *
- REM * -- After those files are archived, regular ROBOCOPY command is run which will *
- REM * update your regular backup location to match the source location files *
- REM * *
- REM * NOTE: *
- REM * There are two variables: *
- REM * SET "filextra= *EXTRA File" *
- REM * SET "filenewer= Newer" *
- REM * *
- REM * The "filextra=" contains a tab and two spaces before "*EXTRA File" *
- REM * The "filenewer=" contains a tab and four spaces before "Newer" *
- REM * *
- REM * These must be tab characters to properly detect files in the log that are *
- REM * sladed to be deleted by ROBOCOPY. So if you copy/paste this file and it *
- REM * doesn't seem to work, go in and manually put in a tab and appropriate spaces *
- REM * *
- REM * USE AT YOUR OWN RISK!!! *
- REM * TEST THOROUGHLY BEFORE USING!!! *
- REM * *
- REM *********************************************************************************
- REM Set current folder to batch folder for when running in administrator mode
- cd /d "%~dp0"
- SET "batchdir=%~dp0"
- REM Get Time for Logs
- CALL :sub_logtime
- REM 'timelog' is for regular backup log file 'folder' is for folder to be backed up/archived
- SET "timelog=databackup_%logtime%"
- ECHO *** LOG START %date% %time% *** > %timelog%.log
- ECHO,
- set day=%date:~0,3%
- REM Use robocopy to backup folder files
- REM Set Source and Destination Drive letter only, not path (network path not working currently)
- REM *** USER INPUT *** set "source=" to source drive letter you want to backup
- REM set "dest=" to destination drive letter you want to backup
- REM set "arch_dest=" to full path where you want historical backup with timestamped folders (no trailing slash)
- SET "source=D:"
- SET "dest=E:"
- SET "arch_dest=Z:\Backup"
- REM Set folder path to be copied (If root of drive, i.e. D:\, plese use "Folder=.")
- REM *** USER INPUT *** set "folder=" to indicate the folder path in the source drive to back up
- REM i.e. if you want to backup "D:\DATA" and all subfolders then set the above "source=" to "D:" and "folder=DATA"
- SET "folder=TEST1"
- call :sub_robocopy_arch
- REM ROBOCOPY command to actually copy source to destination
- REM *** USER INPUT *** - update ROBOCOPY flags as desired
- robocopy "%source%\%folder%" "%dest%\%folder%" /MIR /FFT /sl /xj /r:2 /w:10 /tee /log+:%timelog%.log /ts /fp /np /ndl
- timeout 10
- :END
- DEL /Q /F arch_*.log 2>nul
- DEL /Q /F archp_*.log 2>nul
- EXIT
- :sub_logtime
- SET "logtime="
- for /F "tokens=1-7* delims=/:. " %%a IN ("%DATE% %TIME%") DO (
- SET "MM=%%b"
- SET "DD=%%c"
- SET "YYYY=%%d"
- SET "HH=%%e"
- IF %%e LSS 10 SET "HH=0%%e"
- SET "MI=%%f"
- SET "SS=%%g"
- )
- SET "logtime=%YYYY%%MM%%DD%_%HH%%MI%%SS%"
- GOTO :EOF
- EXIT
- :sub_robocopy_arch
- cd /d "%batchdir%"
- DEL /Q /F arch_*.log 2>nul
- DEL /Q /F archp_*.log 2>nul
- ECHO,
- ECHO ... SCANNING FOR FILES TO BE COPIED FROM %source%\%folder% ...
- robocopy "%source%\%folder%" "%dest%\%folder%" /MIR /FFT /sl /xj /r:2 /w:10 /tee /log+:%logtime%.log /ns /njh /njs /fp /np /ndl /L
- REM Spaces in filextra= is actually one tab and two space, filenewer= is one tab and four spaces for proper detection
- SET "filextra= *EXTRA File"
- SET "filenewer= Newer"
- REM Extract entries in ROBOCOPY Log File flagged as "Newer" or "Extra"
- findstr /L /C:"%filextra%" %logtime%.log >archp_xtrafile.log
- findstr /L /C:"%filenewer%" %logtime%.log >archp_newerfile.log
- type archp_xtrafile.log >> archp_newfile.log
- type archp_newerfile.log >> archp_newfile.log
- REM Trim to include only path and file name.
- REM Use below for drive letter path source
- for /f "tokens=1* delims=:" %%A in (archp_newfile.log) do echo %%B>>arch_newfile.log
- IF NOT EXIST arch_newfile.log goto :CONT
- REM for /f %%p in (arch_newfile.log) do if %%~zp==0 ECHO NO NEW OR CHANGED FILES & goto :CONT
- REM Use below for network path source
- REM for /f "tokens=1* delims=\" %%A in (archp_newfile.log) do echo %%B>>archp_1newfile.log
- REM for /f "tokens=1* delims=\" %%A in (archp_1newfile.log) do echo %%B>>archp_2newfile.log
- REM for /f "tokens=1* delims=\" %%A in (archp_2newfile.log) do echo \%%B>>arch_newfile.log
- REM Use generated arch_newfile.log file to copy deleted and changed files to archive history folder
- for /f "usebackq delims=" %%i in ("arch_newfile.log") do (
- ROBOCOPY "%dest%%%~pi." "%arch_dest%\%logtime%%%~pi." "%%~ni%%~xi" /FFT /r:2 /w:10 /log+:%logtime%_archive.log /NJH /NJS /NP /FP /NDL /TEE
- )
- :CONT
- REM Remove logtime log - already been scrubbed for data it needs
- DEL /Q /F %logtime%.log
- :ENDSUBARCH
- ECHO,
- ECHO *** CONTINUING TO BACKUP SERVER FROM %source% to %dest%
- ECHO,
- REM Return back to original batch home path
- If NOT Exist "%dest%\log" md "%arch_dest%\log"
- IF Exist "%logtime%_archive.log" move "%logtime%_archive.log" "%arch_dest%\log"
- cd /d "%batchdir%"
- GOTO :EOF
- EXIT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement