July 3, 2008

HowTo: Automatically Backup Windows Machines to One Centralized Data Storage

I recently needed to set up an automatic backup for a few Windows workstations and few virtual machines running under VirtualBox. All data should be transferred to one standalone storage -- a RAID 0 Icybox disks.

Having read about rsync, the first idea was to set up rsync to do the backup. Rsync however requires an rsync daemon, or a unix shell on both synced machines. Although it is possible to run rsync on Windows using cygwin, there is no way to get rsync running on the Icybox.

Sidebar: What is rsync?

To clarify things a bit: rsync is a tools for remote synchronization of files. Feed it with two arguments -- the source folder and the destination folder and it will intelligently sync their contents. It will not blindly copy one folder over another, it will detect which files are outdated in the destination and will only copy these, saving your bandwidth and transfer time.

Working around the problem

I solved the problem with Icebox and Windows machines using a third, "man in the middle" server, which is actually running rsync and performing the backup between the mounted network shares.

The scheme is simple. Icybox is one big smb share, so I mount it on the "middle" server (/media/shares/users-backup). Each user is sharing all folders, that need to be backed up, and they are mounted too. I mount the user shares in one parent directory (/media/shares/users/username), which allows me to run rsync recursively on /media/shares/users, so the whole mounting machinery gets transparent for rsync.

Running the command rsync /media/shares/users /media/shares/users-backup will nicely backup all mounted shares to the remote disk. Note, that rsync is blissfully unaware of the fact that it is syncing two remote directories -- it thinks it is doing just a local copy. It also doesn't care what shares are mounted -- what is mounted at the time rsync runs is backed up.

The rsync command is added to cron and it is run each day at 1:00 am. All that is required from the users is to share their backup folders. The shares might be even protected with passwords (on both sides).

Show me teh code

To cook this delicious meal, we would need:

  • rsync
  • smbfs and/or cifs
  • cron

Step 1: Mount

The mounting needs to be done in /etc/fstab so it can be mounted at system startup and by executing mount -a command.

Open the /etc/fstab file and add one line for each of your shares:

//tomas/projects /media/shares/users/tomas/ smbfs ro,user,guest,nounix 0 0 
If your shares are standars Windows shares, or samba shares, use the smbfs option, if you are using nfs use cifs. Smbfs is unmantained and replaced by cifs, but unfortunately, cifs cannot resolve netbios hostnames. If your hosts are are using dhcp addresses, using hostnames instead of IPs is always a good idea.

It might happen that you will not be able to mount you share with smbfs. If this is the case, double check, that your share name does not end with a slash.Using //tomas/projects is ok, but using //tomas/projects/ will get you a nice "is not directory" error. If your share name is ok, but you are still unable to mount, try the cifs option.

Step 2: Write the rsync script

This is nothing complicated, just run rsync and log the times. As the first thing, we remount the shares, so if someone turned on or off the computer since the last mount, we get the current state.
#!/bin/bash
echo -e "\n===Rsync Start: `date` ===" >> /var/log/rsync-users.log

mount -a -o remount >> /var/log/rsync-users.log

rsync --verbose --stats --recursive --checksum --update --times/media/shares/users/ /media/shares/users-backup/ | tee -a/var/log/rsync.log

echo "===Rsync Stop: `date`===" >> /var/log/rsync-users.log
To ignore some files and directories, I use the --exclude-from option and pass it the file with each exclusion pattern on new line. The following sample is used in the script that backs up our vmware servers.
.*
*.vmem
*.WRITELOCK
*.log

Step 3: Add rsync to cron

Edit /etc/crontab and add following line:

1 0 * * *      backup    /home/backup/rsync-users
The second column specifies a user, which the script should run under.

That's it. The machinery starts at 1 am and all computers that are turned on are backed up. The backup is incremental so it usually takes only few minutes to resync.


June 25, 2008

Create new eclipse workspace -- with all your old settings

It's all a matter of taste. Do you like to have just one workspace for all your projects, or do you prefer to have multiple separate workspaces?

Sure, the first way seems to be the official, supported. It should be easy to manage the workspace -- given the tools like working sets (and working set filters), mylyn and the ability to close projects.

But I still don't get it.

I hate when my workspace is overflowing with projects, I want to have as many workspaces as projects.

So I create new workspace and live happily ever after.

But wait -- all my settings are gone. All my carefully crafted custom templates, all my keybindings, my font settings, everything is gone.

It's all text, fortunately


Lucky us. All eclipse settings are saved as a plain text in the workspace directory. So if you want to create new workspace, but preserve your settings, I have two answers for you:

The short answer

All settings are stored in the .metadata/.plugins/org.eclipse.core.runtime/.settings directory. I mean -- all relevant settings. If you look into .metadata/.plugins directory there are many more directories with settings, but they are too project specific. I've walked trough these configuration files one by one, believe me, nothing useful lies hidden there.

So the short answer is: If you want to create a new eclipse workspace and preserve all your settings, simply copy the .metadata/.plugins/org.eclipse.core.runtime/.settings directory into your new workspace directory.

The long answer

Let the code do the talk for me.
I have created a (simple) shell script that automates new workspace creation. The downside is that it requires either *nix or windows with cygwin. It has been tested by me, I and myself so it should work (most of the time).

To use it, save it somewhere, make it executable (chmod +x new-workspace.sh) and run it either in interactive mode
./new-workspace.sh -i
where it will ask you the details, or with paths to your workspaces (the new workspace directory will be created for you, just specify the path)
./new-workspace.sh old-workspace new-workspace.
If the script doesn't work for you, drop me a comment. Feel free to improve it (you may drop me a comment too).

Update: the pastebin page expired (although I'd swear I checked the keep forever option), so I moved the script over to github.


May 27, 2008

Making JavaDoc a bit more usable

First of all, I know people realized JavaDoc is a pain in the ass to use. I am also well aware of all those "let's make better JavaDoc" projects out there. Just for reference, there is

  1. Javadoc online, which is a simple JavaDoc search engine
  2. Docjar, with its tiny, unintuitive flash based JavaDoc browser, which takes ages to load
  3. Windows Help format JavaDoc
  4. Doctree -- directory of (all) JavaDoc sites
  5. Globaldocs, a JavaDoc browser
I am also well aware that each modern IDE can display JavaDoc, if you configure the proper URL first.

Call me old-fogyish, but I tried most of them and always ultimately returned to the (good?) ol' JavaDoc.


Fortunately, I found a way to cure JavaDoc's biggest wound -- it's inability to let me search for a class or a method. The cure is called Greasemonkey and JavaDoc Search Frame or JavaDoc Incremental Search script. How does it work? Install Greasemonkey and install the script. Point your browser on some JavaDoc site and enjoy the search.

Fortunately, it will work on (almost) all JavaDocs in the wild and requires no support from the JavaDoc creator. When the class html frame is loaded, Greasemonkey runs the script and the script automagically adds the search box.

JavaDoc Incremental Search


This is the older of the two scripts. It will display a search box above the class list and will filter out the classes as you type.



JavaDoc Search Frame


Based on the JavaDoc Incremental Search. This script completely removes the packages frame and spans the class frame vertically. It can search package names too and groups the results into packages, classes, interfaces etc.

Now, if only I could also search the methods..