Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 452
  • Last Modified:

Batch File to Copy Dirs to zip file with date


Exactly what I am trying to do:

1. I have 2 directories that need to be backed up.
2. I need to get these folders, subfolders, and files to a server for nightly backup.

What I have done already:

1. I have already manually copied the folder/files to he server.
2. I have written a batch file (very basic), using xcopy, that copies only the files that have updated in 2 directories.

What I would "like" to do:

1. I would like to add to the batch file a way to write the files that were copied to a log.
2. I an also going to add this batch file to scheduler to be run every night at 10pm.
2. If there is a more "correct" way, please suggest!

Some things that have crossed my mind while writing this:

1. Maybe zipping up these directories to a file with the "date" in the file name? But what about logging?
2. Maybe creating new directories on the server with the "end of day date" for the directory?

If anyone has suggestions, a better way, or whatever....any input would be greatly appreciated!

TIA,
Bill
0
wta2
Asked:
wta2
  • 3
  • 2
1 Solution
 
callrsCommented:
0
 
SMFXCommented:
Rather than using xcopy, you may want to look at Robocopy that is part of the Windows Resource Kit as a download from Microsoft:
    http://www.microsoft.com/downloads/details.aspx?FamilyID=9d467a69-57ff-4ae7-96ee-b18c4790cffd&DisplayLang=en

Look at the .DOC that comes with it, but you'd be looking at something like:
    robocopy.exe \\server\source \\machine\destination /E /B /COPYALL /M /LOG+:X:\logfiles\backup.log /TEE

You could also create a job file from this for future use if you'd like using the /SAVE option.

Basically, what this does is copy all files from the source (\\server\source) to the destination (\\machine\destination) in all subdirectories (/E) using a backup method (/B) and include all security, attribute, & ownership attributes of any files and folders (/COPYALL), of only those files that have changed since the last copy (/M), save it to a log file (/LOG+:X:\logfiles\backup.log) and display the action as its happenning (/TEE).

Robocopy is a much more feature rich file copy than xcopy, so is more useful for this items.

HTH,
-SMFX
0
 
SMFXCommented:
Also, if you want to include dates, you can use the system %DATE% variable to accomplish this in a basic fasion.  However, you have account for the special characters in the result.  For instance, on a default install if you type "echo %DATE%" at the command prompt, you'd get something like:
    Sat 07/15/2006

The slashes (/) are invalid and you may not want to bother with the "Sat", so you can just use the variable expansion settings of the command line to strip out only the pieces you want.  The format is:
   %DATE:~S,N%
Where S is the start character and N is the number of characters to include.  So, the month would be %DATE:~4,2%, the day would be %DATE:~7,2%, and the year would be %DATE:~10,4%.  Thus typing "echo %DATE:~4,2%%DATE:~7,2%%DATE:~10,4%":
   07162006
All nice and clean.  Now when you're displaying files, folders, or anything else you may want to sort by the string value, you would probably want to do Reverse Polish Notation or YYYYMMDD.

Okay, add that all to our robocopy command and here you get a nifty little result:
    robocopy.exe "\\server\source" "\\machine\destination\backup_%DATE:~10,4%%DATE:~7,2%%DATE:~4,2%" /E /B /COPYALL /M /LOG+:"X:\logfiles\backup_%DATE:~10,4%%DATE:~7,2%%DATE:~4,2%.log" /TEE

Notice I put the file paths in quotes as some of the paths could wind up with spaces (including the date depending on your system format).  This ensurse robocopy doesn't treat the space as the beginning of the next parameter and screw up the script.

The result is a new directory in your destination titled "backup_YYYYMMDD" that the changed files are copied to.  Also, a seperate corresponding log file for each day, "backup_YYYYMMDD.log".

Finally, if you just want to put them all into one big file, I'd recommend you just use the built-in NTBackup and backup to a file.   Use an incremental backup to only backup the changes.  You can find good documentation in the online doc's of the program (ntbackup.exe).  This will do the scheduling, reporting, logging, and dating for all of your data.

HTH,
-SMFX
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
wta2Author Commented:
Thank you VERY MUCH....this works great...

This may be a stupid question, but how/where does robocopy compare or know what files have been updated? Since the files are not being copied into the same directory every time to compare dates? or ?

Just a little confused.....

Again, Thank you very much...

Bill

0
 
SMFXCommented:
The /M (Modified) parameter looks at the Archive attribute of the file(s).  This is like the system, hidden, and read-only attributes of files.  The Archive attribute is special in that any time a program saves, creates, or changes a file (ie: Modified), it sets the Archive attribute to true.  The /M attribute causes robocopy to copy only those files that have the archive attribute set to true and then changes the flag to false indicating it has been backed up.  Thus next time it runs, it will only copy those files that have been editted and had the Archive attribute set back to true.

There's a lot of good features in robocopy that should be included in the .DOC file; I recommend you take a look at it for more details.  At the very least, just run robocopy.exe /? from a command line or a brief rundown of what's available.

Glad this could help!
-SMFX
0
 
wta2Author Commented:
this works like a champ!!!!  

thanks again!!!!

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now