Quantcast
Channel: Microsoft Community - Using ROBOCOPY In A Batch Script To Backup Files & Folders On Windows Vista/7
Viewing all articles
Browse latest Browse all 78

Using ROBOCOPY In A Batch Script To Backup Files & Folders On Windows Vista/7

$
0
0

This isn't so much a question as it is a contribution for those, like myself, who are looking for thorough examples of how to use ROBOCOPY in a batch script (.BAT file) to backup their Windows Vista/7 data.  I am, by no means, a master of such things, but I've been using the script below for many months, and I believe it's a decent example of how to implement ROBOCOPY as a means to backup data.  Being that I was never able to find a decent example myself, I hope that my contribution will help others.

You must accept all responsibility for use of this script.  I will not accept any responsibility for data that is lost as a result of its use.  I am not claiming excellence in this matter.  There are those that are much better at this than I am.  I'm sure some of you might find gross inefficiencies or other such lameness in my script, but if you do, and feel like commenting about it, please be kind.

Copy the script below, and paste it into Notepad, or similar text editor.  Save the file, and make sure the file name extension is '.BAT'.  Be sure to read the comments in the script, and define the variables to suit your needs.

The backup script below will perform a complete backup on the first day of the month of the users profile that is currently executing the batch, and it will perform incremental backups of the users profile every day subsequent to the day the complete backup was performed.  This process repeats itself on a monthly basis.  I schedule this backup to run every day of the month, and I keep 32 days of backup history.  This means, I always have at least one complete backup, and up to 31 days of incremetal backup history to refer to.

If anyone has suggestions as to how to improve this script, please feel free to contribute in this thread.

Thank you.

-----------------------------------------------------------------------------------------------------

@echo off
title Backing Up Files...
color 87

rem ******************************************************************************************
rem * Use variable BV to define location of backup storage volume.                           *
rem * Do not include trailing '\' character. Do not enclose paths with spaces in quotes.     *
rem ******************************************************************************************

set BV=Z:\Backups

rem ******************************************************************************************
rem * Use variable EXC to define the name of directories to be EXCluded from the backup.     *
rem *                                                                                        *
rem ******************************************************************************************

set EXC="Temp" "TMP" "Temporary Internet Files" "Cookies" "Recent"

rem ******************************************************************************************
rem * Set the backup file TTL (Time To Live) in days.  The backup script will not maintain   *
rem * backup history beyond the specified number of days.  This is a crude way of keeping    *
rem * the backup volume from being overfilled.  Adjust this value to provide maxiumum backup *
rem * history for the amount of data that is being backed up, given the amount of disk space
rem * available to store it.                                         *
rem ******************************************************************************************

set BTTL=32

rem ******************************************************************************************
rem * Check to see if the defined backup storage volume is accessible. Some external storage *
rem * devices will enter a power saving mode (sleep) that may cause this process to fail,    *
rem * so we'll check to see if the OS reports the volume as available.                         *
rem ******************************************************************************************

echo.
echo  Initializing, please wait...
echo.

dir %BV%

if not exist "%BV%" (
    title Backup Process Failed
    color C0
    echo.
    echo.
    echo.
    echo.
    echo   The backup volume, %BV%, appears to be inaccessible.  You may not perform
    echo   a backup at this time.
    echo.
    echo.
    echo.
    echo.
    pause
    exit
)

rem ******************************************************************************************
rem *  Determine the month week, time, and day numbers for the construction of the backup    *
rem *  file names.                                                                           *
rem ******************************************************************************************

FOR /F "TOKENS=1,2* delims=/ " %%A IN ('DATE/T') DO SET MON=%%B
FOR /F "TOKENS=1,2,3* delims=/ " %%A IN ('DATE/T') DO SET DAY=%%C
for /f "tokens=1,2,3 delims=: " %%A in ('TIME /T') do set TM=%%A%%B%%C

IF %MON% == 01 (SET MONTH=January
 goto :begin)
IF %MON% == 02 (SET MONTH=February
 goto :begin)
IF %MON% == 03 (SET MONTH=March
 goto :begin)
IF %MON% == 04 (SET MONTH=April
 goto :begin)
IF %MON% == 05 (SET MONTH=May
 goto :begin)
IF %MON% == 06 (SET MONTH=June
 goto :begin)
IF %MON% == 07 (SET MONTH=July
 goto :begin)
IF %MON% == 08 (SET MONTH=August
 goto :begin)
IF %MON% == 09 (SET MONTH=September
 goto :begin)
IF %MON% == 10 (SET MONTH=October
 goto :begin)
IF %MON% == 11 (SET MONTH=November
 goto :begin)
IF %MON% == 12 (SET MONTH=December
 goto :begin)

:begin

IF %DAY% LEQ 31 SET WEEK=Four
IF %DAY% LEQ 21 SET WEEK=Three
IF %DAY% LEQ 14 SET WEEK=Two
IF %DAY% LEQ 07 SET WEEK=One

echo.
echo.
echo.
echo   Beginning backup process, please wait...
echo.
echo.
echo.

rem ******************************************************************************************
rem * Before we copy a bunch of backup data to the backup volume, let's get rid of the old   *
rem * stuff.  All files older than the amount of days defined by BTTL are deleted.           *
rem ******************************************************************************************

if exist "%BV%" (
    forfiles /p %BV% /d -%BTTL% /c "CMD /Q /C @rmdir /S /Q @PATH"
    forfiles /p %BV% /d -%BTTL% /c "CMD /Q /C del /F /Q @PATH"
    cls
)

rem ******************************************************************************************
rem * If the current months _Complete folder exists, a complete backup has already been      *
rem * performed, so perform an incremental instead.                                          *
rem ******************************************************************************************

if exist "%BV%\%MONTH%_Complete" (
    echo.
    echo.
    echo  Performing incremental backup...
    echo.
    echo.
    mkdir "%BV%\%MONTH%_Day_%DAY%_%TM%_Incremental"
    set BLOG=%BV%\%MONTH%_Day_%DAY%_%TM%_IncrementalBackupLog.txt
    robocopy "%USERPROFILE%" "%BV%\%MONTH%_Day_%DAY%_%TM%_Incremental\%USERNAME%" /B /E /M /R:0 /V /NP /TEE /XJ /LOG+:"%BV%\%MONTH%_Day_%DAY%_%TM%_IncrementalBackupLog.txt" /XD %EXC%
) else (
    echo.
    echo.
    echo  Performing complete backup...
    echo.
    echo.
    set BLOG=%BV%\%MONTH%_CompleteBackupLog.txt
    robocopy "%USERPROFILE%" "%BV%\%MONTH%_Complete\%USERNAME%" /B /E /R:0 /CREATE /NP /TEE /XJ /LOG+:"%BV%\%MONTH%_CompleteBackupLog.txt" /XD %EXC%
    robocopy "%USERPROFILE%" "%BV%\%MONTH%_Complete\%USERNAME%" /B /E /R:0 /V /NP /TEE /XJ /LOG+:"%BV%\%MONTH%_CompleteBackupLog.txt" /XD %EXC%
attrib -A "%USERPROFILE%\*.*" /S
    )
)

:end

exit


hi all, i don't have any experience on scripting, i tried this script, but a lot of error logged and its as following, can any body help me. 



-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows                              
-------------------------------------------------------------------------------

  Started : Tue Apr 02 16:23:34 2013

   Source : C:\Users\m.mustafa.KETAB\
     Dest : \\ketabtech\m.mustafa$\_Complete\m.mustafa\

    Files : *.*
   
 Exc Dirs : Temp
   TMP
   Temporary Internet Files
   Cookies
   Recent
   
  Options : *.* /TEE /S /E /COPY:DAT /CREATE /B /NP /XJ /R:0 /W:30 

------------------------------------------------------------------------------

ERROR : You do not have the Backup and Restore Files user rights.
*****  You need these to perform Backup copies (/B or /ZB).

ERROR : Robocopy ran out of memory, exiting.
ERROR : Invalid Parameter #%d : "%s"

ERROR : Invalid Job File, Line #%d :"%s"


  Started : %hs

   Source %c 

     Dest %c 
       Simple Usage :: ROBOCOPY source destination /MIR

             source :: Source Directory (drive:\path or \\server\share\path).
        destination :: Destination Dir  (drive:\path or \\server\share\path).
               /MIR :: Mirror a complete directory tree.

    For more usage information run ROBOCOPY /?

                                                          
****  /MIR can DELETE files as well as copy them !

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows                              
-------------------------------------------------------------------------------

  Started : Tue Apr 02 16:23:34 2013

   Source : C:\Users\-------@domain\
     Dest : \\--------\useraccount$\_Complete\m.mustafa\

    Files : *.*
   
 Exc Dirs : Temp
   TMP
   Temporary Internet Files
   Cookies
   Recent
   
  Options : *.* /V /TEE /S /E /COPY:DAT /B /NP /XJ /R:0 /W:30 

------------------------------------------------------------------------------

ERROR : You do not have the Backup and Restore Files user rights.
*****  You need these to perform Backup copies (/B or /ZB).

ERROR : Robocopy ran out of memory, exiting.
ERROR : Invalid Parameter #%d : "%s"

ERROR : Invalid Job File, Line #%d :"%s"


  Started : %hs

   Source %c 

     Dest %c 
       Simple Usage :: ROBOCOPY source destination /MIR

             source :: Source Directory (drive:\path or \\server\share\path).
        destination :: Destination Dir  (drive:\path or \\server\share\path).
               /MIR :: Mirror a complete directory tree.

    For more usage information run ROBOCOPY /?

                                                          
****  /MIR can DELETE files as well as copy them !
 

Viewing all articles
Browse latest Browse all 78

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>