Quick and Easy Backup Batch File Windows
Backups are an evil necessity and as with most geek admins we tend to forget to do it. We think of grand schemes and how we can do it better. Well I am still thinking of those grad schemes but in the meantime I use a simple command file to backup file off one drive to another that will only copy newer files. What is so nice with this is I don’t have to have a backup program to retrieve the data, but the drawback is that it uses alot of space. Running it the first time will back them all up but future runs are fast.
The batch file uses xcopy as the start with several switches, displays it in colored backgrounds and writes a logfile when it is done. Here is the simple backup.
@ ECHO OFF rem rem FILE NAME: myback.cmd rem rem UPDATED: May 10, 2011 rem rem Purpose: Backup NAS files to another drive rem rem rem ===================================================================== rem rem cls color 1e @ECHO. @ECHO. @ECHO. @ECHO. @ECHO Press Enter Key to run the program. @ECHO Key Ctrl+C to abort, now or anytime later. @ECHO. @ECHO. @ECHO Backup running...please wait. @ECHO. @ECHO. @ECHO. @ECHO. @ECHO. @ECHO. COLOR 1B @ECHO Copying files with more recent dates... XCOPY "z:\documents\*.*" "i:\backup\documents\*.*" /d /E /R /f /H /Y XCOPY "z:\downloads\*.*" "i:\backup\downloads\*.*" /d /E /R /f /H /Y @ECHO. @ECHO. @ECHO. @ECHO. @ECHO. Echo. >i:\back_doc.txt Echo. >>i:\back_doc.txt Echo. The Following Files Have Been Backed Up >>i:\back_doc.txt Echo. --------------------------------------- >>i:\back_doc.txt Echo. >>i:\back_doc.txt Echo. >>i:\back_doc.txt DIR i:\backup\*.* /-B /-S /on >>i:\back_doc.txt tree i:\backup /A /F >>i:\back_doc.txt REM REM CLS color e9 @ECHO. @ECHO. @ECHO. @ECHO. ECHO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ECHO !! !! ECHO !! BACKUP PROCEDURE HAS COMPLETED. !! ECHO !! !! ECHO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @ECHO. @ECHO. @ECHO. :end
That’s all there is to it. To add more directories just copy one of the lines and change the directory names. For further automation use Windows Scheduled Tasks to add the command to run behind the scenes. You can do this the GUI way or command line. My preference is at the command line.
This example shows to run it weekly
schtasks /Create /SC WEEKLY /TN Backup /ST 03:00 /TR c:\scripts\myback.cmd
Now we are set. For the initial backup you can double click on the command file and watch it run. Then the scheduled task will run much faster.
I get errors when I run this. ‘gt’ is not recognised and I can’t see anywhere that it gets set in the batch file.
That is caused by the post changing the true text. Where ever you see > replace it with >
Meant to say replace the gt and the semicolon with the greater than key on you keyboard. it means to append to the output file.