In my prior post (Puzzle: Why does backing up a data file require web application shut down?) the customer wanted to keep the application running while doing a backup of the data files
The reason the code seemed to work in the VFP IDE is because the repro code was not identical.
SELECTing into a Cursor just opens the DBF and makes an in memory cursor based on the original file. This is extremely fast: so fast that executing it 10 times occurs before the simultaneous XCOPY command opens the FPT file exclusively. This is a race condition. In fact, on my machine, it can do 22 loops before the XCOPY opens the large FPT file exclusively and the error message occurs.
Doing a SELECT using the VFPOleDB Provider into an ADODB Recordset reads through all the records to make the recordset and takes much longer.
Here are a few ways to make the code give the same error:
- Increase the loop count
- Pause a few seconds after launching the XCOPY so that it opens the large FPT file exclusively before doing the SELECT
- SELECT into a TABLE or READWRITE cursor, which forces a scan and copy of all the records
But, you ask, how can I back up files that are in use? Simple. Check out the Shadow Copy feature. From the documentation:
The shadow copy feature in Windows Server 2003 works by making a block-level copy of any changes that have occurred to files since the last shadow copy. Only the changes are copied, not the entire file.
Thus, the changes to a file since the last Shadow Copy are written twice: once to the original and once to the Shadow Copy, so the file can be reconstructed from backup. This allows the file to be copied for backup and still be opened by the application.