Solved

Batch File to Copy Dirs to zip file with date

Posted on 2006-07-15
6
440 Views
Last Modified: 2011-10-03

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
Comment
Question by:wta2
  • 3
  • 2
6 Comments
 
LVL 30

Expert Comment

by:callrs
ID: 17116610
0
 
LVL 3

Expert Comment

by:SMFX
ID: 17116624
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
 
LVL 3

Accepted Solution

by:
SMFX earned 125 total points
ID: 17116648
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:wta2
ID: 17118973
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
 
LVL 3

Expert Comment

by:SMFX
ID: 17119603
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
 

Author Comment

by:wta2
ID: 17127107
this works like a champ!!!!  

thanks again!!!!

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes a user will call me frantically, explaining that something has gone wrong and they have tried everything (read - they have messed it up more and now need someone to clean up) and it still does no good, can I help them?!  Usually the standa…
#Citrix #POC #XenDesktop #vCenter #VMware #ESX
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now