Script giornaliero Robocopy backup incrementale


robocopy

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

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=C:\tmp

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 C
    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=%date:~3,2%
FOR /F "TOKENS=1,2,3* delims=/ " %%A IN ('DATE/T') DO SET DAY=%date:~0,2%
FOR /f "TOKENS=1,2,3 delims=: " %%A in ('TIME /T') do set TM=%date:~-4,4%

IF %MON% == 01 (SET MONTH=Gennaio
 goto :begin)
IF %MON% == 02 (SET MONTH=Febbraio
 goto :begin)
IF %MON% == 03 (SET MONTH=Marzo
 goto :begin)
IF %MON% == 04 (SET MONTH=Aprile
 goto :begin)
IF %MON% == 05 (SET MONTH=Maggio
 goto :begin)
IF %MON% == 06 (SET MONTH=Giugno
 goto :begin)
IF %MON% == 07 (SET MONTH=Luglio
 goto :begin)
IF %MON% == 08 (SET MONTH=Agosto
 goto :begin)
IF %MON% == 09 (SET MONTH=Settembre
 goto :begin)
IF %MON% == 10 (SET MONTH=Ottobre
 goto :begin)
IF %MON% == 11 (SET MONTH=Novembre
 goto :begin)
IF %MON% == 12 (SET MONTH=Dicembre
 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%_Completo" (
    echo.
    echo.
    echo  Performing incremental backup...
    echo.
    echo.
    mkdir "%BV%\%MONTH%_%DAY%_%TM%_Incrementale"
    set BLOG=%BV%\%MONTH%_%DAY%_%TM%_Incrementale_BackupLog.txt
    robocopy "C:\Cartella1" "%BV%\%MONTH%_%DAY%_%TM%_Incrementale\Cartella1" /B /E /M /R:0 /V /NP /TEE /XJ /LOG+:"%BV%\%MONTH%_%DAY%_%TM%_Incrementale_BackupLog_Cartella1.txt" /XD %EXC%
	robocopy "C:\Cartella2" "%BV%\%MONTH%_%DAY%_%TM%_Incrementale\Cartella2" /B /E /M /R:0 /V /NP /TEE /XJ /LOG+:"%BV%\%MONTH%_%DAY%_%TM%_Incrementale_BackupLog_Cartella2.txt" /XD %EXC%
) else (
    echo.
    echo.
    echo  Performing complete backup...
    echo.
    echo.
    set BLOG=%BV%\%MONTH%_Completo_BackupLog.txt
    robocopy "C:\Cartella1" "%BV%\%MONTH%_Completo\Cartella1" /B /E /R:0 /CREATE /NP /TEE /XJ /LOG+:"%BV%\%MONTH%_Completo_BackupLog_Cartella1.txt" /XD %EXC%
    robocopy "C:\Cartella1" "%BV%\%MONTH%_Completo\Cartella1" /B /E /R:0 /V /NP /TEE /XJ /LOG+:"%BV%\%MONTH%_Completo_BackupLog_Cartella1.txt" /XD %EXC%
	
	robocopy "C:\Cartella2" "%BV%\%MONTH%_Completo\Cartella2" /B /E /R:0 /CREATE /NP /TEE /XJ /LOG+:"%BV%\%MONTH%_Completo_BackupLog_Cartella2.txt" /XD %EXC%
    robocopy "C:\Cartella2" "%BV%\%MONTH%_Completo\Cartella2" /B /E /R:0 /V /NP /TEE /XJ /LOG+:"%BV%\%MONTH%_Completo_BackupLog_Cartella2.txt" /XD %EXC%
attrib -A "C:\Cartella1\*.*" /S
attrib -A "C:\Cartella2\*.*" /S
    )
)

:end

exit

In allegato, lo script di esempio:
Script Backup Incrementale