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

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


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

$
0
0

Hello, I like this script you created and was wondering if the rmdir and del line was working for you?

I set the days to 3 and i don't see folders and files being deleted. If you have any ideas please let me know.

Also why are the following written twice:  1) robocopy "%USERPROFILE%" "%BV%\%MONTH%_Complete\%USERNAME%" /B /E /R:0 /CREATE /NP /TEE /XJ /LOG+:"%BV%\%MONTH%_CompleteBackupLog.txt" /XD %EXC%
   2) robocopy "%USERPROFILE%" "%BV%\%MONTH%_Complete\%USERNAME%" /B /E /R:0 /V /NP /TEE /XJ /LOG+:"%BV%\%MONTH%_CompleteBackupLog.txt" /XD %EXC%

and same here: 1) forfiles /p %BV% /d -%BTTL% /c "CMD /Q /C @rmdir /S /Q @PATH"
    2) forfiles /p %BV% /d -%BTTL% /c "CMD /Q /C del /F /Q @PATH"

Thanks

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

$
0
0

Hi thanx for a nice script and for sharing it!

This look like a perfect solution that i can use but!...

I tryed to change th set=bv to a different path

eg.

Path that i will take back up D:\DATA to D:\Backup

then i will stor it in a 7zip archive and copy to \\some server\Archives

but with no luck!

reg.

mike!

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

$
0
0
Very nice Script
Thanks for sharing

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

$
0
0

I apologize for not replying sooner.  I never received a notice from the Answers server that I had any posts on this thread.  I'm glad you like the script.  I've been using it for a long time now with great success.

 

The two robocopy commands are actually different.  Taken from the ROBOCOPY whitepaper published by Microsoft, the /CREATE switch tells robocopy to only copy the files and folders, but only creates the heirarchy and files with zero bytes of data.  This, apparently, helps reduce fragmentation of your backup volume file system.  Once that process is complete, the command is invoked again, this time without the /CREATE switch, and all of the data is then copied to the backup volume.

 

The two forfiles commands might be a mistake.  It's been so long I cannot remember.  I have a feeling that I had been testing the two commands, and instead of deleting one of them, I just uncommented it instead.  So, feel free to do some testing on those, and choose the one that works for you.  Something tells me I might of done that to sweep up some stubborn files that didn't seem to get deleted with the first command, or I might have found it necessary to use both in order for the script to operate on XP/Vista/7 equally well.  Just can't remember for sure.

 

Thank you!

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

$
0
0

You're welcome.  :-)

 

I'm sorry that you couldn't get that to work.  7-Zip is definitely a nice compression algorithm, and there are some nice command-line tools that you could easily integrate into a script like this.  In fact, my first attempt at this script used the PowerArchiver command line tools.  But, the thing I don't like about using archives (whether it's bzip, tar, zip, cat, etc.) is that when it's time to restore files, you have to use a utility to extract files from the archives.  In my experience, simply using the Windows native file system compression utility gives me compressed backup data, and it's completely seamless to the user.  When I prepared my external backup disk, I selected "Compress this drive to save space" before I copied any data to it, so the whole volume is compressed.  Granted, the compression isn't as good as 7-Zip, but it's pretty good as they go.  Plus, when I want to restore a file I just use the Windows 7 search utility to find all instances of the file or folder that I want to restore, and it displays them in the Explorer window for me to copy from.  I get seamless, direct access to any file and/or folder right in the Explorer window without having to use any other tool to sift through a multi-gigabyte archive file.

 

I recognize that some backup strategies warrant the use of compressed archives, though.  If that's the case for you, I wish you luck.

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

$
0
0

Thanks, I appreciate that.  Just FYI, I added one last line to the script.  I added the "attrib" command to execute after the complete backup process.  This ensures that the Archive bit gets reset on all of the files after the complete backup, so that the next incremental backup doesn't copy files that have already been backed up in the complete backup.  Without this command, the first incremental performed after a complete backup will contain some of the files that were copied in the complete backup, even though those files may not have changed.  The incremental process uses the /M switch, which sets the Archive bit of the source files to OFF after copying the files, but the /M switch also instructs robocopy to only copy files whos Archives bits are set to ON, so that switch cannot be used in the complete backup process.

 

The script still functions safely and reliably without the attrib command.  However, it doesn't operate as efficiently as it does with it.  E.g., not a big deal, just a deal.

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

$
0
0
What part is making it hide the backed up files? 

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

$
0
0
Nothing in the script will set the hidden attribute of the copied files.  If your copied files are hidden, I would examine the file system of the volume that you are using to store the copied files, and look for potential causes there.  Perhaps, the directory in which the copied files are stored has it's hidden attribute set, and the subfolders and files are inheriting the attribute.

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

$
0
0
Nothing in the script will set the hidden attribute of the copied files.  If your copied files are hidden, I would examine the file system of the volume that you are using to store the copied files, and look for potential causes there.  Perhaps, the directory in which the copied files are stored has it's hidden attribute set, and the subfolders and files are inheriting the attribute.


Yea its weird, its actually only making the top level date folder hidden and system. had to attrib it via cmd to get it done. Nothing is hidden on the source side except some lightroom cacheing stuff.

 

Scott

from WP

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

$
0
0

What would cause this batch file to leave out the current month and day and only create a folder like this "_Complete".  It does the same with the txt file "_CompleteBackupLog.txt".  I am running Windows 7 64 bit.

 

Thanks

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

$
0
0
Thanks, this script is quite useful, but I ran into a problem which might be related to the last "attrib" command.  During my first full backup, which lasted 24 hours or so, some new files were added to the source fs such that they were not part of the full backup set.  In the subsequent incremental backup, however, these new files also were NOT copied over.  Their archive switch was set to OFF.  Wouldn't this -A command reset archive for files added during the full backup and be missed by the incremental if they were not otherwise touched??!!

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

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

$
0
0

Like your script and put it to good use in a BE2010 environment where I'm trying to backup and EMC device without using an agent in a cifs drive.  Not familiar with the script for setting the filenames.  Noticed that this catalogs files by month and day but would like to do this by week - as I keep full weekly and monthly backups and daily incrementals.  Would you just add the %week% script to most of the lines directing location?  Also what would the filename line be - FOR /F "TOKENS=1,2* delims=/ " %%A IN ('DATE/T') DO SET MON=%%B??? what should this look like to indicate the week??

 

Thanks - Bob

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

$
0
0
Hello Boss Hauss,

you made a nice script, and i need to know if it works on Windows Xp ? i tray and give same error and dont set month and hour can you help me to convert the file run on XP 

Thanks,

mindsai.jd

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 !
 

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

$
0
0
Very useful!

This is a small modification that allows you to pass three arguments to the script:

  1. Source (Source of the bu)
  2. Name (The name of the backup)
  3. TTL
This way, you can run robocopy backups for ANY directories in your network (across the network). I run them using Task Scheduler for the entire network.



@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=E:\Backups

rem ******************************************************************************************
rem * Use variable BS to define location of source of backup (or pass as variable).          *
rem * Do not include trailing '\' character. Do not enclose paths with spaces in quotes.     *
rem ******************************************************************************************

set BS=%1

rem ******************************************************************************************
rem * Use variable BN to define name of backup (or pass as variable).                           *
rem ******************************************************************************************

set BN=%2

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=%3

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%\%BN%

if not exist "%BV%\%BN%" (
   mkdir "%BV%\%BN%"
)

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%\%BN%" (
    forfiles /p %BV%\%BN% /d -%BTTL% /c "CMD /Q /C @rmdir /S /Q @PATH"
    forfiles /p %BV%\%BN% /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%\%BN%\%MONTH%_Complete" (
    echo.
    echo.
    echo  Performing incremental backup...
    echo.
    echo.
    mkdir "%BV%\%BN%\%MONTH%_Day_%DAY%_%TM%_Incremental"
    set BLOG=%BV%\%BN%\%MONTH%_Day_%DAY%_%TM%_IncrementalBackupLog.txt
    robocopy "%BS%" "%BV%\%BN%\%MONTH%_Day_%DAY%_%TM%_Incremental\%BN%" /B /E /M /R:0 /V /NP /TEE /XJ /LOG+:"%BV%\%BN%\%MONTH%_Day_%DAY%_%TM%_IncrementalBackupLog.txt" /XD %EXC%
) else (
    echo.
    echo.
    echo  Performing complete backup...
    echo.
    echo.
    set BLOG=%BV%\%BN%\%MONTH%_CompleteBackupLog.txt
    robocopy "%BS%" "%BV%\%BN%\%MONTH%_Complete" /B /E /R:0 /CREATE /NP /TEE /XJ /LOG+:"%BV%\%BN%\%MONTH%_CompleteBackupLog.txt" /XD %EXC%
    robocopy "%BS%" "%BV%\%BN%\%MONTH%_Complete" /B /E /R:0 /V /NP /TEE /XJ /LOG+:"%BV%\%BN%\%MONTH%_CompleteBackupLog.txt" /XD %EXC%
attrib -A "%BS%\*.*" /S
    )
)

:end

exit

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

$
0
0

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. 

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

 

I know this very late and that you have probably figured out the problem but perhaps this will help others.

 

Try running the script as an administrator.  The same error is received using the following command line but works fine when the command prompt is run as administrator.

 

 

robocopy "c:\System Volume Information" c:\dummy /l /xj /e /nfl /ndl /njh /r:0 /b 

 

While this can be used for any folder it is especially useful for system folders when you want to determine the size.  here is an explanation of the arguments which shows that nothing is listed, copied, or deleted. only the space information is presented.

 

c:\dummy  copy files to c:\dummy
/l List only - don't copy, timestamp or delete any files
/xj exclude junction points
/e copy subdirectories, including Empty ones.
/nfl No File List - don't log file names.
/ndl No Directory List - don't log directory names.
/njh No Job Header
/r:0 number of Retries on failed copies: default 1 million.
/b copy files in Backup mode.   (Backup mode runs Robocopy as a "Backup Operator" allowing Robocopy to override permissions settings (specifically, NTFS ACLs).)

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

$
0
0
Great job. Thanks for sharing this. I have used your code to improve my own incremental backup script. Our two scripts now differ only in the following way:

1. In addition to folder exclusion, I also exclude certain files; e.g., I use the following:
set EXF=thumbs.db ethumbs.db desktop.ini *.tmp *.hdmp *.blf {* .DS_Store ._DS_Store

Then amend the robocopy command lines with:
/XF %EXF%

2. I include a call to a separate batch file that creates environmental variables for time and date parameters:

CALL DateTimeFunctions

This makes the routine international. (Your script as is will fail in Europe, for example.)
It also keeps my backup script concise, since I use this same date-time batch for other purposes. 

I've included my version of DateTimeFunctions.bat below, which includes references to the original author and some testing code at the end. It may be necessary to add SETLOCAL and ENDLOCAL to the top and bottom of the final script, and make minor adjustments to variable names.

Thanks again.




@echo off

:: http://www.computerhope.com/forum/index.php?topic=76081.msg497599#msg497599
:: http://www.computerhope.com/forum/index.php?topic=72945.0
:: Code published in the above two pages has been combined

set vbsfile=newdate.vbs
echo        Newdate = (Date())>%vbsfile%
echo       DateYear = DatePart("YYYY", Newdate)>>%vbsfile%
echo      DateMonth = DatePart("M"   , Newdate)>>%vbsfile%
echo        DateDay = DatePart("D"   , Newdate)>>%vbsfile%
echo     WeekOfYear = DatePart("WW"  , Newdate)>>%vbsfile%
echo      DayOfYear = DatePart("Y"   , Newdate)>>%vbsfile%
echo  WeekDayNumber = DatePart("W"   , Newdate)>>%vbsfile%
echo TodayNameShort = WeekdayName(WeekDayNumber,True)>>%vbsfile%
echo  TodayNameFull = WeekdayName(WeekDayNumber,False)>>%vbsfile%
echo MonthNameShort = MonthName(DateMonth,True)>>%vbsfile%
echo  MonthNameLong = MonthName(DateMonth,False)>>%vbsfile%
echo       TimeHour = Hour(Time)>>%vbsfile%
echo     TimeMinute = Minute(Time)>>%vbsfile%
echo     TimeSecond = Second(Time)>>%vbsfile%
echo If TimeHour^<12 Then>>%vbsfile%
echo   AMorPM="AM">>%vbsfile%
echo Else>>%vbsfile%
echo   AMorPM="PM">>%vbsfile%
echo End If>>%vbsfile%
echo Wscript.Echo DateYear^&" "^&DateMonth^&" "^&DateDay^&" "^&Week^
OfYear^&" "^&DayOfYear^&" "^&WeekDayNumber^&" "^&Today^
NameShort^&" "^&TodayNameFull^&" "^&MonthNameShort^&" "^&MonthNameLong^&" "^&TimeHour^&" "^&Time^
Minute^&" "^&TimeSecond^&" "^&AMorPM>>%vbsfile%


for /f "tokens=1-14 delims= " %%A in ('cscript //nologo %vbsfile%') do (
set Year=%%A
set Month=%%B
set Day=%%C
set WeekNumber=%%D
set DayNumber=%%E
set DayOfWeekNum=%%F
set DayNameShort=%%G
set DayNameLong=%%H
set MonthNameShort=%%I
set MonthNameLong=%%J
set TimeHour=%%K
set TimeMinute=%%L
set TimeSecond=%%M
set TimeAMPM=%%N
)

::----- ADJUST VALUES LESS THAN 10
IF %Day% LEQ 9 SET Day=0%Day%
IF %Month% LEQ 9 SET Month=0%Month%
IF %TimeHour% LEQ 9 SET TimeHour=0%TimeHour%
IF %TimeMinute% LEQ 9 SET TimeMinute=0%TimeMinute%
IF %TimeSecond% LEQ 9 SET TimeSecond=0%TimeSecond%

GOTO :EOF

echo.
echo Date Year                 : %Year%
echo Date Month                : %Month%
echo Date Day                  : %Day%
echo Week Of Year              : %WeekNumber%
echo Day Of Year               : %DayNumber%
echo Day Of Week [Number]      : %DayOfWeekNum%
echo Day Of Week [Short Name]  : %DayNameShort%
echo Day Of Week [Full Name]   : %DayNameLong%
echo Month [Short Name]        : %MonthNameShort%
echo Month [Long Name]         : %MonthNameLong%
echo Time Hour                 : %TimeHour%
echo Time Minute               : %TimeMinute%
echo Time Second               : %TimeSecond%

echo.
echo %DayNameLong%, %MonthNameLong% %Day%, %Year% is in week %WeekNumber%
echo.

pause


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

$
0
0

Hello, I like this script you created and was wondering if the rmdir and del line was working for you?

I set the days to 3 and i don't see folders and files being deleted. If you have any ideas please let me know.

Also why are the following written twice:  1) robocopy "%USERPROFILE%" "%BV%\%MONTH%_Complete\%USERNAME%" /B /E /R:0 /CREATE /NP /TEE /XJ /LOG+:"%BV%\%MONTH%_CompleteBackupLog.txt" /XD %EXC%
   2) robocopy "%USERPROFILE%" "%BV%\%MONTH%_Complete\%USERNAME%" /B /E /R:0 /V /NP /TEE /XJ /LOG+:"%BV%\%MONTH%_CompleteBackupLog.txt" /XD %EXC%

and same here: 1) forfiles /p %BV% /d -%BTTL% /c "CMD /Q /C @rmdir /S /Q @PATH"
    2) forfiles /p %BV% /d -%BTTL% /c "CMD /Q /C del /F /Q @PATH"

Thanks

Viewing all 78 articles
Browse latest View live


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