|
|
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
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.
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
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!
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!
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.
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.
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
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
Enter the thread ID of the thread you are merging into
To report abuse, sign in or continue without signing in
Thank you.
|
|
|
|
Don't have one of the above accounts?