Advertisement
HTWingNut

ROBOCOPY CHECK DELETED

Apr 7th, 2021
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.09 KB | None | 0 0
  1. @ECHO OFF
  2.  
  3. REM *********************************************************************************
  4. REM * *
  5. REM * USE AT YOUR OWN RISK!!! *
  6. REM * TEST THOROUGHLY BEFORE USING!!! *
  7. REM * *
  8. REM * This batch script will use ROBOCOPY /MIR command to copy files from a source *
  9. REM * to a destination path. Any data on the destination path not on the source *
  10. REM * will be deleted. *
  11. REM * *
  12. REM * This script will analyze the data to be backed up with ROBOCOPY and store *
  13. REM * the files that have changed or been deleted by copying from the destination/ *
  14. REM * backup folder to a timestamped folder location specified. *
  15. REM * *
  16. REM * Currently network paths are not supported, only drive letters *
  17. REM * *
  18. REM * Anywhere there is a "REM *** USER INPUT ***" please read and update values *
  19. REM * accordingly. *
  20. REM * *
  21. REM * VARIABLES: *
  22. REM * -- "source=" variable to set source drive letter to be backed up *
  23. REM * Example: "source=D:" - use only drive letter and ":" *
  24. REM * -- "dest=" variable to set destination drive letter to be backed up *
  25. REM * Example: "source=F:" - use only drive letter and ":" *
  26. REM * -- "folder=" variable to set base folder in source location indicated *
  27. REM * above that robocopy will backup. It will backup all files and subfolders *
  28. REM * Example: "folder=Data" so this will backup "D:\Data" combining "source=" *
  29. REM * drive from above and folder location. Destination location will be drive *
  30. REM * location indicated by "dest=" so in this case data will be backed up to *
  31. REM * -- "arch_dest=" variable to set drive letter and path where deleted and *
  32. REM * changed files that were on the source will be stored. *
  33. REM * Example: "arch_dest=Z:\Backup" *
  34. REM * A time stamped folder will be generated here for each ROBOCOPY run that *
  35. REM * will backup the files slated for deletion or have been changed. A log *
  36. REM * folder will also be generated with a log file named same as the time *
  37. REM * stamped folder to indicate files that were backed up. So if you set *
  38. REM * "arch_dest" to "Z:\Backup" there will be a folder called "Z:\Backup\log" *
  39. REM * with corresponding <timestamp>_archive.log file *
  40. REM * *
  41. REM * To summarize, this batch script uses Windows command line ROBOCOPY to: *
  42. REM * -- Backup files slated to be deleted or that have been changed in source. It *
  43. REM * copies them from the backup destination to the archive destination in *
  44. REM * timestamped folders as well as generate a log indicating files backed up *
  45. REM * -- After those files are archived, regular ROBOCOPY command is run which will *
  46. REM * update your regular backup location to match the source location files *
  47. REM * *
  48. REM * NOTE: *
  49. REM * There are two variables: *
  50. REM * SET "filextra= *EXTRA File" *
  51. REM * SET "filenewer= Newer" *
  52. REM * *
  53. REM * The "filextra=" contains a tab and two spaces before "*EXTRA File" *
  54. REM * The "filenewer=" contains a tab and four spaces before "Newer" *
  55. REM * *
  56. REM * These must be tab characters to properly detect files in the log that are *
  57. REM * sladed to be deleted by ROBOCOPY. So if you copy/paste this file and it *
  58. REM * doesn't seem to work, go in and manually put in a tab and appropriate spaces *
  59. REM * *
  60. REM * USE AT YOUR OWN RISK!!! *
  61. REM * TEST THOROUGHLY BEFORE USING!!! *
  62. REM * *
  63. REM *********************************************************************************
  64.  
  65. REM Set current folder to batch folder for when running in administrator mode
  66. cd /d "%~dp0"
  67. SET "batchdir=%~dp0"
  68.  
  69. REM Get Time for Logs
  70. CALL :sub_logtime
  71.  
  72. REM 'timelog' is for regular backup log file 'folder' is for folder to be backed up/archived
  73. SET "timelog=databackup_%logtime%"
  74.  
  75. ECHO *** LOG START %date% %time% *** > %timelog%.log
  76. ECHO,
  77.  
  78. set day=%date:~0,3%
  79.  
  80. REM Use robocopy to backup folder files
  81.  
  82. REM Set Source and Destination Drive letter only, not path (network path not working currently)
  83. REM *** USER INPUT *** set "source=" to source drive letter you want to backup
  84. REM set "dest=" to destination drive letter you want to backup
  85. REM set "arch_dest=" to full path where you want historical backup with timestamped folders (no trailing slash)
  86.  
  87. SET "source=D:"
  88. SET "dest=E:"
  89. SET "arch_dest=Z:\Backup"
  90.  
  91. REM Set folder path to be copied (If root of drive, i.e. D:\, plese use "Folder=.")
  92. REM *** USER INPUT *** set "folder=" to indicate the folder path in the source drive to back up
  93. REM i.e. if you want to backup "D:\DATA" and all subfolders then set the above "source=" to "D:" and "folder=DATA"
  94. SET "folder=TEST1"
  95.  
  96. call :sub_robocopy_arch
  97. REM ROBOCOPY command to actually copy source to destination
  98. REM *** USER INPUT *** - update ROBOCOPY flags as desired
  99. robocopy "%source%\%folder%" "%dest%\%folder%" /MIR /FFT /sl /xj /r:2 /w:10 /tee /log+:%timelog%.log /ts /fp /np /ndl
  100.  
  101. timeout 10
  102.  
  103. :END
  104. DEL /Q /F arch_*.log 2>nul
  105. DEL /Q /F archp_*.log 2>nul
  106.  
  107. EXIT
  108.  
  109. :sub_logtime
  110. SET "logtime="
  111. for /F "tokens=1-7* delims=/:. " %%a IN ("%DATE% %TIME%") DO (
  112. SET "MM=%%b"
  113. SET "DD=%%c"
  114. SET "YYYY=%%d"
  115. SET "HH=%%e"
  116. IF %%e LSS 10 SET "HH=0%%e"
  117. SET "MI=%%f"
  118. SET "SS=%%g"
  119. )
  120. SET "logtime=%YYYY%%MM%%DD%_%HH%%MI%%SS%"
  121. GOTO :EOF
  122.  
  123. EXIT
  124.  
  125. :sub_robocopy_arch
  126.  
  127. cd /d "%batchdir%"
  128. DEL /Q /F arch_*.log 2>nul
  129. DEL /Q /F archp_*.log 2>nul
  130.  
  131. ECHO,
  132. ECHO ... SCANNING FOR FILES TO BE COPIED FROM %source%\%folder% ...
  133.  
  134. robocopy "%source%\%folder%" "%dest%\%folder%" /MIR /FFT /sl /xj /r:2 /w:10 /tee /log+:%logtime%.log /ns /njh /njs /fp /np /ndl /L
  135.  
  136. REM Spaces in filextra= is actually one tab and two space, filenewer= is one tab and four spaces for proper detection
  137. SET "filextra= *EXTRA File"
  138. SET "filenewer= Newer"
  139.  
  140. REM Extract entries in ROBOCOPY Log File flagged as "Newer" or "Extra"
  141. findstr /L /C:"%filextra%" %logtime%.log >archp_xtrafile.log
  142. findstr /L /C:"%filenewer%" %logtime%.log >archp_newerfile.log
  143. type archp_xtrafile.log >> archp_newfile.log
  144. type archp_newerfile.log >> archp_newfile.log
  145.  
  146.  
  147. REM Trim to include only path and file name.
  148. REM Use below for drive letter path source
  149. for /f "tokens=1* delims=:" %%A in (archp_newfile.log) do echo %%B>>arch_newfile.log
  150.  
  151. IF NOT EXIST arch_newfile.log goto :CONT
  152.  
  153. REM for /f %%p in (arch_newfile.log) do if %%~zp==0 ECHO NO NEW OR CHANGED FILES & goto :CONT
  154.  
  155. REM Use below for network path source
  156. REM for /f "tokens=1* delims=\" %%A in (archp_newfile.log) do echo %%B>>archp_1newfile.log
  157. REM for /f "tokens=1* delims=\" %%A in (archp_1newfile.log) do echo %%B>>archp_2newfile.log
  158. REM for /f "tokens=1* delims=\" %%A in (archp_2newfile.log) do echo \%%B>>arch_newfile.log
  159.  
  160. REM Use generated arch_newfile.log file to copy deleted and changed files to archive history folder
  161.  
  162. for /f "usebackq delims=" %%i in ("arch_newfile.log") do (
  163. ROBOCOPY "%dest%%%~pi." "%arch_dest%\%logtime%%%~pi." "%%~ni%%~xi" /FFT /r:2 /w:10 /log+:%logtime%_archive.log /NJH /NJS /NP /FP /NDL /TEE
  164. )
  165.  
  166. :CONT
  167. REM Remove logtime log - already been scrubbed for data it needs
  168. DEL /Q /F %logtime%.log
  169.  
  170. :ENDSUBARCH
  171.  
  172. ECHO,
  173. ECHO *** CONTINUING TO BACKUP SERVER FROM %source% to %dest%
  174. ECHO,
  175. REM Return back to original batch home path
  176. If NOT Exist "%dest%\log" md "%arch_dest%\log"
  177. IF Exist "%logtime%_archive.log" move "%logtime%_archive.log" "%arch_dest%\log"
  178. cd /d "%batchdir%"
  179.  
  180. GOTO :EOF
  181.  
  182. EXIT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement