Windows – Help for creating a bat file to schedule a backup

backupscheduledwindows

I have a mdb file at C:\Program Files\ApplcationName\filename.mdb. Please let me know the COPY command to take a backup using .bat file to a folder C:\Backup.
In short i am having probles because of the space between "Program" and "Files".

Thanks
jaz

Best Answer

The filename portion of the backup is the worst part to create. You need to be able to create a new file everyday without too much hassle. So here's what I use:

If you combine two of these answers (mine and a previous one) you get this gem:

set mydate=%date:~10,4%_%date:~4,2%_%date:~7,2%
xcopy "%ProgramFiles%\ApplicationName\filename.mdb" C:\Backup\%mydate%_filename.mdb /H /K /O /Y

Doing it this way will give you a file for ever day its run, named with the date.


A complete backup script would look like this

@echo off
:: Yes this looks bad, but it works, it sets the file veriable mydate to YYYY_MM_DAY
set mydate=%date:~10,4%_%date:~4,2%_%date:~7,2%
echo Backing up DC1:
:: start a new backup session, the /M switch is for the type of bakcup being performed, type ntbackup /? for more info
start /wait ntbackup backup \\DC1\c$ /j "DC1 Backup" /f "C:\BAK\DC1\DC1_%mydate%.bkf" /M incremental
echo DC1 is Done
echo Backing up EXCH:
start /wait ntbackup backup \\EXCH\c$ /j "EXCH Backup" /f "C:\BAK\EXCH\EXCH_%mydate%.bkf" /M incremental
echo EXCH is Done
echo Backing up FS1:
start /wait ntbackup backup \\FS1\c$ /j "FS1 Backup" /f "C:\BAK\FS1\FS1_%mydate%.bkf" /M incremental
echo FS1 is Done
echo Backup was completed %date% %time%
pause

This is a complete interactive solution. But the real power is in the set mydate line. Being able to slice a string is a forgotten art in DOS;

set mydate=%date:~10,4%_%date:~4,2%_%date:~7,2%

I take the command output of date, then cut it up, and spit out something that can be used as a filename. That way a new file is created everyday, and you can have a "real" backup system.

Just edit the servernames, DC1, FS1, EXCH and put in your own, or just use drive paths.