5

Is it possible to mirror the two directories without overwritting the files in destination directory with new/changed/deleted files. Something like snapshots.

Example: Copy the source directory with all files and sub-directories to destination directory, but if destination directory contains, for example, file A.xls and A.xls has been changed in source directory then copy A.xls but keep the previous A in destination directory as well. To preserve the previous file a datestamp, or counter can be added to the file name.

Example after copy:
SomeDirectory
|--A.xls
|--A_20120701.xls
|--A_20120920.xls

Thank you.

2 Answers 2

5

No, this isn't possible using Robocopy. Mirroring a folder path using Robocopy will delete files in the destination if they no longer exist in the source. You can ensure that you don't overwrite files with older versions but you can't retain older versions / rename during the copy process.

I wrote a command script / batch file that copies modified files, but creates the tree structure based on the date that the script is run

e.g. my tree for one particular file would be.

c:\_archive\201209\
                    23\
                        0930
                             Todays notes.txt
                        1145
                             Todays notes.txt
                    24\
                        1000
                             Todays notes.txt

I could post a sample of that script if you might find that useful.

Due to popular demand, please find below my basic backup script.

@echo off
cls
rem parse the output of the date /t command to create a date in the format yyyymmdd, and also remove the delimiter (/)
rem  store the results in environment variables
for /F "tokens=1,2,3,4 delims=/ " %%i IN ('date /t') do SET Z_DATE=%%k%%j%%i 
for /F "tokens=1,2,3,4 delims=/ " %%i IN ('date /t') do SET Z_YEAR=%%k
for /F "tokens=1,2,3,4 delims=/ " %%i IN ('date /t') do SET Z_MONTH=%%j
for /F "tokens=1,2,3,4 delims=/ " %%i IN ('date /t') do SET Z_DAY=%%i

echo date %Z_DATE%
echo year %Z_YEAR%
echo month %Z_MONTH%
echo day %Z_DAY%

rem parse the output of the time /t command to remove the delimeter (:)
rem  store the result in an environment variable
for /f "tokens=1,2 delims=:. " %%i IN ('time /t') do SET Z_TIME=%%i%%j

echo time %Z_TIME%
rem change the colour to a nice deep green on black.
color 02
::-------------------------

@echo on
xcopy "c:\MyFiles\*.*" "C:\MyArchive\ByDate\%Z_YEAR%%Z_MONTH%\%Z_DAY%\%Z_TIME%%~p1%~n1\" /ksymhr
@if @@ERRORLEVEL==1 SET Z_BACKUP_ERROR_FLAG=1

Goto End

::---------------------------------------------------------------------------
:NotifyUser
echo.
echo An error occurred during the backup.
echo.
pause

::---------------------------------------------------------------------------
:End

echo %Z_DATE%
echo %Z_TIME%
time /t

rem Clear out the environment variables
SET Z_DATE=
SET Z_TIME=
SET Z_BACKUP_ERROR_FLAG=

if you have any questions about this script (I cribbed it quickly from my main backup script, and didn't test it thoroughly) then email me at my gmail account with the subject line 'Server Fault Backup Script query'. If you can work out my gmail address without asking then I'll be happy to help you.

2
  • Thanks, sounds cool, it would be nice to see your script. Please post it. Sep 24, 2012 at 13:07
  • could you please add your script? Thanks. Sep 25, 2012 at 12:20
0

The lines to parse out the date, year, month and day did not work for me. However, these did and the rest of the script works perfectly:

Set Z_Date=%date:~10,4%%date:~4,2%%date:~7,2%
Set Z_Year=%date:~10,4%
Set Z_Month=%date:~4,2%
Set Z_Day=%date:~7,2%

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .