Page 1 of 2
Help with a Xcopy Batch File
Posted: 18 May 2013 12:05
by creeky
Hi all,
First post, I am trying to create a scheduled back up using a batch file. I have created the code and it backs up OK. I have added the /D parameter to prevent long running back ups as not all the data will have changed. This Parameter refuses to work. I dont know what I am doing wrong. I copied the code from a website, Modded the file destinations. The first test seemed to work ok on a short test. But when I put in the full file paths and run the second time, the back up looks at all the files. Below is the file. Can you give me some help please?
Thanks in advance
@echo off
:: variables
set drive=z:\Dannys Backup
set backupcmd=xcopy /s /c /d /e /h /i /r /y
echo ### Backing up My Documents...
%backupcmd% "%USERPROFILE%\Documents" "%drive%\My Documents"
%backupcmd% "%USERPROFILE%\Desktop" "%drive%\Desktop"
%backupcmd% "%USERPROFILE%\Downloads" "%drive%\Downloads"
%backupcmd% "%USERPROFILE%\Music" "%drive%\Music"
%backupcmd% "%USERPROFILE%\Videos" "%drive%\Videos"
%backupcmd% "%USERPROFILE%\Office and Visio exe's" "%drive%\Office and Visio exe's"
echo Backup Complete!
@pause
Re: Help with a Xcopy Batch File
Posted: 18 May 2013 23:15
by foxidrive
If you use Vista or later then you also have a tool called Robocopy.exe and it is downloadable for XP as well.
The copy subroutine accepts the two source and destination folders and is the command I use to mirror directory trees.
It doesn't copy files that already exist, and it deletes files that are no longer on the source - thus creating a mirror backup.
I think that /DST is a new switch so if your Robocopy doesn't support /DST to ignore daylight saving time changes,
then remove the /DST - it will fail to run if it doesn't like the command line.
Code: Select all
@echo off
:: variables
set "drive=z:\Dannys Backup"
set "log=%drive%\backuplog.txt"
echo ### Backing up My Documents...
call :copy "%USERPROFILE%\Documents" "%drive%\My Documents"
call :copy "%USERPROFILE%\Desktop" "%drive%\Desktop"
call :copy "%USERPROFILE%\Downloads" "%drive%\Downloads"
call :copy "%USERPROFILE%\Music" "%drive%\Music"
call :copy "%USERPROFILE%\Videos" "%drive%\Videos"
call :copy "%USERPROFILE%\Office and Visio exe's" "%drive%\Office and Visio exe's"
echo Backup Complete!
pause
goto :eof
:copy
robocopy %1 %2 /DST /FFT /R:0 /MIR /NP /TEE /LOG+:"%log%" /FP /NDL /NJH /NJS
Re: Help with a Xcopy Batch File
Posted: 19 May 2013 14:19
by creeky
Hi Foxidrive,
Thank you very much for your help, Code works great. FYI I am running win 8, so DST switch works. I previously had separate back up batch files for 3 different users. But this makes sense to have just one. I have had to look at some folders individually (instead of the User Directory all at once) as the mirror copy the Temp Internet Files all the time as these change.
The only thing two I would like to add, If Possible are 1, To Time stamp the last back up made in the log, As this will be scheduled. 2, Or If possible to email me to notify back up has been completed.
I don't know if you would like to help, and it maybe cheeky me asking, so If not no worries.
Thanks again for your help.
Creeky.
Re: Help with a Xcopy Batch File
Posted: 19 May 2013 14:24
by foxidrive
I'm glad it works for you too.
Regarding the log and timestamp - do you want a timestamped log file, one for each time it runs?
BTW, you can exclude folders by adding them to the end of the robocopy command. Adding this will exclude that folder whenever it encounters it.
/XD "temporary internet files"
similarly you can exclude files and include multiple exclusions.
/XF "c:\*.sys" /XF "*.MKV" /XF "*.mp3" etc
Re: Help with a Xcopy Batch File
Posted: 19 May 2013 14:36
by creeky
Hi,
Yes I would like to ensure the scheduled back up is carried out successfully. If this is too much work, I would just like a time 'stamp to tell me the last time the back up was run.So I don't have to check the mirror file sizes to confirm.
Thanks
Creeky
Re: Help with a Xcopy Batch File
Posted: 19 May 2013 14:36
by creeky
PS, thanks for the switches
Re: Help with a Xcopy Batch File
Posted: 19 May 2013 23:35
by foxidrive
This includes a timestamped .txt log file for each launch, as well as a timestamped .txt.log file with the success or otherwise of each copy operation.
Code: Select all
@echo off
:: create a timestamp YYYY-MM-DD_HH-MM-SS
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set dt=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%_%dt:~8,2%-%dt:~10,2%-%dt:~12,2%
:: variables
set "drive=z:\Dannys Backup"
set "log=%drive%\backuplog-%dt%.txt"
echo ### Backing up My Documents...
call :copy "%USERPROFILE%\Documents" "%drive%\My Documents"
call :copy "%USERPROFILE%\Desktop" "%drive%\Desktop"
call :copy "%USERPROFILE%\Downloads" "%drive%\Downloads"
call :copy "%USERPROFILE%\Music" "%drive%\Music"
call :copy "%USERPROFILE%\Videos" "%drive%\Videos"
call :copy "%USERPROFILE%\Office and Visio exe's" "%drive%\Office and Visio exe's"
echo Backup Complete!
pause
goto :eof
:copy
robocopy %1 %2 /DST /FFT /R:0 /MIR /NP /TEE /LOG+:"%log%" /FP /NDL /NJH /NJS
if errorlevel 16 >>"%log%.log" echo ***FATAL ERROR*** --- copying %1 %2 & goto :eof
if errorlevel 8 >>"%log%.log" echo **FAILED COPIES** --- copying %1 %2 & goto :eof
if errorlevel 4 >>"%log%.log" echo *MISMATCHES* --- copying %1 %2 & goto :eof
if errorlevel 2 >>"%log%.log" echo EXTRA FILES --- copying %1 %2 & goto :eof
if errorlevel 1 >>"%log%.log" echo Copy successful --- copying %1 %2 & goto :eof
if errorlevel 0 >>"%log%.log" echo --no change-- --- copying %1 %2 & goto :eof
Re: Help with a Xcopy Batch File
Posted: 20 May 2013 01:34
by creeky
Thank you very much!
I will try tonight.
Re: Help with a Xcopy Batch File
Posted: 04 Jun 2013 12:53
by creeky
Right I'm Back.
I have managed to get a bit of time to try and get this right. Which I think I have now (Set Up Wise). I now have 3 batch files, One for each user account. These files are placed in the users start up folder to allow a daily back up when the PC is started and the user logs on. The PC shuts down at 02:00 every morning.
The only issue I have now is that One Batch File (my account) work perfect. but the other two accounts report a error no.5 when asked to back up the current users document folders. It seems to be a weird fault as when backing up the documents folder Robocopy creates 'My Pictures' My Videos' and 'My Music' folders within the My Documents folder even though they do not exist in the original source folder. After creating them the follow error occurs
2013/06/01 23:00:03 ERROR 5 (0x00000005) Time-Stamping Destination Directory z:\Dannys Backup\Molly User Folder\My Documents\My Music\
Access is denied.
2013/06/01 23:00:03 ERROR 5 (0x00000005) Time-Stamping Destination Directory z:\Dannys Backup\Molly User Folder\My Documents\My Pictures\
Access is denied.
2013/06/01 23:00:03 ERROR 5 (0x00000005) Time-Stamping Destination Directory z:\Dannys Backup\Molly User Folder\My Documents\My Videos\
Access is denied.
The Code appears to be exactly the same as my batch file (copied and pasted several times to confirm) but the error still exists.
The batch file is run from the users accounts when they log on and NOT from mine.
Below is the code, Can you check just incase I have missed something please?
Code: Select all
@echo off
:: create a timestamp YYYY-MM-DD_HH-MM-SS
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set dt=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%_%dt:~8,2%-%dt:~10,2%-%dt:~12,2%
:: variables
set "drive=z:\Dannys Backup"
set "log=%drive%\Molly User Folder\backuplog-%dt%.txt"
echo ### Backing up Computer...
echo ### Starting in 30 Seconds, Exit to stop.
timeout 30
echo ### Backing up Computer Files...
call :copy "%USERPROFILE%\Documents" "%drive%\Molly User Folder\Documents"
call :copy "%USERPROFILE%\Desktop" "%drive%\Molly User Folder\Desktop"
call :copy "%USERPROFILE%\Downloads" "%drive%\Molly User Folder\Downloads"
call :copy "%USERPROFILE%\Music" "%drive%\Molly User Folder\Music"
call :copy "%USERPROFILE%\Videos" "%drive%\Molly User Folder\Videos"
call :copy "%USERPROFILE%\Pictures" "%drive%\Molly User Folder\Pictures"
call :copy "%USERPROFILE%\Contacts" "%drive%\Molly User Folder\Contacts"
call :copy "%USERPROFILE%\Favorites" "%drive%\Molly User Folder\Favorites"
echo Backup Complete!
pause
timeout 10
goto :eof
:copy
robocopy %1 %2 /DST /FFT /R:0 /MIR /NP /TEE /LOG+:"%log%" /FP /NDL /NJH /NJS /XF "*.ini" /XF "*.ithmb" /XF "*.itc2"
if errorlevel 16 >>"%log%.log" echo ***IN USE OR WRITES ISSUE*** --- copying %1 %2 & goto :eof
if errorlevel 8 >>"%log%.log" echo **FAILED COPIES** --- copying %1 %2 & goto :eof
if errorlevel 4 >>"%log%.log" echo *MISMATCHES* --- copying %1 %2 & goto :eof
if errorlevel 2 >>"%log%.log" echo EXTRA FILES --- copying %1 %2 & goto :eof
if errorlevel 1 >>"%log%.log" echo Copy successful --- copying %1 %2 & goto :eof
if errorlevel 0 >>"%log%.log" echo --no change-- --- copying %1 %2 & goto :eof
Thanks in advance
Just to add; If I comment out the first Call Copy line, The batch file runs perfect with no errors.
Re: Help with a Xcopy Batch File
Posted: 04 Jun 2013 13:12
by Squashman
Do they have the proper NTFS permissions to access this folder?
z:\Dannys Backup
Re: Help with a Xcopy Batch File
Posted: 04 Jun 2013 15:19
by creeky
Hi,
Thanks for the reply, I would presume so as robocopy copies the rest of the folder over OK.
The Back up completes OK it just it reports this error. Which isn't too much of an issue. Although I will be checking the logs regularly to ensure there are no errors.
I have checked permissions and found all looks OK. Weirdly (although I am pretty sure I did not change anything) when I now run both batch files, I do not get any errors during the back up process.
But when its finished and I check the Log file it show the following
**FAILED COPIES** --- copying "C:\Users\Mummy\Documents" "z:\Dannys Backup\Mummy User Folder\Documents"
If I access the other change Log nothing is shown.
How can I find out what is driving the error now?
Thanks in advance.
Re: Help with a Xcopy Batch File
Posted: 04 Jun 2013 22:36
by foxidrive
I also get errors and it would seem to be a Robocopy bug on these folders, which are LNKs in the source folder.
Just ignore it.
Code: Select all
2013/06/05 14:33:01 ERROR 5 (0x00000005) Time-Stamping Destination Directory d:\abc\a\My Music\
Access is denied.
2013/06/05 14:33:01 ERROR 5 (0x00000005) Time-Stamping Destination Directory d:\abc\a\My Pictures\
Access is denied.
2013/06/05 14:33:01 ERROR 5 (0x00000005) Time-Stamping Destination Directory d:\abc\a\My Videos\
Access is denied.
Re: Help with a Xcopy Batch File
Posted: 05 Jun 2013 12:44
by Ocalabob
foxidrive wrote:I also get errors and it would seem to be a Robocopy bug on these folders, which are LNKs in the source folder.
Just ignore it.
foxidrive,
Did you try using the /XJ switch?
/XJ :: eXclude Junction points. (normally included by default).
Re: Help with a Xcopy Batch File
Posted: 05 Jun 2013 13:59
by foxidrive
Ocalabob wrote:foxidrive wrote:I also get errors and it would seem to be a Robocopy bug on these folders, which are LNKs in the source folder.
Just ignore it.
foxidrive,
Did you try using the /XJ switch?
/XJ :: eXclude Junction points. (normally included by default).
Good solution there Ocalabob - it solves the error.
Re: Help with a Xcopy Batch File
Posted: 05 Jun 2013 18:51
by TechnoMage
Hi Guys! Newcomer here!
I too back up all my data files every day that I post any update to them, add new pictures, etc.
A simple one-liner in a batch file does most of the work.
xcopy "C:\Documents and Settings\Alexi\My Documents\*.*" "M:\My Documents\" /S /Y /H /R /DIn the above example, drive M is a Toshiba, USB 3, external 1TB hard drive.
I also have a 32 gig, USB 3, Flash Drive permanently plugged into my USB 3.0 card, for an additional backup location.
I added additional lines for other data folders that I also want to update, like saved EMail, My Wordperfect files, my Firefox Bookmarks, etc.
I've used XCOPY since the DOS days and find it fast, efficient and very easy to program.
Since XCOPY is programmed to only back up new files, or files that have been changed since the last backup,
my daily backup takes only a few seconds.
I can run my backup batch file manually, or from my Startup Folder or from a Shutdown Batch file containing my
shutdown command. Here's that command:
%windir%\System32\shutdown.exe -s -t 00 -fLong Live DOS!
TechnoMage