Home > Microsoft > Example Backup Batch Script

Example Backup Batch Script

February 14th, 2012

Had to whip up a batch script to backup a data location on a Server 2003 box to an external USB hard drive. Normally this is a pretty simple task, however the client wanted the backup to run nightly and to clean out existing backups older than 30 days. I figured I would share an example of the script here. I am sure there are many better ways to script this, but it was kind of spare of the moment on-site. Feel free to add corrections/suggestions in the comments.

@echo off

REM Delete backups older than 30 days
forfiles /p C:\destination /s /m *.* /d -30 /c “cmd /c del @path”
for /f “delims=” %%d in (‘dir C:\destination /s /b /ad ^| sort /r’) do rd “%%d”

REM Perform backup
set folder=%date:~10,4%%date:~4,2%%date:~7,2%
mkdir C:\destination\%folder%
xcopy /e /c /k /o C:\source C:\destination\%folder%

To get the script to run nightly I just created a “Scheduled Task” for it.

Categories: Microsoft Tags: