Windows Batch Script To Backup Data

As I have briefly mentioned in my previous posts, for my day job I am the primary technical resource for a small business and as a “side gig” I manage web servers for hosting companies. One of the great benefits to this is I have become quite adept at developing command line scripts, or “batch” scripts.

One of the most common, and well suited, applications for a command line script is data backup. Command line scripts can be automated to run at any time without any human interaction and are only limited by… well, nothing.

Why Command Line Scripts?

Having experience with both commercial and free backup programs, I always find command line scripts to be, by far, the most effective tool for the job. Here are a few reasons why:

  • Native Commands: What better way to backup data than by using the functions made available through the program which creates the data. Whether this is the operating system itself via a simple file copy command or a database command to produce a restorable binary file, the source program knows how best to back itself up.
  • Ultimate Control: Since a command line script follows a simple step-by-step procedure, you know exactly what is happening and can easily modify the behavior.
  • Fast: Since everything is a native command, nothing is subject to interpretation. Again, you are using commands provided by the program itself, so overhead is kept to a minimum.
  • Powerful: I have yet to see a backup task which cannot be accomplished through a command line script… and I have done some funky stuff. Albeit, some research and “trial and error” may be required, unless you need something incredibly unique, typically the built in functions and features of the scripting language you are using is more than sufficient.
  • Free and Flexible: Obviously, a command line script does not cost anything (outside the time to develop it), so the emphasis I want to make is command line scripts can be copied to and implemented on other systems and quickly adapted with little to no time or cost. Compare this to the cost of purchasing licenses for backup software on several servers and/or desktop machines.

A Quick Overview Of The Backup Batch Script

Since a lot of people do not have the need/time/desire to learn command line scripting, it is considered somewhat of a “black art”. So to demonstrate the power of the command line, I am providing a simple Windows batch script to backup your important data. This configurable and customizable script does not
require any knowledge (or willingness to learn) of the Windows batch scripting language.

What the backup script does:

  1. Creates full or daily incremental (see below for a definition) backups of files and folders you specify in a separate configuration text file (see below).
    • When a folder is provided, that folder and all sub-folders are backed up.
    • When a file is provided, just that file is backed up.
  2. Compresses (zips) the backed up files. After all files to be backed up are copied, they are compressed to save space. 7-Zip is required to be installed on your system for this to work.
  3. Dates the compressed file and moves it to a storage location. After the backup files are compressed, the resulting archive is given a file name according to the current date and then moved to a configured storage location, such as an external drive or network location.
  4. Cleans up after itself. After all tasks are completed, the batch script cleans up all the temporary files it created.

Requirements:

Windows 2000/XP/2003/Vista,
7-Zip (it’s free).

Configuration file:

The configuration file is simply a text file which contains files and folders to backup, entered one backup item per line. This file must be named “BackupConfig.txt” and be located in the same folder as the backup script. Here is an example of a BackupConfig.txt file (note, the first line is always ignored):

# Enter file and folder names, one per line.
C:\Documents and Settings\Jason Faulkner\Desktop C:\Documents and Settings\Jason Faulkner\My Documents\Important Files C:\Scripts\BackupScript.bat

The example above would backup the Windows user Jason Faulkner’s desktop (and all folders on the desktop), the folder called “Important Files” inside of My Documents (and all folders inside “Important Files”) and the file “BackupScript.bat” inside the C:\Scripts directory.

Types of backups:

  • Full backup: A complete copy of all files and folders (including sub-folders) are included in the backup.
  • Incremental backup: When a folder is provided, only files created or modified on the current date are
    backed up. When a file is provided, it is always backed up, regardless of when it was modified.

The Data Backup Windows Batch Script

I want to emphasize this script is very basic, as all it does is create backups by a utilizing a simple file copy. There are some configuration options you can set:

  • The backup storage location where the resulting compressed backup files are stored.
  • The day of the week the full backup is run (any other day would run an incremental backup).
  • Location of where 7-Zip is installed on your computer. The script is automatically set to look in the default location.

If you have any suggestions or feature requests, please comment below. I would really love to do a follow up article to this post which features an updated script based on reader input.

If you need instructions on how to “use” this script or set up a scheduled task, take a look at the links below the script source.

Without further ado, here it is:
Note: Since the quotes do no display correctly below (and as a result can mess up the script), I have included a plain text link below the script which you can use to get an accurate source to copy from.

@ECHO OFF

REM BackupScript
REM Version 1.01, Updated: 2008-05-21
REM By Jason Faulkner (articles[-at-]132solutions.com)

REM Performs full or incremental backups of folders and files configured by the user.

REM Usage---
REM   > BackupScript

SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

REM ---Configuration Options---

REM Folder location where you want to store the resulting backup archive.
REM This folder must exist. Do not put a '\' on the end, this will be added automatically.
REM You can enter a local path, an external drive letter (ex. F:) or a network location (ex. \\server\backups)
SET BackupStorage=C:\Backup

REM Which day of the week do you want to perform a full backup on?
REM Enter one of the following: Sun, Mon, Tue, Wed, Thu, Fri, Sat, *
REM Any day of the week other than the one specified below will run an incremental backup.
REM If you enter '*', a full backup will be run every time.
SET FullBackupDay=*

REM Location where 7-Zip is installed on your computer.
REM The default is in a folder, '7-Zip' in your Program Files directory.
SET InstallLocationOf7Zip=%ProgramFiles%\7-Zip

REM +-----------------------------------------------------------------------+
REM | Do not change anything below here unless you know what you are doing. |
REM +-----------------------------------------------------------------------+

REM Usage variables.
SET exe7Zip=%InstallLocationOf7Zip%\7z.exe
SET dirTempBackup=%TEMP%\backup
SET filBackupConfig=BackupConfig.txt

REM Validation.
IF NOT EXIST %filBackupConfig% (
  ECHO No configuration file found, missing: %filBackupConfig%
  GOTO End
)
IF NOT EXIST "%exe7Zip%" (
  ECHO 7-Zip is not installed in the location: %dir7Zip%
  ECHO Please update the directory where 7-Zip is installed.
  GOTO End
)

REM Backup variables.
FOR /f "tokens=1,2,3,4 delims=/ " %%a IN ('date /t') DO (
  SET DayOfWeek=%%a
  SET NowDate=%%d-%%b-%%c
  SET FileDate=%%b-%%c-%%d
)

IF {%FullBackupDay%}=={*} SET FullBackupDay=%DayOfWeek%
IF /i {%FullBackupDay%}=={%DayOfWeek%} (
  SET txtBackup=Full
  SET swXCopy=/e
) ELSE (
  SET txtBackup=Incremental
  SET swXCopy=/s /d:%FileDate%
)

ECHO Starting to copy files.
IF NOT EXIST "%dirTempBackup%" MKDIR "%dirTempBackup%"
FOR /f "skip=1 tokens=*" %%A IN (%filBackupConfig%) DO (
  SET Current=%%~A
  IF NOT EXIST "!Current!" (
    ECHO ERROR! Not found: !Current!
  ) ELSE (
    ECHO Copying: !Current!
    SET Destination=%dirTempBackup%\!Current:~0,1!%%~pnxA
    REM Determine if the entry is a file or directory.
    IF "%%~xA"=="" (
      REM Directory.
      XCOPY "!Current!" "!Destination!" /v /c /i /g /h /q /r /y %swXCopy%
    ) ELSE (
      REM File.
      COPY /v /y "!Current!" "!Destination!"
    )
  )
)
ECHO Done copying files.
ECHO.

SET BackupFileDestination=%BackupStorage%\Backup_%FileDate%_%txtBackup%.zip

REM If the backup file exists, remove it in favor of the new file.
IF EXIST "%BackupFileDestination%" DEL /f /q "%BackupFileDestination%"

ECHO Compressing backed up files. (New window)
REM Compress files using 7-Zip in a lower priority process.
START "Compressing Backup. DO NOT CLOSE" /belownormal /wait "%exe7Zip%" a -tzip -r -mx5 "%BackupFileDestination%" "%dirTempBackup%\"
ECHO Done compressing backed up files.
ECHO.

ECHO Cleaning up.
IF EXIST "%dirTempBackup%" RMDIR /s /q "%dirTempBackup%"
ECHO.

:End
ECHO Finished.
ECHO.

ENDLOCAL

Plain text source is available here: Jason Faulkner’s Backup Script

If you need help getting started with implementing this script, here are a couple of links to help you out:

This is the same script I use to backup my computer daily (with a couple of modifications of course), so I know it works very well. I hope you find it useful.

Enjoy.

Opt In Image
Free Weekly PCMech Newsletter
Almost 500 Issues So Far, Received By Thousands Every Week.

The PCMech.com weekly newsletter has been running strong for over 8 years. Sign up to get tech news, updates and exclusive content - right in your inbox. Also get (several) free gifts.

Comments

  1. I’ve always loved the power of scripting in DOS but only knew basic short functions. The higher level stuff can get quite complicated like an acutal application.

    Windows scripting replaced DOS batch stuff, but I’m not a programmer!

    • Jason Faulkner says:

      Actually, batches have not been replaced by Windows Script.

      A Windows Script to do the same thing requires to you create objects and send events to the shell object (MUCH more complicated) whereas Batch Scripts are simply an “automated” command line your script is “dumped” to (with fairly limited control (if,for,etc.) structures).

      Yes, Windows Script can do a lot more complex stuff than Batch Scripts alone, but by no means is it a replacement. It just boils down to some jobs being more suited for one script versus the other.

  2. I’m a 70 year old trying to catchup to all this modern IT stuff, I have an external hard drive and can’t find a simple backup system. I read all articles that you send me including the above and I wish there was a simple program working in the background that backed up everything. I have tried several commercial products but they try to do to much. I tried LINUX but APT’s p***** me off. Keep up the good work. John

  3. This thing is new to me but I think this is really interesting! Right now I’m learning to write macros in ms excel and a little about html and vbscript. I mean, I’m quite interested with stuffs like this. I’d like to learn more about this windows scripting. Can you help me out with this? Where can I find sources about it? ..perhaps some kind of tutorial on how to write it?

    thanks.

    • Jason Faulkner says:

      I learned all this through experience over the years, so I don’t really know where to point you outside of the help documentation (from the command prompt: ‘for /?’, ‘if /?’, etc.).

      Google is always your friend or perhaps a “For Dummies” style book would be a good starting place.

  4. Love it… The no mess no fuss option.
    One of the best dos scripts ive seen, i especially liked the 7 zip compressing, would never of thought of that.

  5. REM > BackupScript
    Neat trick for last run timestamp. What were you going to do with it? Might be better located at end of script.

    SET NowDate=%%d-%%b-%%c
    SET FileDate=%%b-%%c-%%d
    US date format may need local tweaking.

    SET Current=%%~A
    IF NOT EXIST “!Current!” (
    Wow! never new you could substitute ! for % or is something less obvious happening here.

    SET swXCopy=/s /d:%FileDate%
    What about files that changed between last run (yesterday) and midnight this morning?

    REM Determine if the entry is a file or directory.
    IF “%%~xA”==”" (
    Fails if list not include directory entry like:
    C:\path\to\. or C:\path\to – dir /b/s/a:-d does not do this.

    John

    • Jason Faulkner says:

      John, good questions:

      >>SET swXCopy=/s /d:%FileDate%
      >>What about files that changed between last run (yesterday) and midnight this morning?

      The batch file will only copy files modified on the current day, so you would need to have a scheduled task set to run at something like 11:59PM (the “FileDate” variable gets set to the current day) so all the files that day are picked up.

      >>REM Determine if the entry is a file or directory.
      >>IF “%%~xA”==”” (
      >>Fails if list not include directory entry like:
      >>C:\path\to\. or C:\path\to – dir /b/s/a:-d does not do this.

      The configuration file is supposed to include the full directory path or full file name. If you include a “relative” path it will not work… most systems operate like this anyway, so this requirement is not unique.

  6. I have 7-Zip installed under the default folder(C:\Program Files\7-Zip). When I try to run it, I am getting the following message.

    “The system cannot find the path specified.
    7-Zip is not installed in the location: dir7Zip
    Please update the directory where 7-Zip is installed.
    Finished.”

    I have verified and reinstalled 7-Zip but no luck. Please let me know if I need to change anything in the script.

    Thanks,

    JDK

    • Jason Faulkner says:

      It looks like the “ProgramFiles” environment variable is not set on your machine (odd). Here is the fix:

      Replace this line:
      SET InstallLocationOf7Zip=%ProgramFiles%\7-Zip

      With this:
      SET InstallLocationOf7Zip=C:\Program Files\7-Zip

      Basically, we are just hard coding it… of course, this assumes C:\Program Files\7-Zip is indeed where 7-Zip is installed (which should be the case according to your comment).

      • try changing “Program Files” to “Progra~1″ We are working with DOS afterall so the DOS file naming conventions apply, (ie 8.3 character names no spaces/reserved characters etc…)

  7. Thanks Jason, Now,I have setup “Program Files” environment variable and I got the same output. I have also hard coded and I still don’t see 7-Zip.

    Here I have added an extra line echo %exe7Zip% to show you that, it reads the path correctly but it still fails. See below.

    REM Usage variables.
    SET exe7Zip=%InstallLocationOf7Zip%\7z.exe
    SET dirTempBackup=%TEMP%\backup
    SET filBackupConfig=BackupConfig.txt
    echo %exe7Zip%

    With this added in the script, here is my output.

    C:\Scripts>BackupScript.bat
    C:\Program Files\7-Zip\7z.exe
    The system cannot find the path specified.
    7-Zip is not installed in the location: C:\Program Files\7-Zip
    Please update the directory where 7-Zip is installed.
    Finished.

    Question:
    Why this line has double quotation and the other one doesn’t have it?
    IF NOT EXIST “%exe7Zip%”

    IF NOT EXIST %filBackupConfig%

    I removed the quotation and it still didn’t work.
    I hope that this not something to do with copy and paste from the site. Thanks again for your help.

    JDK

  8. It is copy and paste. My bad luck! Now, I am stuck at the last stage. This is where it’s getting an error. Is it possible if you can have a link to the script so people can download it instead of copy and paste? If I am missing the link, please let me know.

    Here is the message.

    C:\Scripts>BackupScript.bat
    Starting to copy files.
    Copying: C:\Documents and Settings\jdk\My Documents\home1
    41 File(s) copied
    Done copying files.

    Compressing backed up files. (New window)
    The system cannot find the file a.
    Done compressing backed up files.

    Cleaning up.

    Finished.

    I am not sure if it’s still caused by copy&paste or this command is not getting executed correctly. I am sure the script is fine and it’s just my bad luck!
    “%exe7Zip%” a -tzip -r -mx5 “%BackupFileDestination%” “%dirTempBackup%\”

    The temp files are getting copied to C:\Documents and Settings\jdk\Local Settings\Temp\backup and then the error message window pops up and it displays the following message:
    “Windows cannot find ‘a’. Make sure you typed the name correctly and then try again. To search for a file, click the Start button, and then click Search”

    Once I click “OK”, then it removes the the above directory “backup”.

    Sorry for cluttering the message board. I just started learning scripting and I have a long way to go. Thanks again for your help.

    • Jason Faulkner says:

      I think it is definitely the copy-paste. I have posted a plain text source link below the script in the article. Try copy and pasting from it.

  9. I saved your .txt file to .bat and I still got the same error message “The system cannot find the file a.”.

    After going through more testing, I was able to run your script but I had to make a minor change. The line where it was giving an error is START “Compressing Backup. DO NOT CLOSE!”. It doesn’t like exclamation mark. Once I removed it, the script worked.

    Thanks for your help.

    Jdk

  10. I can get the script to work, but when I look at my backup zip it creates in the storage Backup folder, it’s not backing up anything and I have the file path’s to folders and directories I want to backup in the BackupConfig.txt file. What am I doing wrong there?
    This is a pretty cool script.

  11. Disregard previous post. Found out what I was doing wrong with typing my path’s in the config text file. Brain froze for a moment but it’s all good now. Thanks for the script Jason and your help with this too JD.

  12. Two questions:

    1) Why 7-Zip? Why not just use the “Compact” command?

    2) I’m certainly no expert on batch files but the script seems overly complex. Why can’t use xcopy with the files you would like to copy. Then use compact to compress them to another destination and lastly delete the copies?

    Are there any books or more in depth references you could provide where people could learn more about this? Thanks for the article it is a good read and refreshing to see people interested in returning to the command line in such a GUI driven computing environment.

    • Jason Faulkner says:

      Samo,

      1. 7-Zip is much (much much) more robust and faster than compact. Additionally, it is the tool I prefer, so that is why I used it in the script. If you prefer to use compact instead, just modify the compression command.

      2. Xcopy performs erratically if you are only copying a single specific file. Copy performs predictably when you are only copying a single file, which is why I used both commands depending on the task needed.

      Again, feel free to modify the script however you see fit so it suits your needs.

      As for additional references, please read my comment above to ‘Nimrod’.

  13. Hi Jason,

    Hi,

    I need help with xcopy. I have a script that copies user “My Documents” folder to a mapped drive. When I run the batch file while logged in, it backs up to the specified destination. But when I put the batch file with the logon script and when a user logs on, it copies the admin “My Document” folder not the user ” My Document” folder who is currently logged in.

    We have a VB script that runs when user logs in and that VB script then calls the batch file at last. It is a function in the VB script that calls the batch file.

    Here is the batch file that I have modified

    @echo off
    ATTRIB +A “%userprofile%\My Documents\*.*” /S
    xcopy “%userprofile%\My Documents\*.*” “Z:\%username%” /C/E/H/R/K/D/M/Y/I
    exit

    As I said the batch file works fine when logged on and executed. But it only copies the admin folder “My Document” when run in batch file. I have given full permission of the Z: drive to Everyone. Please help what I am doing wrong.

    • Jason Faulkner says:

      The variable %UserProfile% will resolve to the profile of the Windows user who launched the process.
      If you have a task set to run as the Administrator, the batch file will be launched as Administrator, hence the variable %UserProfile% will resolve to the Administrator’s profile.

  14. One problem when i run this bat.
    The files insides the folder is not copied to dirTempBackup and it prompts Invalid switch – /g

    Any idea ? Thanks.

    Starting to copy files.
    Copying: C:\AnalyseAgentData\Input\geam\_Default_LOT
    Invalid switch – /g
    Done copying files.

    Compressing backed up files. (New window)
    Done compressing backed up files.

    Cleaning up.

    Finished.

    • Jason Faulkner says:

      You can remove the “/g” switch from the XCOPY command. All it does is allow copying of encrypted files. Depending on your version of Windows, it may not be a valid option anyway.

  15. lizaoreo says:

    I’m trying to get this to work with IZARc’s command line interface rather than 7-Zip but keep running into trouble. Everything seems to work except the compression line which I’ve modified to read:

    START “Compressing Backup. DO NOT CLOSE!” /belownormal /wait “%exeIZArc%” -a -cx -r %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_Backup.zip “%BackupFileDestination%” $”%dirTempBackup%\”

    I think that’s how it should be, but it keeps coming back and saying “Cannot find -a”

    Any ideas?

    • Jason Faulkner says:

      One commenter above was getting that error and he fixed it by removing the ‘!’ after DO NOT CLOSE.

      • lizaoreo says:

        Thanks, I saw that post, but it just didn’t click. That fixed my issue and if anyone’s interested in using this with IZArc, here’s the line for use. You’ll have to update the other references to 7-zip of course.

        REM Compress files using IZArc in a lower priority process.
        START “Compressing Backup. DO NOT CLOSE” /belownormal /wait “%exeIZArc%” -a -r -p -cx “%BackupFileDestination%” “%dirTempBackup%\”
        ECHO Done compressing backed up files.

        Thanks for your help and script Jason.

  16. What about exclusions? e.g. .DLL files.

    • Jason Faulkner says:

      Just add this switch to the end of the XCOPY line:

      /exclude:.dll+.ocx

      This would exclude all DLL and OCX files from being copied to the backup.

  17. Is it possible for someone to help me on this;

    At work we are running a server on Windows 2003 Server R2. We run (everyday apart the weekends) a scheduled task to back up some important files, which are placed in a folder corresponding to what day they were run (e.g. todays backup will go in friday). The folders are very, very big and uncompressed (about 1.5gb). If I zip the folder manually, the folder will come down drastically (to about 30mb).

    My question is; is it possible to produce a batch script which will work out what day it is today and go into today’s folder, compress all of the backup files, make the output into 1 compressed zip file which will name it todays date and delete the old files?

    Sorry I know its a bit of a long one, but if anyone can help will be appreciated big time! Thanks!

    • Jason Faulkner says:

      Absolutely. There is a variable set in the script already called ‘DayOfWeek’. Just redirect your zipped file to to include the ‘DayOfWeek’ variable.
      For example, change the ‘BackupFileDestination’ variable line to something like this:

      SET BackupFileDestination=%BackupStorage%\%DayOfWeek%\Backup_%FileDate%_%txtBackup%.zip

  18. im getting an error “No configuration file found, missing: BackupConfig.txt”.. how to resolve it

    • Jason Faulkner says:

      There has to be a file named ‘BackupConfig.txt’ in the same directory as the batch script. The article has instructions on how to use this file.

  19. I also had the *Cannot find a* error. And as you mentioned, removing the exclamation mark from the phrase *DO NOT CLOSE* solved the issue. Nice script! Works well and unobtrusively. Thanks for sharing!!

    • Jason Faulkner says:

      Since a lot of people seem to be having this issue, I’ve changed the script to remove the ‘!’.
      Thanks for the feedback.

  20. The script is great! but…

    When I want to backup everything there isn’t enough free space in the temp folder…

    Could/would you be so kind to give me the script but without the compression and therefor without first running a copy to a local temp folder?

    thanks in advance.

    • Jason Faulkner says:

      Just modify the script like so and it will take care of the problem:

      1.
      Change the line reading:
      SET dirTempBackup=%TEMP%\backup
      to point to the destination directory where you want the output to end up.

      2.
      Put REM in front of the following lines (just like you see throughout the script):
      START “Compressing Backup. DO NOT CLOSE” [...]
      and
      IF EXIST “%dirTempBackup%” RMDIR /s /q “%dirTempBackup%”

  21. Ok. totally unrelated. sortof.

    what im attempting to do is get the date of a folder that was modified on the current day, preferably the last one modified perhaps based on time. i need that foldername to pipe to a variable.

    im trying to get the last modified user profile to find out who logged into a machine last. the registry wont work cause our usernames are aliases for the true username.

    ive spent about 6 hours working on different techniques to get this to work. only thing ive gotten close to the current logged on user using wmic. obviously that wont work.

    any ideas?

  22. This is great stuff, nice one Jason.

    However I have had massive trouble trying to find out how to back up My Documents, Favorites and Desktop folders for all the local profiles.

    Its was easy to simply copy them full profiles togther however that also meant Local Settings etc were copied, and I used %username% which only copied the currently logged on user.

    Annoyingly I thought I had it cracked at one point, but can’t remember the command I used… anyway it copied the profiles over minus the rubbish but for some reason my profile only had two system files backed up.

    Any idea’s?

    • Jason Faulkner says:

      Use the source path C:\Docs and Settings
      And then use XCOPY’s /exclude switch (read the documentation on XCOPY /?). Your exclude file might have:
      \Application Data\
      \Local Settings\
      \Temp\
      (etc.)

  23. Hello, when using this script I get an error when trying to copy files ? they are never found?

    I’m using danish XP Pro, and I had to change the ” delims=/ ” to ” delims=- ”

    Don’t know if this has anything to do with my error :(

    nicely script

  24. Thank you very much!
    Nice script.

  25. Okay, can somebody help with this script ?

    when adding single files to the config file nothing happens, i ALLWAYS get an error, using dirs is no problem…

  26. well, my date /t result is 09-10-2008

    So what would I do about the ” weekdays ” ? :)

    :(

  27. This is getting better and better.
    I want to add one more feature.
    Does anyone know where i can find info on sniffing whether a back-up location (usb hdd) is available?

  28. Jason Faulkner says:

    Use:

    IF EXIST “H:” (enter what to do)
    IF EXIST “I:” (enter what to do)

  29. I would like to modify this script to MOVE the files rather copy. i want to use it as an archive script. Will this work?

    • Jason Faulkner says:

      It *should*, but I haven’t tested it.
      Off the top of my head, I’m not sure how MOVE works with directories.

  30. There is a problem with the COPY part – if the destination dir structure has not already been created, COPY will fail.

    So: how do we extract the destination dir from !Destination! to test if it exists?

  31. Hi I’m getting the following error when doing an incremental
    Invalid parameter – /d:12-04-

    I assume it is coming from the line
    SET swXCopy=/s /d:%FileDate%
    I’m running windows 2003

    • Ok I realised that the “date /t” command in win xp and w2k3 (not sure of others) only gives yyyy/mm/dd and NOT the day of the week. So I modified the script with a clever bit of code I found on the web that uses abit of vb to get the day of week.

      Here it is. (Replace Lines 51 to 56)
      I hope the formatting is preserved when I post if not you’r gonna have to work it out

      REM Added to get the Day of Week
      SET “TM_=%TEMP%\_TMP$.VBS”
      >%TM_% ECHO/WSCRIPT.ECHO WEEKDAYNAME(WEEKDAY(DATE),TRUE)
      FOR /F %%? IN (‘CSCRIPT //NOLOGO %TM_%’) DO (SET “DayOfWeek=%%?” &&(DEL %TM_%))
      REM Untidy but remeber that the day of week variable has just been defined above as %DayOfWeek%
      REM Backup variables.
      FOR /f “tokens=1,2,3 delims=/ ” %%a IN (‘date /t’) DO (
      rem SET DayOfWeek=%%a
      SET NowDate=%%c-%%b-%%a
      SET FileDate=%%b-%%c-%%a
      )

      THEN after line 108 I added this to remove files older than 7 days

      forfiles /p %BackupStorage% /s /m *.* /d -7 /c “cmd /c del @path”

      HEY no dodgy comments, it works ok!! There is probably a better way but ….
      Man this would have been SOOO MUCH EASIER if this was a linux box. AAARRGGH Windoz sucks…

  32. I like this script. I’m using to backup all my games save files since it’s always a pain to hunt through the filesystem trying to find them.

    I would like to know how I would ‘restore’ the backups using this. I’m not very good with DOS scripting (more of a Linux guy). Maybe add a /restore flag to the script to restore everything? But I have no idea how to do that…

    Someone help?

  33. Jason Faulkner says:

    All the backed up files are compressed to a Zip file and stored in the location you specified (the “BackupStorage” value).
    Just open the Zip file and copy the files you want to restore to the original location.

  34. Is it possible to find out for the batch script if it needs a full backup, the first one must be a full backup and the rest a incremental? And it ain’t working here at all, I made 1 full backup and then incremental, if I open the zip file it hasn’t made a backup (incremental). It’s empty just some folders and he makes a folder BackUpTemp in my zip file.

    I just did copy/paste and edited the .txt file and .bat for destination.

    Maybe can you help me remote? I have alot of wishes maybe it’s easier to have an expert for 5minutes.

    Best regards,

    Marco

  35. I tend to clean up before i backup. This way unimportant files are not copied, which saves some time and bytes.

    I use two methods:

    * ccleaner (freeware): there is some need to configure all options well, otherwise useful cookies and program settings will be deleted over and over.
    Command: “%programfiles%\ccleaner\ccleaner.exe” /auto

    * Windows disk cleaner: you can set the desired options via /sageset:x and run disk cleaner with the saved settings using /sagerun:x (x is a number, i use 100)
    Command: cleanmgr /sagerun:x

  36. Looks like a nice little utility!

    Every time I run it I get an empty zip file and I do have a path set in config file.

    My path reads
    E:\download

    There are multiple files and sub directories in that folder

    Thanks in advance
    Randy

    • Jason Faulkner says:

      If you are getting an empty zip file, it sounds like the script isn’t picking up any files to backup. How long does it take the script to run? If under 5 seconds, most likely you do not have your backup locations configured correctly.

  37. Hi Jason, thanks for the script and the tip for KLS which I use as a secondary backup once a week for Outlook.

    One small irritation, when the script runs I get prompted to confirm whether the PST file destination is a File or a Folder ? Does the script not handle that ?

    Res
    Tony

    • Jason Faulkner says:

      Make sure Outlook isn’t open when you run the script. I believe the file is locked when Outlook is open.

      • Hi Jason, no Outlook isn’t open, to get the script to run unattended I added the following line before the COPY statement (in the FILE branch of the IF statement):

        SET Destination=%dirTempBackup%\!Current:~0,1!%%~pA

        this means that destination will always resolve to a drive\path\ rather than a drive\path\filename\extension, which I believe is what you intended. In fact the DIRECTORY branch may work with this syntax also as I am not sure why you use %%~pnxA here.

        I also used XCOPY as COPY did not work with the modified line above.

        Tony

        PS The only remaining issue I have is that certain files are not copied …I assume because of long file name issue. Does XXCOPY get round this or just flag the files that are not copied as another poster has suggested ?

        • Jason Faulkner says:

          You shouldn’t need to modify the script to copy a single file, rather just add the full path of the single file as an entry in the config file. I tested this thoroughly before posting the script. Also see my comment above about using COPY vs. XCOPY for single files.

  38. Thanks for the script.
    I do have a couple of questions
    How would I set the options like I do in the 7-zip gui to use the 7z format at a compression level of ultra, a dictionary size of 64 MB, a word size of 273, and a solid block size of 64 GB with 2 CPU threads. In testing what I am backing up this gives the smallest size.
    I would also like to delete the archive after it is 180 days old. This gives me a automated daily backup for six months of my important and daily changing data.
    Thanks again,
    Mike

    • Jason Faulkner says:

      You can modify the compression format and level by changing the text on the line which does the compression from:
      [...] -tzip -r -mx5 [...]
      to
      [...] -t7z -r -mx9 [...]

      You also need to change the backup file name line to reflect you are using 7z format:
      SET BackupFileDestination=[...].zip
      to
      SET BackupFileDestination=[...].7z

      As for keeping the number of files, there are many things you can do such as “folder rolling” or modify the script to include tools which delete items based on file dates. These are a bit beyond the scope of what this particular script does… maybe if I do a follow-up I will include something like this.

  39. Jason, I hear what you are saying, but my config file does contain the full path i.e. drive\folder\filename\extension of any files I am copying wrapped in quotes and I still get the prompt for file or folder.

    As an example, pasted from the current file:

    “C:\Documents and Settings\TonyC\Local Settings\Application Data\Microsoft\Outlook\Outlook.pst”

    Tony

    • Jason Faulkner says:

      Remove the quotes from the entry in your config file and also use the unmodified script above. I just tested using a single file and it works as expected.

  40. Jason it still does not work for me, is there an environment variable that needs adjutsing possibly ?

    Tony

    Btw Surely the quotes are a good idea where there are spaces in the path name ?

  41. Jason logically resolving DESTINATION to something that looks like a file and doesnt already exist as a folder is always going to get the DOS prompt for ‘File or Folder’.

    Also using this statement:

    IF “%%~xA”==”” (

    to determine whether you are copying a file or folder is prone to error as in XP a folder can legitimately be called:

    C:\temp\ThisIsAFolder.foo

    Tony

  42. Hi Jason,

    Great script, thanks for sharing.
    I would like to know if it’s at all possible to make the Destination file not have the same folder structure as the Current. i.e If I’m coping files from a network share for example, the file that gets created includes the whole path of the network share which I’d ideally like to drop.

    What is this line, and particularly the switches doing exactly: SET Destination=%dirTempBackup%\!Current:~0,1!%%~pnxA

    Is that changeable at all to achieve what I’m after?

    Cheers,
    Chris

  43. Chris, that line stores your files in a temp directory under your local profile….from there the files are compressed and set to the final destination folder (defined by %BackupStorage%)

    To send the compressed files and folders to the backup directory excluding the path use:

    SET Destination=%dirTempBackup%\%%~nxA

    This will effectively flatten out your directory structure so every file or folder shows under the backup directory. (I think there is a switch that will accomplish the same thing – if I find it, I’ll post it.)

    %%~A is the variable that stores the entry in Backupconfig.txt. It is then That varaible is then used to carry out the copy on. You can modify this variable to send to a different drive/path/location as follows:

    %%~pA – expands %%A to a path only
    %%~nA – expands %%A to a file name only
    %%~xA – expands %%A to a file extension only

    You can combine these to obtain combinations that suit your need. For a fuller explanation go to a Command window and type HELP FOR…Some way down you will find a list of modifiers.

    You may also find these sites useful.

    http://www.robvanderwoude.com/index.html
    http://www.ss64.com/index.html

    Hope that helps in Jason’s absence.

    Tony

  44. Btw:

    Current:~0,1 takes the 1st character of the string representing your file or folder name, e.g.

    C:\SomeFile.txt

    starting at position 0. So in this case it gives:

    C

    So in Jason’s script the drive is converted to a directory in the backup location e.g.

    E:Backups\C\SomeFile.txt

    Tony

  45. Anuroop says:

    Hi,

    I wanted to use this script to copy a file but whenever i run the script, it is not copying the file.

    But when i try to put in the folder name in the config file it works fine, but not for the file.

    Can you please let me know is there any thing to be done.

  46. two questions:
    1) what if i only want the full backup to occur once and month and incremental backups every other time?

    2) is it possible to make it so that, instead of the incremental creating a separate file from the full backup, it adds the new files to the full backup file? that way i only have one file per backup period.

    thanks

    • Jason Faulkner says:

      Kevin,

      You could accomplish #1 by adding this below the line which reads ‘SET DayOfWeek=%%a’:
      SET DayOfMonth=%%c
      and then replace the line reading ‘IF /i {%FullBackupDay%}=={%DayOfWeek%}’ with this:
      IF {%DayOfMonth%}=={01}

      If you are looking to do #2, one of my other tips might be more applicable to you:
      http://www.pcmech.com/article/easily-maintain-a-mirrored-directory-structure/
      The script could be modified to handle this situation, but it is would require more writeup than a reply here.

  47. This is exactly what I was looking for ;-) Thank you

    Quick question: Is there anyway to make a log of sharing violations (or other errors)?

    • Jason Faulkner says:

      Misty,

      You can try redirecting the copy lines to a file by adding:
      > C:\LogFile.txt
      to the end of each respective command.
      This is a quick way to accomplish this.

      • I tried that, but for some reason the output of these “shared violations” of xcopy is still displayed on the screen and not in the log file. But thank you for the quick replay!

  48. Hi,

    I have problems with accessing files which consist of two words, e.g. Peter Smith.mdb. The same way I have problems with two words directories. Any solution? (XP)

    Thanks!

    Copy c:\Peter Smith.mdb e:\New Backup\Peter Smith.mdb

  49. djemmers says:

    hi , after a quick test this looks like it is working great,

    did you ever made the follow up article like you said?

    one thing isn’t clear to me:
    “Incremental backup: When a folder is provided, only files created or modified on the current date are”
    So let’s say this happends
    day 1: full backup
    day 2: no changes , incremental backup
    day 3: changes, incremental backup
    day 4: changes, but no backup is made (for wathever reason)
    day 5: changes, incremental backup

    if I understand it,
    the changes from day 4 are not backed up? because incremental is only created or modifiede ON THE CURRENT DATE ?

    • Jason Faulkner says:

      Correct. I am using the XCOPY tool which only supports copying files for the current day.
      I may get around to revising this, but I don’t have any plans too at the moment.

  50. I hope you backup script solves the Vista & Windows 7 file attribute hassle that seems related to DRM. I can copy anything to any other folder or partition on my laptop but have problems dragging & dropping to the NAS on my wifi network.

    • I wrote my own batch file that initializes a VPN connection offsite, then backs up company data offsite. Then disconnects. I think I will modify my script to compress files as you did, however, I would prefer to compress my folders FIRST, then transmit the data of course. Thanks for sharing the info on 7-zip. I’ll be looking at it, as well as any alternatives.

  51. Hi,

    I’m implement daily backup which start at 2am daily.

    How do store the filename using dd/mm/yy format

    example
    Backup_20-07-09_Full.zip

    instead the follwing format which i dun want.

    Backup_07-2009-_Full.zip
    Any idea?
    Thanks

  52. Hi Jason Faulkner,

    The filename became Backup_2009-07-_Full.zip,instead my expectation which is Backup_2009-07-dd_Full.zip
    still missing the day format, any idea?Do i miss out any thing?

  53. How do i get the script to only back up the previous 7 days files. I want it to look at the date modify date

  54. I tried your script and it works great for directories however once I try to copy a file or add a file in the list, it shows the message as follows:

    The system cannot find the path specified.
    0 file(s) copied.

    I tried to debug it and could see that the destination path is showing as follows:

    C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\backup\C\USB\backup\Test\PHPTest

    The same path is for directory copying however it works perfectly but not while copying the single file.

    The code which needs review is as follows:


    FOR /f “skip=1 tokens=*” %%A IN (%filBackupConfig%) DO (
    SET Current=%%~A
    IF NOT EXIST “!Current!” (
    ECHO ERROR! Not found: !Current!
    ) ELSE (
    ECHO Copying: !Current!
    SET Destination=%dirTempBackup%\!Current:~0,1!%%~pnxA
    REM Determine if the entry is a file or directory.
    IF “%%~xA”==”" (
    REM Directory.
    XCOPY “!Current!” “!Destination!” /v /c /i /g /h /q /r /y %swXCopy%
    echo !Current!
    echo seperator
    echo !Destination!
    echo dirend
    ) ELSE (
    REM File.

    COPY /v /y “!Current!” “!Destination!”
    echo !Current!
    echo seperator
    echo !Destination!
    )
    )
    )

    Any idea?

    • Thanks for this script Jason.
      It seems COPY does not create the destination path for the file. Tried using XCOPY but it prompts since it doesn’t know if it needs to create a file or folder. I pipe the ‘f’ in since we know it is a file. It seems to work but there might be a better way?
      COPY /v /y “!Current!” “!Destination!” —> echo ‘f’ | XCOPY /v /y “!Current!” “!Destination!”

      • I’m on a Dutch version of Windows 7 and although i don’t know a thing about dos commands, I notice that the input flag is language specific.
        The best way to figure out the required input in your language is to run the adjusted batch script from an (elevated) command prompt and see what pops up.

        The original line in the script is:
        COPY /v /y "!Current!" "!Destination!"

        Paul suggests:
        echo 'f' | XCOPY /v /y "!Current!" "!Destination!"

        In my case (in Dutch) this becomes:
        ECHO B | XCOPY /v /y "!Current!" "!Destination!"

        Note that i’ve left out the single quotes around the input “B”. The script will run with the quotes (sort of) but they’re superfluous.

        (Please ignore any code-tags with the examples)

  55. im trying to use that script but only does an empty zip file…

  56. Hey this script works amazing! I was wondering tho, it does save it as a *.zip file, I was wondering if we can change the compression to “best” and have it create a *.7z file instead, is this possible to tune it up like that?

    My output is 1.6gb and I wanted to compress it to make it smaller, and create it in a *.7z. Is that possible? Thanks so much!

  57. I love this script, any plans to make modifications to allow FTP upload for offsite backups?

    • Jason Faulker says:

      I’m doing this in a couple of scripts I have on our servers, it is not very difficult.

      Basically you use the FTP command in your script (this is a tool built into Windows) and then pass it a file which has the FTP server name, password and then the subsequent transfer commands each on a single line.

      There are plenty of pages out there which explain how to use this, so google help regarding the Windows FTP command and the command line and you can see some examples.

  58. Thank you for this script. It has enhanced my knowledge of batch scripting. I have an issue:

    For some strange reason, there must be an existing environmental variable because my backups always include Picasa3 folders; namely: “\CDPrep” and “\Picasa2AlbumsTemp”. The strangeness of this is that the BackupConfig.txt only contains ONE directory to backup. Any thoughts?

  59. I enjoyed reading your discussion on “Why Command Line Scripts?”. How very appropriate. Have you considered using biterscripting ( http://www.biterscripting.com ) on windows instead of batch ? It has much better parsing, comparing, catalogging and monitoring capabilities.

  60. I can’t seem to get the script working for me. I’m using XP and in the config file I used the following:
    # backup files
    “\\Fs-data\data2\OVI\Safety Management\BRU referentielijst.xls”
    “\\Fs-data\data2\OVI\Safety Management\[INC] Incidenten\[DOS] Dossiers\INC-DOS-0041 hazard log.xls”
    It doesn’t matter if I use quotation marks, the files just won’t backup. I can get the file to work when putting in:
    \\Fs-data\data2\OVI\Safety Management, however that backups the entire subfolder structure as well, while I only want to backup these 2 files.
    Can somebody help me?

    • Jason Faulkner says:

      Try replacing this line:
      COPY /v /y “!Current!” “!Destination!”

      with this:
      XCOPY “!Current!” “!Destination!” /v

  61. Hi Jason, thanks for the tip, but I still get an empty zip-folder. Any other ideas?

  62. Please check your BackupConfig.txt file
    the first line is always ignored .

  63. still, I can’t imagine something wrong with the following:

    # backup files
    \\Fs-data\data2\OVI\Safety Management\BRU referentielijst.xls
    \\Fs-data\data2\OVI\Safety Management\[INC] Incidenten\[DOS] Dossiers\INC-DOS-0041 hazard log.xls

  64. Thanks for posting this script for us to use! I have been using it since the summer of 2009 and it’s worked great. As a tip for anyone who cares I cloned the script and use multiple instances of it for several different directories that I want to backup. That way, I can schedule them up to run whenever I feel like it. I feel so much better now that I’ve been able to back up my music.

  65. Can you do a follow up but instead of using 7zip use mkisofs to build spanning iso files cd. using cds to store backups is the way to go.

  66. my ‘date’ command returns only date. there is no day of week
    I have Windows Server 2003
    also tried on Windows 7. same result.

    • Jason Faulkner says:

      Change this:
      REM Backup variables.
      FOR /f “tokens=1,2,3,4 delims=/ ” %%a IN (‘date /t’) DO (
      SET DayOfWeek=%%a
      SET NowDate=%%d-%%b-%%c
      SET FileDate=%%b-%%c-%%d
      )

      To this:
      REM Backup variables.
      FOR /f “tokens=1,2,3 delims=/ ” %%a IN (‘date /t’) DO (
      SET DayOfWeek=%FullBackupDay%
      SET NowDate=%%c-%%a-%%b
      SET FileDate=%%a-%%b-%%c
      )

      This will cause the script to run a full backup every time, but since you do not have the day of week, there is no way to avoid it.

  67. The script creates an emtpy backup file for me as well with win 2003 server. I found out that it ignores the first line in the config file.

    Config file:
    c:\test1 <– no backup
    c:\test2 <– backup
    c:\test3 <– backup

    The first line may be empty.
    Hope this helps and thanks for your script.

  68. I have tried to run the script on a 2000 box — verified that the config file is there and after the script runs it terminates immediately indicating that the config file is missing.

    Any ideas?

    Thanks,

    • Jason Faulkner says:

      Try changing the line which reads:

      IF NOT EXIST %filBackupConfig% (

      to

      IF NOT EXIST “%filBackupConfig%” (

  69. Hi To All,

    Im a newbie in netbackup system. Would like to ask some assistance about scripting. We have an existing Replication Manager (RM) that is doing the cloning of disk. and when the cloning is finish we then perform a backup. But we usualy encounter an error. It s because that the cloning is not yet finish and the backup schedule starts already. My boss ask me to create a script that will call to backup the disk after it finish the cloning. I see that we can put the script in the RM system to call it after if finish the cloning. My problem is that scheduling on backup.

    we have Daily backup, Weekly backup and every 28 of the month fullbackup.

    for daily = 1 week retention
    for weekly = 2 weeks retention
    for 28th of the month = 1 month retention.

    How to create a script that will check if is Monday, Tuesday, Wednesday, Thursday, Friday, Saturday = Daily; Sunday will be Weekly and every 28th of the month is Monthly backup.

    Please help me if you have a script that I can use for this setup.

    Thank you very much,

    Regards,
    Mike
    email :netbackup_newbie@yahoo.com.sg

  70. Loving the script. Got around a problem with incremental backups by changing the below:

    SET FileDate=%%b-%%c-%%d
    to
    SET FileDate=%%b-%%c-2010

    For some reason it does not want to include the year but the above works.

    I however cannot use variables like %username% or %appdata% in the “BackupConfig.txt” file.

    Any idea why not?

    • Jason Faulkner says:

      The BackupConfig.txt file reads the data in as literal text. It will not process environment variables.

  71. Hassan Bintook says:

    I want to do monthly backup and delete the files

    Also the output file should be like xxxxxx_Mar2010.rar

    Thanx

  72. I use this scripts, i think is pretty good. It get the job done.
    Now, is there away to Exclude a files or folders?

    I have few Vmware folder(BIG size) and few temp folder, which i dont want to back it up.

    TY
    k

  73. ismashkhy says:

    Hi, I’m a newbie in scripting, can this be possible?

    source folder
    \Folder1\sample.txt

    destination backup folder
    \Folder1\sample.txt

    each time the backup is initiated, I only need the added or modified files are the only one that is copied onto the backup destination…

    is this possible?, I don’t need any date or what so ever at this time of trying scripting for the first time?…

    can anyone give me template script of what I want to do?..

    the file to be copied is as it is…copied as original not compressed in zip or rar…

    thanks in advance

  74. Hi, Im wondering, how can you make this script to backup only certain type files, for example only files with .doc extensions etc, is there any way? BTW that script is nice!!

  75. Do you plan on fixing this script for windows 7 or porting to powershell? Has anyone found an alternative that works on Windows 7? I’m on a x64 version.

    I had fun using this script on Vista. I cannot get Jason Faulkner’s backup script to run on Windows 7 unless I run manually from an elevated prompt in the right directory. When setting the appropriate rights on the bat-file itself and just executing it, the script dies within the first few seconds. Trying every available parameter and option that i could think of, It still dies as a scheduled task in the new Windows 7 task scheduler. That sort of sucks, because I know of no other backup utility that allows me to set different times for full and incremental backups (incremental backups btw also not working on windows 7).

  76. I found that script very instresting as i have not done any modification in bat file but i do it in txt file but my all data gets backup in “temp” folder

    Please advice

  77. can you help me by writing a rpogram which do the following:propose an automatic, network-based backup system for our irreplaceable data. The backup should be done on regular basis intervals every Friday at 8 of every week.
    Your proposal should be easy to implement, inexpensive, powerful, reliable and documented. Assuming you have a networked windows and Linux machine ready, you should be able to use your project to set up your own automatic backup system in a short time.

  78. Can you except some files / directorys?
    d:\www
    but except d:\www\cache

    Thanks.

Leave a Reply to Meego92 Cancel reply

PCMech Insider Cover Images - Subscribe To Get Your Copies!
Learn More
Tech Information you can use, sent to your inbox each and every week. Check out PCMech's digital e-zine...