Batch file backup deletes previous file

March 30, 2010 at 12:37:26
Specs: Windows XP, Quad/3gb
I created a backupfile.bat to run from my user's Windows Scheduler. So far I've put the bat file on 35 computers and intend to add it to at least another 30-40 (o/s are a mix of xp and 2000). The bat file is supposed to backup to a network server, five Lotus Notes email files (ex. bookmarks, id files, ini files) including a large file that can be up to 5gb or more. The scheduled times for the backups have been randomly selected. The backup process using the Windows Scheduler and the backupfile.bat files seems to work ..for the most part ..pretty good. There have been a few that just wouldn't run and so I would change the scheduled time and then they would run. The backupfile.bat file includes a statement to connect to the server: net use Y: \\theServer\backupDir and a statement to disconnect: net use Y: /delete /YES. I also have it write to a log file each time it xcopies one of the five files. BIG problem is ... sometimes four of the files will be copied just fine and the fifth one, which is that big one up to 5 or more gb (Lotus archive file) will not be copied (and the log file shows nothing copied). I don't know if it's just too big of a drain on resources at some point somewhere or what (any ideas?), because the next day it might copy the big file just fine. However, when it chooses not to copy that big file, it for some reason DELETES the big file that was there from the previous successful copy...AND this is a very bad thing. Very glad I noticed this before I needed to restore that file for someone and it wasn't there. My intent IS (and my code should be) to replace the old files with the new files BUT not if the new file is NOT going to be copied. Here is my code:

And thank you in advance for any help...or advice...or tips ;-)

@echo off::Variables

::Save path where the Lotus files are located.

:: Files are directly under C:
SET LotusDir=c:\lotus\notes
SET LotusDataDir=C:\lotus\notes\data
SET LotusArchiveDir=C:\lotus\notes\data\Archive
::Connects mapping session

net use Y: /delete /YES

net use Y: \\theServer\backupDir
::Create Folder for User
::Save path where files will be backed up to
SET vArchive=Y:\%USERNAME%\
::Only want to keep one day's logs so delete log file each time

@ECHO Y | DEL %vArchive%%LotusBackupsLog.txt
::Save path where log file will go for redirects below

SET TheLog=%vArchive%%LotusBackupsLog.txt
ECHO ~ bookmark.nsf ~ >>%TheLog%
XCOPY %LotusDataDir%\bookmark.nsf %vArchive%/Y/Q >>%TheLog%
ECHO.>>%TheLog%ECHO %TIME% >>%TheLog%
ECHO.>>%TheLog%ECHO ~ id files ~ >>%TheLog%
XCOPY %LotusDataDir%\*.id %vArchive%/Y/Q >>%TheLog%
ECHO.>>%TheLog%ECHO ~ notes.ini ~ >>%TheLog%
XCOPY %LotusDir%\notes.ini %vArchive%/Y/Q >>%TheLog%
ECHO ~ names.nsf ~ >>%TheLog%
XCOPY %LotusDataDir%\names.nsf %vArchive%/Y/Q >>%TheLog%
ECHO.>>%TheLog%echo Backing up your Lotus files.  You can minimize and continue working while it's running.echo When its done running it will close on its own.
ECHO ~ archive file ~ >>%TheLog%
XCOPY %LotusArchiveDir%\*.nsf %vArchive%/Y/Q >>%TheLog%
ECHO ~ Finish time %TIME% ~>>%TheLog%
::Disconnects mapping session>>%TheLog%
net use Y: /delete /YES 

edited by moderator: Code formatting fixed. -Razor2.3

See More: Batch file backup deletes previous file

Report •

March 30, 2010 at 12:51:45
You want to use the /Z switch on your copy.

EDIT: Also, you log errors with 2>>, not >>

Report •

March 30, 2010 at 13:51:30
Thank you for the suggestion of using the /Z switch. I will give that a try.

Regarding your comment about logging errors, I'm just logging info (ex. "1 file copied"), not errors.

Report •

March 30, 2010 at 15:04:31
The /Z parameter may work, but also behold this:

- What may be wrong, is that a process on the source is still using the file, and possibly locking it. This may prevent the copy command to fail on that file ..
- I see you copy to Y:\%USERNAME% ... but, how unique is that directory ? If - at the same time - multiple clients, connected as the same user, try to run the script, this may cause locking on the target server. Same here ... copy commands may fail randomly ...
- The problem with targetfile already deleted but new sourcefile not copied ... check all of the above mentioned things in this thread, and if that does not work : normal copy commands are protected for that. Maybe you then need to check the copy parameters ... or, you build your own check : start with making an extra backup on target, THEN try to copy source to target, THEN check existance of new target. If an error occured, replace old target.

Report •

Related Solutions

April 1, 2010 at 15:28:02
Re: "a process on the source is still using the file, and possible locking it." and also, "I see you copy to Y:\%USERNAME% but, how unique is that directory?" The files are being backed up to their own folder named from their USERNAME environmental variable. Each of these folders are in a directory on the server (Y) called Employees. So, no two people are accessing the same source or destination file, however, two or more people can be trying to access the same server Y:\Employees. Even so, sometimes the backup is successful and sometimes not.

I will look into your suggestion about adding some more code to do a check for existance of the target.

Thank you.

Report •

April 2, 2010 at 08:01:32
Just a hint, but suppose there are 20 computers which, at the exact same time (scheduled) are writing to a subdirectory in Y:\employees (being : Y:\employees\<username>)

... maybe the directory Y:\employees may be locked (or, unavailable) ... although directories are not easily locked, files are.

Let us know what you find

Report •

April 2, 2010 at 08:36:18
I understand what you're saying although I'm not real clear on what locks files/directories. Right now I'm testing the /Z switch on the XCOPY Razor suggested above, it's the quickest and easiest one to test, although since the problem only happens intermittently, it may take a week or so to determine if that actually was the fix. I'm going on vacation for 10 days so will have to pick up on it when I return. If this thread needs to be completed/closed before then, I'll understand. If the thread can stay open I'll be sure and post back with what fixed it. I'm also looking into Robocopy and Richcopy which I understand offer more copy options.

Thank you.

Thank you.

Report •

April 16, 2010 at 09:23:18
Thank you all for your tips and suggestions. I am going to look into Robocopy as a better solution when I get a chance. But in the meantime the Z switch seems to be working pretty good.

Report •

Ask Question