PDA

View Full Version : ATTN: Batch File Writters!


samuraisam
06-30-05, 03:07 PM
I'm not experienced in writting batch files and I need one to back up an entire hard drive nightly, but only back up the files that have been changed or are new. I'm assuming this can be done with batch...? If not, let me know, and if you know a program that can do this (for free, mind you) nightly. If I can do this with a batch file, that would be great. :)

Thanks,
-Sam

seadave77
06-30-05, 03:13 PM
If you're running XP Pro, the backup utility can do this for you. If not, I'll have to look into it.

Slackfumasta
06-30-05, 03:20 PM
You could use:

xcopy <source drive>:\*.* <destination drive:\<folder> /e /v /y /c /r

That will copy everything from A to B, including empty directories. It will continue on errors, overwrite existing read-only files, not prompt for anything, and verify each copy.

It will not copy only new or changed files however. If you want to do that, download the Winzip Command Line tool from www.winzip.com (you will need Winzip 9.0 to use this), and use a script along the lines of this:

C:\"Program Files"\WinZip\wzzip -u -rP -wsh <destination> <source1> <source2> <source3...>

The destination should be a folder path with a .zip file at the end. What this will do is create a .zip file, copy all files from the <source> directories (you can specify more than one source, such as "c:\documents and settings\user\desktop" and "c:\documents and settings\user\my documents", or you can just tell it to copy c:\ and get everything on the drive), and it saves all path information. Then, each subsequent time you run it, it will only copy files that are new/changed.

You should register Winzip if you want to do this; if you don't, it will still work, but it will require you to hit a keystroke before it actually starts the backup. Registering it removes that limitation, and it's cheap enough for the great job it does.

You can also add a -x@exclude.lst argument to that Winzip command, and inside that exclude.lst file (which should be in the same Winzip folder you are starting the batch file from) put something like this:


*.mp3
*.bmp
*.gif
*.jpg
*.exe
*.css
*.tmp


What this will do is to exclude any file types that you specify, so you don't end up copying useless stuff like bitmaps. You wouldn't want to exclude stuff like that if you are backing up user data though, they may have stuff that they need, depending on their job.

Whichever method you choose, you can use the Scheduled Tasks feature to schedule the batch file to run at a specified time. I would not recommend copying an entire drive if that drive has program files and the OS on it, but if it's a pure data drive, go for it.

redduc900
06-30-05, 03:48 PM
Like seadave77 suggested, you could also use the built-in (assuming you're running XP Pro) backup software to perform nightly backups. Start | Run | Type ntbackup, and click OK | When the "Backup or Restore Wizard" opens, click on "Advanced Mode" | "Scheduled Jobs" tab | Double click a date, or click the "Add Job" button | A Wizard will help guide you through the steps of scheduling a backup.

If you're running XP Home, ntbackup isn't installed by default. You'll need to install ntbackup from the valueadd folder on the XP Home CD...retail version.

Slackfumasta
06-30-05, 04:34 PM
If you use the ntbackup utility, set it up so that it does a full backup once a week, and then a differential backup on the rest of the days of the week; that way you aren't doing a full backup every night. A differential backup will only backup files that have been changed/created.

samuraisam
07-01-05, 09:22 AM
So basically we have to /pay/ for the same thing tar does?

Bah. Is there something like 'winzip command line' that's free?

-Sam

Slackfumasta
07-01-05, 10:04 AM
Winzip command line is free, but it just pops up a message saying "Thank you for using an evaluation copy of Winzip. If you do not want to see this message, please register this product" or some such wording.

It has the same functionality, but you have to acknowledge the message before it will actually run a backup.

P.S. It's not the Winzip command line function that needs to be purchased, it's Winzip itself if you don't want the message popping up.

JigPu
07-01-05, 12:13 PM
XCOPY can do it, but it will take a bit of work. Firstly, you need the date option so that you can copy files made/modified after the last backup:
/D:m-d-y Copies files changed on or after the specified date.
If no date is given, copies only those files whose
source time is newer than the destination time.

Secondly, you need some method of "remembering" the date of the last backup, since XCOPY can't do it for you. Using a file with only a timestamp shouldn't be too hard:
date /t > lastbackup.txt

Finally, you need a way to access this timestamp, which can be done by storing the various parts of the stamp into variables like so:
for /f "tokens=2-4 delims=/ " %%a in (lastbackup.txt) do (
set mm=%%a
set dd=%%b
set yyyy=%%c
)


The full batch file would then be:
for /f "tokens=2-4 delims=/ " %%a in (lastbackup.txt) do (
set mm=%%a
set dd=%%b
set yyyy=%%c
)

date /t > lastbackup.txt

xcopy <source drive>:\*.* <destination drive:\<folder> /e /v /y /c /r /k /D:%mm%-%dd%-%yyyy%

You may want to go through the list of options for XCOPY, since there are a number that you may or may not want depending on the situation (for example, /O may be important). Also, this dosen't compress the backup (as tar could), but it shouldn't be too hard to find something which can. For that matter, if you really like tar, you can find Win32 ports fairly easily (for example, in the package here (http://unxutils.sourceforge.net/) there is a tar.exe) :)

JigPu