Subscribe to our rss feed

Create a Windows Daily Backup Script

Posted in Administration Tutorials, Windows Tutorials by John Ward on the May 9th, 2007

This tutorial will show you how to use a simple Windows Batch file and the task scheduler to create an automated daily backup. We will use the date variable to backup the files into folders with the first three letters of the day as the folder name.

First you will need to open notepad. The easiest way is going to start > run and typing notepad.
Create a Windows Daily Backup Script

We will use notepad to create are batch file. A windows batch file is a set of instructions that tells the computer what to do. All we really need to do is add the command to copy the folder that we want to backup. I have a folder called ‘test’ that I want to backup to a folder called ‘backup on a network drive which I have mapped as the x drive. It only takes one simple command to do that actual backup. (note: there is no | in the command next the the word date, that is just the cursors)
Create a Windows Daily Backup Script

Now I will explain the command “xcopy c:\test\* x:\backup\%date:~0,3%\* /Y /Q /S”

The xcopy commands will copy files and folders. The syntax for the command is xcopy [source] [destination] [arguments]. Where source is the folder that you want to backup and destination is the backup on the network drive. The arguments tell xcopy how to handle certain situations. For my script I used /Y to suppress prompting when overwriting files. If you do not include this the script will ask you before copying each file, that wouldn’t be automated. /Q doesn’t display the file names while copying. /S copies directories and sub directories. You can find more arguments by opening the command windows and typing xcopy /?.

Where you see \%date:~0,3%\ is the name of the destination folder. If you didn’t want to change the name daily you could simple call it \backup\ or something like that. The %date% is a variable. If you type %date at the command line, you will get the current date and time output. The :~0,3 tells the command line to return the first three letters of that date (0-3) and trim off the rest.

Once you have made the changes that you nee, save the file as backup.bat
Create a Windows Daily Backup Script

Now Open the Control Panel and select Scheduled Tasks
Create a Windows Daily Backup Script

Now we want to add a new task so click the “Add Scheduled Task” button.
Create a Windows Daily Backup Script

Click next and then browse. Locate the backup script you just saved and click open.
Create a Windows Daily Backup Script

Select perform this action daily and then click the next button.
Create a Windows Daily Backup Script

Select enter the time you want the task to run at in the start time box. Then make sure daily is selected and change the date to the day you would like this task to start running on.
Create a Windows Daily Backup Script

Enter your password and click next. (note: if you do not have a password, you may have problems with the scheduled tasks running correctly. I recommend adding a password to your account, or setting up a separate account to run scripts.)
Create a Windows Daily Backup Script

Click finish. Now we can test the backup script. Located the script in the scheduled tasks folder, Right click it and select run.
Create a Windows Daily Backup Script

You should see the command window open. When the task is finished, browse to your backup folder and verify that the files have been copied.
Create a Windows Daily Backup Script

This is a very basic idea of how to use the task scheduler and batch files to make backups.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Reddit

48 Responses to 'Create a Windows Daily Backup Script'

Subscribe to comments with RSS or TrackBack to 'Create a Windows Daily Backup Script'.

  1. sandesh said,

    on May 17th, 2007 at 8:26 am

    i am unable to copy all files & folders, if i run the script only one files is copying.

  2. John Ward said,

    on May 17th, 2007 at 9:32 am

    Look at the first part of the command. In my example it is copy c:\test\*

    Make sure that you use the asterisks(*). It is a wild card that tells the command to copy everything in the folder.

    You could also use the * to copy all of a certain type of file. For example copy *.jpg would copy all jpg files.

  3. Dave said,

    on May 23rd, 2007 at 6:16 am

    This is just what I was looking for except that I need to have the whole date recorded in the folder name, preferably ‘year_month_day_backup’. When I run your script without limiting the output from date, I recieve the following error:
    \Wed 05/23/2007\* /Y /Q /S
    Invalid number of parameters

    Obviously a format not allowed!

    Can you help me achieve what I need to do?

    Thanks

  4. Dave said,

    on June 5th, 2007 at 4:01 am

    Anybody out there able to answer this please?

  5. John Ward said,

    on June 5th, 2007 at 7:49 am

    %date:~0,3% That tells the script to out put 3 characters from the 0 space. So you could tell it to out put the year by using the characters. (%date:~10,4%) and then do the same for the day and year.

    Try this at command line.
    echo %date:~10,4%_%date:~4,2%_%date:~7,2%_backup

    Hope that helps. Sorry it took so long to get back to you, I didn’t understand the question at first.

  6. Dave said,

    on June 6th, 2007 at 5:54 am

    Perfect!
    Thank you John!

  7. Jim Barbera said,

    on August 2nd, 2007 at 11:01 pm

    For the folder name as date to work, you need to change the date format in regional settings to replace slashes with dashes.

    I also rearranged the short date format to YYYY-MM-DD since that’s how I name all folders as dates. This way, alphabetical filename sorting will produce a neat list in chronological order top to bottom.

    Here’s how. Click the following:

    Start
    Control Panel
    Regional and Language Options
    Regional Options tab
    Customize button
    Date tab
    Short date format: select yyyy-MM-dd
    Date Separator: select “-” (Dash) or “.” (Dot) if you prefer.
    OK
    OK

    My backup batch file to back up My Documents, Firefox, and Email looks like this:
    xcopy C:\docume~1\user\MYDOCU~1\* I:\Backup\”%date%”\My_Documents\* /Y /Q /S
    xcopy C:\docume~1\user\Desktop\* I:\Backup\”%date%”\Desktop\* /Y /Q /S
    xcopy C:\docume~1\user\APPLIC~1\Mozilla\* I:\Backup\”%date%”\Mozilla\* /Y /Q /S

    Hope this helps.

    -Jim Barbera

  8. Bin said,

    on August 3rd, 2007 at 5:07 am

    Hi,

    Am able to do something like this at all?

    xcopy \\server01\Test\* etc…..

    I really need this….

    Thanks

    Bin


  9. on August 3rd, 2007 at 6:44 am

    Bin,

    I don’t see a problem with using a UNC address when copying. Have you tried it?

  10. Bin said,

    on August 3rd, 2007 at 10:08 am

    I tried it but hasnt worked for me. Thought that it may not work for UNC paths. Any chance you would be able to confirm?

    Many Thanks

    Bin


  11. on August 3rd, 2007 at 10:15 am

    Yes. UNC copying works for me.

  12. Bin said,

    on August 3rd, 2007 at 10:20 am

    Hi, Many thanks. I will check why mine is not working.

    Thanks

    Binesh

  13. Jim Barbera said,

    on August 3rd, 2007 at 12:51 pm

    Thanks John Ward! Your explanation of the Date naming function now makes sense. I was having trouble figuring it out. Here’s what I came up with…

    If you don’t want to change the date format on your machine, you can set up the date naming function like this:

    Instead of
    %date%

    Use
    %date:~10,4%_%date:~4,2%_%date:~7,2%

    To back up your My Documents folder to the “I” drive, your line would look like this:
    xcopy C:\docume~1\user\MYDOCU~1\* I:\backup\%date:~10,4%_%date:~4,2%_%date:~7,2%\My_Documents\* /Y /Q /S

    This will result in a valid backup folder name as YYYY-MM-DD.

    -Jim

  14. Scott said,

    on August 7th, 2007 at 7:35 pm

    Thanks, good tip. UNC worked fine for me as well. I used this to copy from one server to another where the backup tape resides

  15. thib´ said,

    on August 13th, 2007 at 7:27 pm

    Hi,

    For more advanced users, don’t forget that WinRAR has command line features (use “rar.exe /?” and “unrar.exe /?” in the WinRAR installation directory). You can then auto-archive your backups to save place. Also, I recommand GnuPG for encryption (which is also essentially CL). I don’t have much time to write a full script here (actually just thought of it after seeing this great tutorial), but by reading the doc and help files of each software, I’m sure you can make a good auto-backup-packager-encrypter without any difficulties ;).

    -thib´

  16. John Ward said,

    on August 13th, 2007 at 8:33 pm

    Great suggestion thib. Adding a win rar command would be a good idea. We also used pkzip on my last job to do some backup scripts.

  17. Ken Allen said,

    on August 14th, 2007 at 3:16 pm

    i’m trying to set up a batch file for each day of the week and then an overall weekly backup. so i have various folders set up. i’m having trouble using the date variable to find the files that have been modified that day and copy them to the appropriate folder. the beginning of my batch file looks like:

    @echo off
    :: variables
    set drive=F:\backups1_monday
    set backupcmd=xcopy /s /c /d:%date% /e /h /i /r /y

    but it bombs because %date% wants to return Tue August 14, 2007 and d:Tue doesn’t mean anything.

    any ideas? sorry for being a newbie - thanks for the examples to work from as they have been very helpful.

  18. John Ward said,

    on August 14th, 2007 at 6:32 pm

    What are you trying to copy from? Is the folder named tuesday? I didn’t really write this script to copy only files that changed on a certain day. The purpose is to do a full backup daily and then name the back up whatever that date is. Then you could do a backup at the end of the week and back all those up and name it.

  19. Joseph Abasolo said,

    on August 16th, 2007 at 6:17 am

    Great help! thanks dude ^_^ , im currently backing up daily and my script only has
    a xcopy command. AHU! AHU! AHU! (spartans)

  20. balwinder said,

    on August 21st, 2007 at 10:49 pm

    your command help me lot, but i am need time to display

  21. balwinder said,

    on August 22nd, 2007 at 4:47 am

    please help me——this command help;s me but i want me file name should be

    i.e. 2007_08_20_12:20:20

    i.e. YYYY_MM_DD_HH:MM:SS

    please held

  22. John Ward said,

    on August 22nd, 2007 at 6:48 pm

    To Balwinder
    This command can easily be replaced with the time variable. This is a system variable and you can call it using %time% just like I did with the date. Then you will need to adjust the format using the ~and select the characters as shown in the tutorial.

  23. John Ward said,

    on August 22nd, 2007 at 6:57 pm

    @balwinder again. Where you see %date% that will display the default date for your system. So mine will display: Wed 08/22/2007. So for example if you want to display the year, it would be something like %date:~10,4%. That is telling the script to display only from the 10th position (the position is the space in front of the character, so starting in front of the W will be position 0, count over to the space in front of the character you would like to use) and the 4 tells our script to display 4 characters after the selected position(10). I don’t have time to test this so I will give it a shot, you will have to adjust the code as needed. Your code would look something like this (if you have standard us date/time settings for the environmental variable):

    xcopy c:\WhatYouWantToCopy C:\BackupFolderName\%date:~10,4%_
    %date:~7,2%_%date:~4,2%_%time:~0,2%_%time:~3,2%_%time:~6,2%

    * No spaces. I had to add one to fit it in the comment box on this website. I had to replace your colons (:) with underscores (_) because a colon is not a valid character in a Windows folder name.

  24. Balwinder said,

    on September 1st, 2007 at 6:06 am

    hi

    sorry, John Ward, your script contain error please check at your end

    and get me a solution to —– folder name as

    c:\2007_08_20_12:20:20\

    i.e. YYYY_MM_DD_HH:MM:SS

  25. John Ward said,

    on September 1st, 2007 at 8:52 pm

    I already explained to you that YOU CANNOT USE A COLON (:) TO NAME A FOLDER. This not a valid character for a windows folder name. Replace it with some other character. I am not going to rewrite the script. I am sure you can figure it out.I didn’t write this tutorial so I could write everybody’s backup script for them. I have no problem answering the occasional question, but I am not going to do your work for you. If you follow this tutorial and read through the comments you should have no problem creating your own version of the script.

  26. balwinder said,

    on September 5th, 2007 at 5:54 am

    hi

    sorry, John Ward, your script contain error please check at your end

    this is not a matter of

    COLON (:)

    please check

  27. John Ward said,

    on September 5th, 2007 at 11:51 am

    Read the tutorial again, you can figure it out.

  28. Damany Harden said,

    on September 10th, 2007 at 7:07 pm

    Is there any way to exclude files or folders in the script? For instance I want to backup the My Documents folder but not the My Music folder inside My Documents.

    Thx in advance

  29. John Ward said,

    on September 10th, 2007 at 7:34 pm

    Thats a good question. You may have to do each folder separately. I will look into that though and try to get back to you.

  30. Damany Harden said,

    on September 21st, 2007 at 4:37 pm

    This script is diabolical - It answers the question of excluding folders and more. It is pieced together from a variety of sources and modified for my need. The modifications are easy to fit anyone’s need.
    ———————————————————————————————————–
    ::set username if you are mounting to a network drive or some location that
    ::requires a username and password. In this case it is using the windows ::account password

    set USER=xxx

    ::mount drive - I am mounting a network drive and map it to the U drive.
    ::The start /wait allows time for the drive to mount before moving on to the ::next process

    start /wait NET USE U: \\(your_server)\(some_directory) /USER:%USER% ‘password’

    @echo off
    ::create a file with a running list of directories, files and/or file types you ::want to exclude. In my case I want to exclude certain folders. See ::further down example of exclude file. I copy the exclude txt file to a tmp ::file just incase the txt file gets caught up in some other process. I save ::the exclude txt in my docs because I want to back it up as well, so I will ::always have a up-to-date copy with each backup.

    copy “%USERPROFILE%\My Documents\exclude_folders.txt” exclude.tmp

    :: variables
    set drive=U:\.backup
    set folder=_%date:~10,4%_%date:~7,2%_%date:~4,2%
    set backupcmd=xcopy /s /c /d /e /h /i /r /y /v

    ::My Documents has its own variable because of the exclude. I don’t want ::certain directories to be backed up. here is where the exclude file is ::called

    set mydocbackup=xcopy /s /c /d /e /h /i /r /y /v /exclude:exclude.tmp

    :: THE BACKUP
    echo ### Backing up My Documents…
    %mydocbackup% “%USERPROFILE%\My Documents” “%drive%\My Documents%folder%”

    echo ### Backing up IE Favorites and Firefox Bookmarks…
    %backupcmd% “%USERPROFILE%\Favorites” “%drive%\Favorites%folder%”
    %backupcmd% “%USERPROFILE%\Application Data\Mozilla\Firefox\Profiles” “%drive%\Mozilla%folder%”

    echo ### Backing up email and contacts (MS Outlook)…
    ::%backupcmd% “%USERPROFILE%\Local Settings\Application Data\Microsoft\Outlook” “%drive%\Outlook%folder%”

    :: use below syntax to backup other directories…
    :: %backupcmd% “…source directory…” “%drive%\…destination dir…”

    del exclude.tmp

    ::Disconnect the mapped drive
    start /wait NET USE U: /DELETE

    echo Backup Complete!
    @pause
    ———————————————————————————————-

    This is my “exclude_folders.txt” content in the exact format

    My Documents\My Music
    My Documents\My Pictures

  31. Damany Harden said,

    on September 21st, 2007 at 4:40 pm

    Script without all the comments

    set USER=xxx

    start /wait NET USE U: \\(your_server)\(some_directory) /USER:%USER% ‘password’

    @echo off
    copy “%USERPROFILE%\My Documents\exclude_folders.txt” exclude.tmp

    :: variables
    set drive=U:\.backup
    set folder=_%date:~10,4%_%date:~7,2%_%date:~4,2%
    set backupcmd=xcopy /s /c /d /e /h /i /r /y /v
    set mydocbackup=xcopy /s /c /d /e /h /i /r /y /v /exclude:exclude.tmp

    echo ### Backing up My Documents…
    %mydocbackup% “%USERPROFILE%\My Documents” “%drive%\My Documents%folder%”

    echo ### Backing up IE Favorites and Firefox Bookmarks…
    %backupcmd% “%USERPROFILE%\Favorites” “%drive%\Favorites%folder%”
    %backupcmd% “%USERPROFILE%\Application Data\Mozilla\Firefox\Profiles” “%drive%\Mozilla%folder%”

    echo ### Backing up email and contacts (MS Outlook)…
    ::%backupcmd% “%USERPROFILE%\Local Settings\Application Data\Microsoft\Outlook” “%drive%\Outlook%folder%”

    :: use below syntax to backup other directories…
    :: %backupcmd% “…source directory…” “%drive%\…destination dir…”

    del exclude.tmp

    start /wait NET USE U: /DELETE

    echo Backup Complete!
    @pause

  32. Momo said,

    on September 28th, 2007 at 2:02 pm

    Anyway to have this run more than once a day?

  33. John Ward said,

    on September 28th, 2007 at 3:05 pm

    The only way I can think of to have it run multiple times a day is to set up several scheduled tasks. For example you can have 1 run everyday at 1pm and 1 run every day at 1am. I don’t think windows task scheduler has an option to do hourly. Maybe a third party scheduler would do the trick?

  34. abazari said,

    on March 1st, 2008 at 2:05 am

    Hi tanks for Help
    For Create Zip File Name From Date System You must Install “Command Line Support Add-On” on winzip 8 or 9 and cerate a .bat file exclude:
    ———
    Path \\computer1\e$\Backup_Commander\WinZip
    wzzip -P -r -m -Jhrs -whs \\computer2\d$\Zip_Surce\%date:~10,4%_%date:~4,2%_%date:~7,2% \\computer1\e$\Backup\*.*
    —–
    this command cerate a zip file from current date and move data in this zip file


  35. on May 15th, 2008 at 4:42 pm

    Fantastic posts I am highly impressed and if you could guide me to a list of system variables and commands available in dos.

  36. Strojki said,

    on July 29th, 2008 at 5:46 am

    hi i have a question i already tried this backup but it said invalid number of parameters what should i do ?? Tnx in advance

  37. shawn said,

    on August 1st, 2008 at 11:53 pm

    Am I missing something or is not using the veritas backup utilitiy built into win2k and xp easier to config and use?!?!?!

  38. Anand said,

    on August 21st, 2008 at 8:49 am

    Thanks John!!!!!

    It worked great….

  39. Shilpam said,

    on August 25th, 2008 at 5:40 am

    please help to archive the given file in the window

  40. ikraam said,

    on September 2nd, 2008 at 4:53 am

    Hi

    I am trying to copy a ……\backup\*.bak file from MSSQL to another folder on the mapped network drive but i am having an issue that it does not copy the .bak file to another folder even if the destination folder is on the local drive
    (xcopy c:\……\backup\*.bak z:\Backup) that is the command line that i am using.

    I am guessing with my little knowledge of script writing that this is incorrect as its not working?LOL

    Please i need a little assistance in regards to get this working

    P.S. i even stopped the service of SQL to make sure that the program is not using the .bak file while copying the file but that too does not copy the file over

    your help will really be appreciated
    Thanks

  41. peter said,

    on September 18th, 2008 at 11:27 am

    hi john,
    I have an issue. I need to backup at three servers whoes date format are different from one another.

    I there any other option so that If I run the backup batch file once, it should backup the source data to the target folder say sunday (folder name) and similarly if I run the same batch file the next time it has to automatically backup the source data to another folder called monday and so on… for the entire week (7 days). I use xcopy …. but no clue how to achieve it.

    Pls help . thanks

  42. Babak said,

    on September 28th, 2008 at 3:41 pm

    I’ve written something similar to this , but more advanced.
    I add some simple lines of code to add backup folder like this
    \yyyy\mm\dd\backup
    and also generate a log file
    Check my post out:
    http://zangoole.com/1387/07/07/batch-backup-script/
    Unfortunately It’s Farsi (Persian) but take a look at the code.


  43. on September 28th, 2008 at 3:43 pm

    [...] اینجای کار را از TeamTutorials یاد گرفتم. اما زنگوله این وسط چی کاره است؟ یک مترجم خشک [...]

  44. amir said,

    on October 5th, 2008 at 1:01 pm

    Look at the comment above, he’s an Iranian…

  45. mitch said,

    on October 13th, 2008 at 3:45 am

    hey John i was wondering what exactly does this backup do if u can answer me please email me at x4.20mitchx@hotmail.com

  46. mitch said,

    on October 13th, 2008 at 3:47 am

    what is backing up files good for and y do ppl do it??? plz hlp hehe

  47. Scott said,

    on November 10th, 2008 at 5:01 pm

    Thanks for the help, really great guide you have going here.

  48. Killer said,

    on December 10th, 2008 at 1:52 am

    Didn’t quite see the answer to the question regarding backups of “newest” files.

    I want to create a copy of a music directory and then run your script nightly so that if there are new mp3 files added to the source directory it will only copy those files.

    Any ideas?

    Thanks in advance.

Leave a Comment