Create backup archive using RAR

Sometimes it's quite handy to be able to archive a project or folder into a date stamped archive without manually doing the job each and every time. Enter RAR and a bat-script. If you don't already have, download WinRAR from here.

Mission brief

So lets set up a bat-script to archive a folder and date stamp the archive. The mission brief is to backup all files within the D:\Projects\MyProject\Source\ folder, but exclude temporary files created by the ReSharper plugin.

Creating the required files

First we create a text file containing all the paths we want to archive. Notice that you don't need to specify sub folders as we'll use the recursive function when archiving. We'll put the files in the same folder which will act as a base when storing the paths, we'll use "D:\Projects\".

filelist.lst
D:\Projects\MyProject\Source\

Now we create another text file, but this time with the paths within the one specified above that we don't want included in the archive, i e the ReSharper plugin working folder. If you want to exclude any other folders or files, here is where you want to add them.

exlude.lst
D:\Projects\MyProject\Source\_ReSharper.MyProject

Okay, lets create the actual bat file. You might want to change the path towards your WinRAR installation or the date stamp pattern. I use YYYYMMDDHHMM which gives (without the plus signs) year+month+day+hour+minute using two digits for each value (except year which uses four).

Backup.bat
@echo off
"C:\Program Files\WinRAR\rar.exe" a -agYYYYMMDDHHMM "MyBackupFolder\MyProject-Backup-" -ep1 -r -m5 -x@"exclude.lst" @"filelist.lst"

Explanation of the parameters

a
Add files to archive

-ag[format]
Generate archive name using the current date

ep1 
Exclude base directory from names (we don't need to archive D:\Projects\, we're happy with archiving the content as MyProject\Source\...).

r
Recurse subdirectories

m<0..5>
Set compression level (0-store...3-default...5-maximal) (we're using 5 in the example above)

The above script will create an archive in "MyBackupFolder" called "MyProject-Backup-201209251907.rar".

Final words

That's it! Now you have a nice archiving script that you either can trigger from the bat file or why not set it up as a scheduled job?

Related posts:

Comments

comments powered by Disqus