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
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

Viewing all articles
Browse latest Browse all 78

Trending Articles



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