Using batch files to do daily backups

Often times there are situations where you want to use batch files that run on a continuous basis but are aware of the date that they are running on.

Let me illustrate by using as example. Let’s say that I want to backup a critical executable file on a daily basis by simply using the copy command in DOS but I want the backup filename to reflect today’s date. If today was 08/09/2007, i want my target filename to be backup20070809.exe

In order to accomplish that with a batch file, use the following code-

date /t > temp.txt
set /p date= < temp.txt
del temp.txt

echo %date:~4,2%
echo %date:~7,2%
echo %date:~10,4%

The last three lines can be taken out and you can use the code from the last 3 lines to perform your backup operation

5 Responses to “Using batch files to do daily backups”

  1. Arpan sharma Says:

    hi boss

    Can u help me in dos batch files plz?? reply me on

    arpansharma2000@hotmail.com

  2. hello Says:

    hello!can you help me, please???i want to backup a directory every day(with a batch file), but i want to update only the things that change not the entire dir.Also the backup directory should change the name with the new date! i know how to backup using batchfile, but it creates one directory for each day!i want to renew(backup) the directory only if it is made a change, and changing also the name of the directory. tankyou!
    you can contact me on gherciudan06@yahoo.it

  3. Pranav Says:

    Hi,
    Can you please send me the code you already have so I can better understand what you are trying to do. However, if you just want to make backup of data, I recommend that you try mozy.com – you get 2GB of free backup (full & incremental)
    -Pranav

  4. gherciu dan Says:

    hello!look at this batch (I put it as a scheduled task at 0:00 for all week long)
    @echo off
    :: variables
    set drive=e:\Backup
    set folder=”%date:~0,2%-%date:~3,2%-%date:~6,6%”
    set backupcmd=xcopy /s /c /d /e /h /i /r /k /y
    echo ### Backing up directory…
    %backupcmd% “C:\Programmi\Acer” “%drive%\%folder%”
    echo Backup Complete!
    @pause

    this batch create a dir. in e:\backup\currentdate with the current backup
    but tommorow I don’t change the source dir, so it should not create a new backup directory
    after tommorow I make some changes in the source dir, so it should backup everything in a new dir with the current date
    the question is: how to compare two directories to see the differences and how to implement it in a batch file(it will be an “if function” ??? or comp???)

    thankyou

  5. Pranav Says:

    I recommend you take a look at this free utility: http://www.download.com/Everyday-Auto-Backup/3000-2242_4-10713232.html?tag=lst-0-5

    If you want to do this with a batch file, then you have to get the ‘Last Modified’ date for your folder and then compare it to the last modified date from your last backup. You can just dump the last modified date into a text file right after your xcopy command.

    The main problem is how to get the last modified date of a folder. I’ve used a utility in the past called modtime.exe that does exactly that. However a quick google search didn’t reveal a download link for it - see if you can find the same utility or something similar.

    Batch files DO take IF statements. Take a look at microsoft’s documentation for batch files- http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true

    Hope that helps.

Leave a Reply