WinRAR Tip #7: Batch File Backups

Wednesday, July 5, 2006
By: Matthew Doucette

WinRAR is a file compressor / archiver great for backing up data.  Download WinRAR at rarlab.com/download.htm.

 

WinRAR Tip #7: Batch File Backups:

Customize and make complex backups more efficiently with batch files.

For example, use one of these convenient batch files:

Windows version WinRAR batch file:

REM Windows Version
"C:\Program Files\WinRAR\WinRAR.exe" a -esh -m5 -hpYOURPASSWORD -mt2 -r -s -t "YOURARCHIVE.rar" @"files_to_include.txt" -x@"files_to_exclude.txt"
pause

Command line version WinRAR batch file:

REM Command Line Version
"C:\Program Files\WinRAR\RAR.exe" a -esh -m5 -hpYOURPASSWORD -mt2 -r -s -t "YOURARCHIVE.rar" @"files_to_include.txt" -x@"files_to_exclude.txt"
pause

They backup all files listed in files_to_include.txt but not including all files listed in files_to_exclude.txt.

The archive will be password protected with "YOURPASSWORD" as the password, although you can use the -p switch without specifying a password.  If you do, WinRAR will ask you for it.  If possible, I recommend not including your password so that it is not recored on your computer (inside the batch file).  As previously mentioned, do not use "YOURPASSWORD" as your password!  Visit GRC's Ultra High Security Password Generator for tips on secure password generation.

Let me break down the batch file for you

Note that WinRAR commands do not use a dash (minus sign) and WinRAR switches require a dash (minus sign).  To see more commands and switches, view WinRAR's help, found in the Help menu and named help topics.

Play around with it and test it out.  Be sure to check the results to ensure you have not made any bugs.  Do not just assume your backups work.  Extract your backups and ensure they have everything you need so when the time comes to use them, they will do their job.

 

Use Batch File to Create Filename Using Date and Time:

In a batch file, you can use the %date% and %time% variables to access the date and time.  Then you can extract just the letters and numbers you need.  This allows you to create filenames from the current time and date, so it always creates a new filename that does not overwrite previous filenames.  This is perfect for continual backups.

For example, if %time% equals "14:28:28.06", then %time:~0,2% equals "14".  The "0" defines the position to start (the first string character is 0, not 1) and "2" defines the amount of characters to extract.  So starting at position 0 and extracting 2 characters from "14:28:28.06" gives "14".  Simple.

Try this simple batch file for various examples:

@ECHO OFF

REM %time% AND %date& SAMPLE OUTPUTS:
REM ---------------------------------
REM %time% example: 14:28:28.06
REM %date% example: Mon 03/26/2007

REM GET DATE / TIME:
REM (ensures no switch-over errors during parsing)
REM ----------------------------------------------
SET DATETIME=%date%%time%

REM PARSE DATE:
REM -----------------

SET YYYY=%DATETIME:~10,4%
SET MT=%DATETIME:~4,2%
SET DD=%DATETIME:~7,2%

REM PARSE TIME:
REM -----------------
SET HH=%DATETIME:~14,2%
SET MM=%DATETIME:~17,2%
SET SS=%DATETIME:~20,2%

REM REPLACE LEADING SPACE IN HOUR WITH 0 (IF LESS THAN 10)
REM ------------------------------------------------------
IF %HH% LSS 10 SET HH=0%HH:~1,1%

ECHO year = %YYYY%
ECHO month = %MT%
ECHO day = %DD%
ECHO hours = %HH%
ECHO minutes = %MM%
ECHO seconds = %SS%
ECHO format sample: %YYYY%-%MT%-%DD% %HH%.%MM%.%SS%s
pause

The output shouuld be something like this:

year = 2007
month = 03
day = 30
hours = 12
minutes = 42
seconds = 36
format sample: 2007-03-30 12.42.36s
Press any key to continue . . .

The above batch file example extracts various sections of the time and date strings.  You can combine them together as you wish.  A good example combination would be YYYY-MM-DD to ensure the filenames sort properly.

 

All WinRAR Tips:

 

Have Any Tips To Add?

If you have WinRAR tips you would like to add, please contact us.

 

Also See:

 

External Links:

 

 

About the Author: I am Matthew Doucette of Xona Games, an award-winning indie game studio that I founded with my twin brother. We make intensified arcade-style retro games. Our business, our games, our technology, and we as competitive gamers have won prestigious awards and received worldwide press. Our business has won $190,000 in contests. Our games have ranked from #1 in Canada to #1 in Japan, have become #1 best sellers in multiple countries, have won game contests, and have held 3 of the top 5 rated spots in Japan of all Xbox LIVE indie games. Our game engines have been awarded for technical excellence. And we, the developers, have placed #1 in competitive gaming competitions -- relating to the games we make. Read about our story, our awards, our games, and view our blog.