Friday, September 05, 2008

Automatic system backups

The need of creating backups is obvious. Data is the most important thing on everyone's PC. But how about all the environment? What about system, it's configuration, configuration of all installed applications? A while ago a new idea was presented - image creating software. The first was (I think) Norton Ghost. At the very begging the app was very, very small (just a little bit bigger than 100 kilobytes) and run under DOS (so yes, you did have to have a DOS bootable diskette, or ability to boot into plain old DOS environment). This kind of software became more and more popular, and nowadays there are dozens of applications you may choose from. There is, however, one I would like to present - special version of Acronis True Image available under the name of "MaxBlast" at Seagate's site. This software is free for everyone who has Maxtor or Seagate hard drive installed in their PC. I am not sure which version of Acronis True Image is being available under the label of MaxBlast, but as far as I remember it is version 10.0 (there is version 11.0 of Acronis True Image available). So what is so great about this software except it's price? Well, it suits my needs very good: it creates really small images (although it takes a lot of time, if you choose the maximum compression level), and allows to restore images in a very convenient way - you just decide what you want to do, and if you choose to restore whole system partition (the one you actually work on) the software boots before windows, and makes all operations necessary to restore your previous environment.

There is one thing, I think would be very handful for a lot of people. Possibility of storing configuration on-line. I imagine it should work like this: all applications will use the same way of storing all its settings (for example somewhere in Applications Data). Zipping those files should not be a problem. And of course it should be possibile to restore the configuration in only few clicks. I would like it to be connected with one of the free file storing services (Dropbox, Microsoft's SkyDrive or any other). Advantage of such a behavior is obvious - one could save a lot of time in case of reinstalling system and all applications (and it is not always possible to restore previously created system image - sometimes you just install your system from scratch, for example after acquiring new machine...).

Anyway... going back to system backups. They are really important, but what I have learned while working with Windows is that registry files are crucial. Sometimes you may wonder what happened, and after deep investigation you realize that one (or more) of registry files is broken. So... what to do? Creating system backup everyday is not convenient (and as I said before - it takes a lot of time. Personally I create full system backup once a week only). But there is a way to keep those vulnerable files in safe place in case something happens. There is even a small and handy application written that does this task for you - Erunt. I started using this small tool a while ago, and after a couple of weeks I have spotted an interesting article that shows how to configure Erunt using task scheduler, so it is being run every time your Windows starts. It is great, but... those backup files are a bit too big (registry backup on my Vista based laptop takes about 80MB for each day - for me it is definitely too big). I started to create a small batch, that you may find useful. So, here it is:

  1: @echo off
  2:
  3: set root=AutoBackup
  4: set maxfiles=5
  5:
  6: "erunt\AUTOBACK.EXE" %root%\#Date# sysreg otherusers 
/noconfirmdelete /noprogresswindow
  7:
  8: rem setting date variables
  9: set day=%DATE:~7,2%
 10: if %day:~0,1% == 0 (set shortday=%DATE:~8,1%) else (set shortday=%DATE:~7,2%)
 11: set month=%DATE:~4,2%
 12: if %month:~0,1% == 0 (set shortmonth=%DATE:~5,1%) else (set shortmonth=%month%)
 13: set year=%DATE:~-4%
 14:
 15: rem setting archive and folder names...
 16: set archive_name=%year%-%month%-%day%
 17: set folder_name=%shortmonth%-%shortday%-%year%
 18:
 19: rem compress the registry backup files...
 20: cd %root%
 21: rem backup command: "c:\program files\7-Zip\7z.exe
a -r -t7z -mx9 <archive name> <directory>
 22: "c:\program files\7-Zip\7z.exe" a -r -t7z -mx9 %archive_name%.7z %folder_name%
 23: cd ..
 24:
 25: rem ...removing folder...
 26: rd /S /Q %root%\%folder_name%
 27:
 28: rem ...checking if there are some old-registry backup files 
that should be deleted
 29: @if exist files.txt ( del files.txt )
 30: cd %root%
 31:  @set filenum=0
 32:  @dir *.7z /b /o:-n > ..\files.txt
 33:
 34:  for /f %%f in (..\files.txt) do (
 35:   call ..\autorun-del.cmd %%f
 36:  )
 37: cd..
 38: if exist files.txt ( del files.txt )
and (thanks to the limitations of batch files) the second, very small batch:
  1: set /a filenum=filenum+1
  2: if %filenum% GTR %maxfiles% ( del /F /Q %1 )
How does it work? The idea is simple. After creating folder that contains backup of all registry files - the folder and its' content is being compressed. Then, the folder is being deleted. At the end the script checks if there are too many backup files (i.e. if amount of backup files is greater than the one you set in 'autorun.cmd') - if so, the oldest file(s) are deleted. I have set the script to run before logging into Windows, but you can of course run the script manually (if using Vista with UAC remember to give it proper privileges). Archives containing backups of registry files are created per-day - if the script is being run more then once - it updates the content of the archive. I assumed that:
  • the folder where Erunt is stored is named 'Erunt'; both batch files should be located in the Erunt's parent folder
  • archives with registry backup files are stored in Erunt's sibling-folder. The folder's name is 'AutoBackup' (you may change it - folder name is stored in %root% variable)
  • default amount of backup archives is 5 - it may be changed by editing %maxfiles% - thanks to compression one archive is only about 11MB, so even storing backups of last two weeks is not a big-deal.
After struggling with windows batch files, I solemnly promised myself not to use it any more. Next time I will have to create anything like this, I will use PowerShell or Python ;) Batch files are a real pain. Hope someone finds it a bit helpful. For me it is a way to be more confident that in case of system damage I will be able to recover my environment very fast.

No comments: