# Batch file / script backup help



## Headshot_Harry (Apr 23, 2009)

Hi all, need a little bit of advice if possible.

I'm absolutely no developer what so ever, and rarely can grasp even simple coding! LOL, that includes batch script! 
 


But... I'm trying to automate a small backup procedure that copies data from a documents directory to a USB disk (only new files, or data that has been modified or changed), then verifies that the backup completed successfully by comparing the data in the original location with the data in the backup directory on the USB disk, and *then *send out an email out *only* if the backup was unsuccessful (_this is all done with Windows Vista / XP)_.

So far I've locked the USB disk to a certain drive letter (J: to make life easier should the disk be unplugged etc.

I've managed to get the copy part of the batch script to work using XCopy with some of its various switches, and Ive also got the email part working using the 'Blat' cmd line software.

The bit I'm struggling with is to get the script to do the compare of directories, and only start the email procedure if there are inconsistencies within the two folders.

Any chance someone who actually knows what they are doing helping me out?

Many thanks in advance.

 [Edit, my experience is not advanced! Must change that! - I am an IT consultant by trade, but am only a beginner with things like scripting, even at a basic level! So be gentle please )


----------



## TheOutcaste (Aug 8, 2007)

Welcome to TSG!

How do you want to do the compare?

File name, size, attributes (R-H-S-A-C), date modified, date accessed, or a byte by byte comparison? (Sounds like the latter).
Is this one document folder (and subfolders) shared by all users of the PC?
...example, copying C:\Docs to J:\Docs
Or is there a document folder in each User's profile?
...If multiple folders, the backup path should include the Username.
Is the same USB stick used on different PCs?
...If so, the backup path should include the PC name.
If files are missing or don't compare, do you need the file name emailed, or just logged with the email noting the error?

The above info would be needed as a start. Would probably be useful if you can post your script. Just be sure to modify any info you don't want public, like User/PC names and email info that Blat uses.

Something else to consider:
Do you need to keep multiple versions of files?
...Backup all files to a dated folder, deleting old folders as needed, or
...Take a base backup once every XX days, and copy only changed files to different, dated folders.

Some options and methods
You can add the /V switch to Xcopy that will verify that the copy was successful. This only verifies that the destination file can be read without error, it doesn't do a byte by byte comparison.

You can run Xcopy in List Only mode twice, once from Source to Destination, once Destination to Source. If both directions return zero files then the dates and names are the same, and there are no missing files.
This won't check size or file attributes, or that the contents of the files are the same.

You can run the fc.exe program to do a byte by byte comparison. If no files are missing fairly easy to do, but time consuming. You could limit it to only run fc.exe on files modified since the last backup, or in the last XX days.

Robocopy (comes with Vista, available for XP) is much more flexible, and can show if the file sizes, time stamps or attributes are different, and if the file exists only in the Source or only in the Destination, all in one pass. It only flags files sizes as different if the time stamps are the same though. This would only be an issue if the same USB stick is used on multiple PCs and you don't use different folders for each PC, or someone is deliberately messing with things.

Does your batch file run from the USB stick? If so, and the USB stick is shared, you can put the XP version of Robocopy on it, and just have it check the OS; If it's XP, use Robocopy from the USB stick, no need to install it on every XP system. (Installing is just a matter of copying the file to the Windows\system32 folder though so not too big a deal)

Does your script delete files from the backup that no longer exist in the source? (Not a good idea to run automatically!)

Files accidentally deleted should generate an email, but if you deliberately delete a file from the source, that would also generate an email unless you also delete or archive the file from the destination.

You could include a routine that will delete (or archive) files in the destination that no longer exist in the source if they are more than XX days old. During the XX day grace period, they would still generate an email each time the backup runs.

What I would do is have files in the destination that are missing from the source be archived automatically, and send an email. That way the email will only be sent once, and the file will still be available in the archive folder if the deletion was by mistake. If it was deliberate, the email can be ignored.
You can then run an archive clean up script (or make it part of the backup script) to delete files in the archive folder that are more than XX days old.

Jerry


----------



## PatrickMc (Jun 5, 2009)

You want to copy all files from "somefolder" (this is the path) to J: drive which is a USB stick. You want to copy only the files that were modified since the last backup. Use a file "somefolder/BackupMarker.txt" as the back up marker. Each time we do a backup, we will update the modification time of the BackupMarker. Next time, we will only back up files that were modified since the last backup time.

Here is the script.


```
# Last backup time.
var str lastbackuptime
af "somefolder/BackupMarker.txt" > null
if ($fexists)
    # $fmtime (system variable) now contains mod time of "somefolder/backupMarker.txt".
    set $lastbackuptime = $fmtime    # First time, the marker file will not exist.
endif
 
# Get list of files. Go thru files one by one.
var str list ; lf -n "*" "somefolder" > $list
while ($list <> "")
do
    # Get the next file. Is its modification time later than $lastbackuptime ?
    var str file ; lex "1" $list > $file
    af $file > null
    # $fmtime (system variable) now contains mod time of $file.
    if ($fmtime > $lastbackuptime)
        # File was modified after $lastbackuptime. Copy.
        system copy ("\""+$file+"\"") "J:/"
    endif
done
 
# Update the modification time of the marker.
echo > "somefolder/BackupMarker.txt"
```
The script is in biterscripting ( http://www.biterscripting.com ) . Feel free to modify to work with your favorite scripting/batch language.

Patrick


----------



## TheOutcaste (Aug 8, 2007)

Welcome to TSG!

This seems much easier:
*xcopy /d "somefolder" J:\
*
and will backup the files that the script will miss; the script won't backup a file that did not previously exist if it's modified time is earlier than the last backup. Files moved or copied into "somefolder" will retain their modified timestamp and could be missed.

Now an easy way to compare the file _contents_ to make sure they match would be useful, and to flag missing and extra files, if the OP also needs that. Hard to guess since he hasn't replied.


----------



## PatrickMc (Jun 5, 2009)

Outcaste:



> the script won't backup a file that did not previously exist if it's modified time is earlier than the last backup


Absolutely right. Glad you caught is. I missed it. Perhaps, the code line


```
if ($fmtime > $lastbackuptime)
```
needs to be changed to

```
if ($fmtime > $lastbackuptime) OR ($fctime > $lastbackuptime)
```
$fmtime is mod time, $fctime is creation time. xopy is good too.

Patrick


----------



## TheOutcaste (Aug 8, 2007)

When you move a file, the modified and created timestamps aren't changed, so it would still be possible to add a file to the Source folder that would never be backed up.

You'd need to check the Destination folder, and if the file doesn't exist, always copy.


----------



## chadw76 (Jun 17, 2009)

The following is 3 different versions of the same batch file. First one is set to backup any new or changed documents within My Documents to the network drive H:\My Documents. The second one is set to backup C:\go to C:\back. Last one simply states where to insert your paths. This is a simple batch file for backing up changed or added files to a set path. In order to work you have to save the file as a *.bat file, mine is backup.bat, then simply execute it. You can edit the file with notepad or wordpad. Hope this helps. 


echo off
if exist "H:\" goto GO
goto ERR

:GO
xcopy "C:\Documents and Settings\%username%\My Documents\*.*" "H:\My Documents\*.*" /m/e/y
echo/
echo ** Copy complete! **
echo/
pause
exit

:ERR
echo/
echo ** ERROR - No H:\ drive mapped - files cannot be copied! **
echo/
pause
exit

---------------------------------------------------------------------------
echo off
if exist "C:\" goto GO
goto ERR

:GO
xcopy "C:\go\*.*" "C:\back\*.*" /m/e/y
echo/
echo ** Copy complete! **
echo/
pause
exit

:ERR
echo/
echo ** ERROR - No C:\ drive mapped - files cannot be copied! **
echo/
pause
exit

---------------------------------------------------------------------------

echo off
if exist "C:\" goto GO
goto ERR

:GO
xcopy "INSERT PATH FOR FILES TO BE BACKEDUP\*.*" "INSERT BACKUP PATH\*.*" /m/e/y
echo/
echo ** Copy complete! **
echo/
pause
exit

:ERR
echo/
echo ** ERROR - No C:\ drive mapped - files cannot be copied! **
echo/
pause
exit


----------

