Xona.com is the collaborative spyware-free web presence of Jason Doucette & Matthew Doucette.
Home
Contact
About
Forums
 ___
Articles:
Tech
Windows
Webmaster
Blog
Coding
 ___
Web Utilities:
Domain Hacks
Suggest
 ___
Freeware:
ZoomView
AntiSpam Mailto
 ___
Quotes:
Inspirational
Intelligence
Education
Action
Scientific
Chess
Financial
Programming
 ___
Books
Videos
Wallpapers
 ___
Music
Color List
TI-99/4A
Parsec
Optical Illusions
Web Design
Trivia
Domains
Donations
Copyrights
Disclaimer
Links
 ___
Forums
About
Contact
WinRAR Tip #7: Batch File Backups


Wednesday, July 05, 2006
By: Matthew Doucette
Printer Friendly Version

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

  • REM stand for "remarks" and is how you provide commenting.  Anything after "REM" is ignore during execution.  In the examples above, I labelled the windows and command line versions using the REM command.
  • "C:\Program Files\WinRAR\WinRAR.exe" is the full path to the Windows version of WinRAR.  Change this if you have installed it in a different location.
  • "C:\Program Files\WinRAR\RAR.exe" is the full path to the command line version of WinRAR.  Change this if you have installed it in a different location.
  • a stands for "add to archive" which tells WinRAR to wish to make an archive as opposed to extracing files from an archive or many other things it can do.
  • -m5 sets the compression method.  -m5 is the best (and slowest) compression.  -m3 is the normal and default compression.  -m1 is the fastest (least compressive) compression.  -m0 sets no compression.  See WinRAR's help for more details.
  • -hpYOURPASSWORD sets the password you wish to use for archives that are password protected.  Replace YOURPASSWORD with your actual password.  If you use -hp instead, with a blank password, WinRAR will ask you for it manually each time.  I recommend using -hp with a blank password as having your password stored in a batch file is a security risk.  Note, this switch is similar to -p, but switch -p encrypts only file data and leaves other information like file names visible.  -hp encrypts all sensitive archive areas including file data, file names, sizes, attributes, comments and other blocks, so it provides a higher security level.  Without a password it is impossible to view even the list of files in archive encrypted with -hp.
  • -mt2 sets the number of threads to 2.  You can set the value from 0 to 16.  -mt0 makes WinRAR use its single threaded algorithm.  -mt1 to -mt16 makes WinRAR use its multi-threaded algorithm with as many threads as you specified.  The multi-threaded algorithm has slightly different compressions than the single threaded algorithm.  Use this if you have a multi-processor or hyper-threaded CPU, and set the value to the number of logical processors you have.  Also see our WinRAR Tip #5: Run with Multi-Threading tip in this same article.  If you do not know what you have, then do not use this switch.  Note that using more threads affects the compression ratio, so the same archive created with different amount of threads will produce slightly different archives.
  • -r sets WinRAR to recurse subfolders.
  • -s sets WinRAR to create a solid archive.  A solid archive is less secure as it each file is archived as a whole.  This means greater compression but less stability in the archive.  If your archive file becomes slightly corrupted, the entire archive will be unextractable with solid archiving.  Without solid archiving, only the files stored in the section of the file that is corrupted will be lost.  (You may also wish to see the data recovery -rr switch if you use solid archiving.  See WinRAR help files for details.)  A solid archive can have incredible compression rates if you are compression a lot of similar files.  I have compressed 1.5GB of source code files down to less than 15MB with this option.  It is incredible!
  • -t tests files after archiving.  I recommend this switch!
  • "YOURARCHIVE.rar" is the filename of the RAR archive you are going to make.  Name it whatever you wish.
  • @FILENAME includes the files listed inside FILENAME in the archive.  This is how you tell WinRAR what to archive.  You can list files from all over your computer, just be sure to include the full path if the files are not found in the same folder as the batch file.
  • -x@FILENAME excludes the file listed inside FILENAME from being archived.  This is excellent for skipping unimportant files.
  • -esh does not compress system (s) and hidden (h) files.  The -e switch sets file exclude and include attributes.  See WinRAR help files for more details and variations of this switch.  This switch makes it convenient to create a WinRAR backup batch file that archives your home folder (e.g., C:\Documents and Settings\USERNAME\*.*) without archiving needless system and hidden files.  That said, there are some system and hidden files that may be important to you, like Outlook Express email archives found in the hidden C:\Documents and Settings\USERNAME\Application Data\Identities folder.

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:

 

By: Matthew Doucette
(Interested in Writing Articles For Xona.com?)



> Home
> Discuss
> Contact
  ©Xona.com™


Email:
Xona.com™ (formerly Xonatech™ & Saw Tooth Distortion™)
2,482,499 page views (since 2004-Jul-27)

Wills | Php Scripts | Mortgage Calculator | Php Scripts | Repair Bad Credit