Date created: Thursday, August 16, 2018 9:40:16 AM. Last modified: Thursday, August 16, 2018 9:42:13 AM
RoboCopy and Delete Old Backups
@echo off rem Delete backups older than 30 days: set backupage=-30 rem Backup location without trailing slash set backuproot=D:\LocalBackup rem !END OF USER VARIABLES! set /P AREYOUSURE=Create new backup now (y/n)? if /I %AREYOUSURE% NEQ y GOTO end set year=%date:~-4% set month=%date:~3,2% if "%month:~0,1%" == " " set month=0%month:~1,1% set day=%date:~0,2% if "%day:~0,1%" == " " set day=0%day:~1,1% set hour=%time:~0,2% if "%hour:~0,1%" == " " set hour=0%hour:~1,1% set min=%time:~3,2% if "%min:~0,1%" == " " set min=0%min:~1,1% set sec=%time:~6,2% if "%sec:~0,1%" == " " set secs=0%sec:~1,1% set fulldate=%year%-%month%-%day%--%hour%-%min%-%sec% set backupdir=%backuproot%\%fulldate% mkdir %backupdir% rem /E :: copy subdirectories, including Empty ones. rem rem /COPY:copyflag[s] :: what to COPY (default is /COPY:DAT). rem (copyflags : D=Data, A=Attributes, T=Timestamps). rem (S=Security=NTFS ACLs, O=Owner info, U=aUditing info). rem rem /PURGE :: delete dest files/dirs that no longer exist in source. rem /MIR :: MIRror a directory tree (equivalent to /E plus /PURGE). rem rem /R:n :: number of Retries on failed copies: default 1 million. rem /W:n :: Wait time between retries: default is 30 seconds. echo * Backup: %userprofile%\Applications to D:\LocalBackup\%fulldate%\Applications "%userprofile%\Applications\RoboCopy" "%userprofile%\Applications" "%backupdir%\Applications" /E /COPY:DAT /MIR /R:1 /W:1 /log+:"%backupdir%\%fulldate%_Applications.log" echo * Backup: %userprofile%\Backups to D:\LocalBackup\%fulldate%\Backups "%userprofile%\Applications\RoboCopy" "%userprofile%\Backups" "%backupdir%\Backups" /E /COPY:DAT /MIR /R:1 /W:1 /log+:"%backupdir%\%fulldate%_Backups.log" echo * Backup: %userprofile%\Desktop to D:\LocalBackup\%fulldate%\Desktop "%userprofile%\Applications\RoboCopy" "%userprofile%\Desktop" "%backupdir%\Desktop" /E /COPY:DAT /MIR /R:1 /W:1 /log+:"%backupdir%\%fulldate%_Desktop.log" echo * Backup: %userprofile%\Documents to D:\LocalBackup\%fulldate%\Documents "%userprofile%\Applications\RoboCopy" "%userprofile%\Documents" "%backupdir%\Documents" /E /COPY:DAT /MIR /R:1 /W:1 /log+:"%backupdir%\%fulldate%_Documents.log" rem /P pathname Indicates the path to start searching. rem rem /S Instructs forfiles to recurse into rem subdirectories. Like "DIR /S". rem rem /M searchmask Searches files according to a searchmask. rem The default searchmask is '*' . rem rem /D date Selects files with a last modified date greater rem than or equal to (+), or less than or equal to (-). rem rem /C command Indicates the command to execute for each file. rem @file - returns the name of the file. rem @path - returns the full path of the file. rem rem forfiles is supposed to be used to find files not folders however, rem if the /S flag isn't used to recurse into sub-directories then rem only the backup folders called %fulldate% are returned: rem forfiles /P "D:\LocalBackup" /M "*" /D -30 /C "cmd /c rd /S /Q @PATH" forfiles /P "%backuproot%" /D %backupage% 1> nul 2>&1 rem No files/folders found: if %ERRORLEVEL% GEQ 1 goto end echo * Delete old backups: forfiles /P "%backuproot%" /D %backupage% /C "cmd /C echo @path" set /P AREYOUSURE=Delete these old backups now (y/n)? if /I %AREYOUSURE% NEQ y GOTO end forfiles /P "%backuproot%" /M "*" /D %backupage% /C "cmd /C rd /S /Q @PATH" :end
Previous page: Web Backup Scripts
Next page: DFTBA