Backup with WinRar/Batch Files and Task Scheduler in Windows

The last week we setup an internal site for the needs of the company. Although I prefer Linux, the server was running Windows 2003 so we had to go with this.

When we finished the setup and the configuration the last thing we had to do was to find a way to backup our data. When I say backup I mean to backup the data at least every week since it is not updated very often.

Because I don’t know if there is any worth free backup solution for Windows and because I prefer to learn something new I start thinking a way to automate the backup.

What I had was a Windows system and the WinRar tool. I start searching about WinRar and I found that WinRar tool can be used in a terminal through the rar.exe plugin. I must admit that the manual of the WinRar is very good!

As I found what I need (how to make a rar archive and append the year,month and day in the name of my archive) I start writing a batch file. After that, the only thing that left was to make this small batch file repeat itself once a week. The best way to do this is to schedule a task in windows.

Enough with my story. Let’s see some code now!

Here is a sample script that does the job.

@ECHO OFF
"%PROGRAMFILES%WinRARrar.exe" a -agYYYY-MM-dd -r "%USERPROFILE%Desktopdatabackup-.rar" "C:data"
IF %ERRORLEVEL% == 0 (
ECHO %date%, %time% : Backup succeeded >> "%USERPROFILE%DesktopData_Backup_Log.txt"
) ELSE (
IF %ERRORLEVEL% == 1 (
ECHO %date%, %time% : Backup succeeded with warnings >> "%USERPROFILE%DesktopData_Backup_Log.txt"
) ELSE (
ECHO %date%, %time% : Backup failed >> "%USERPROFILE%DesktopData_Backup_Log.txt"
) )

The above script takes all the data from the C:data folder it compress them with the name databackup and appends to the name the year, month and day that was created. Something like databackup-2011-04-19.rar.

You will find the archive and a text file that contains informations about the backup process to your desktop .

Of course you can change the script to fit your needs. Also you can add and network commands if you want to move your backup in a network drive or something. So a small example for moving our backup in a network drive is this :

NET USE I:
IF %ERRORLEVEL% == 0 ( MOVE "%USERPROFILE%Desktop*.rar" I:Backup
) ELSE (
ECHO ERROR: UNMOUNTED DISK
PAUSE
)

The last thing we have to do is to create a task for our batch file. Here is how you can do this:

  • Start->Programs-> Accessories->System Tools->Scheduled Tasks
  • Create a new task
  • Press Next and then Press the Browse button and select your batch script.
  • Something very important here is the optional Start in field. You must enter the path of your script otherwise it’s not going to work. For example, if our script is located in the C:myscriptbackup_script.bat you must enter in the Start in field this: C:myscript
  • Enter the info for your script and when you want it to run
  • Before you press the finish button make sure that you have checked the box Open the Advanced Properties for this task when I click finish
  • After that in the properties window set the password for your account. By doing this the system will be able to execute our script even if we are not logged on.

The above procedure is for Windows Server 2003 but it’s also working for Windows 7.

That’s all!

4 Comments

  1. vago says:

    Hey buddy, the example is great but What should I do if i want to compress only a file and not taking all of them (inside of the folder). I’m confused, could you help please i’m breaking my head with this …

  2. vago says:

    dude I made a mistake, your code works perfectly. But you didn’t say how to delete the older file.

  3. the same person says:

    hey man, it doesn’t matter, your code works perfectly I don’t need your help now. Thanks for your post!

    1. I am not an expert but you can use this somewhere in your batch file:

      FORFILES /P “%USERPROFILE%Desktoptest” /D -1 /M *.rar /C “cmd /c del @path”

      The above line will erase all the .rar files that are older than a day and are located inside the test folder. The @path gives the full path and name of the file.Be cautious with the del command…Hope it helps!

Leave a Comment