February 25, 2008

Synchronize Files with rsync

I have been looking for an effective way to synchronize files across machines for quite some time. I researched online and found quite a few programs that synchronize files. However, no one program was overwhelmingly better than the rest and had the features I was looking for. I really needed to implement something, so last week I decided to see what I can do anything with rsync. After about a day, I came up with a simple shell script that worked for me. It is not glamorous, but it gets the job done. Here is an explanation of how you can set up something similar.

Requirements

The image above illustrates an example computing environment that will be used in this post. This environment can be expanded, but for the simple shell script to work, the following items are necessary.

  • Every machine has ssh and rsync installed.

  • The Sync Server is the main synchronization point. The clients (Laptop, Home and Work Desktop) always synchronize their data to it, and not to each other.

  • SSH is already configured to provide the necessary level of security for the data you are synchronizing. The proper configuration of the SSH server and clients is beyond the scope of this post.

  • The clients must use the same top-level synchronization directory. The Sync Server's top-level synchronization directory can differ from the clients'.

In this example, the following directory structure will be used to store the synchronizable data. Both the clients and the Sync Server use the same top-level synchronization directory.

/sync/ - the top-level synchronization directory /sync/mydocs/ - directory for personal documents /sync/apps/ - directory for application data /sync/apps/bash/ - directory that holds the synchronization shell script (IMPORTANT) /sync/apps/ffox/ - copy of your firefox profile, modify your ~/.mozilla/firefox/profiles.ini to point here or use a symlink /sync/data/ - directory containing other data /sync/otherdata/ - directory containing data that we do not want to synchronize

If you have an user account on a restricted server and want to use it as the Sync Server, simply alter your paths to use your home directory.

~yourlogin/sync/ ~yourlogin/sync/mydocs/ ~yourlogin/sync/apps/ ~yourlogin/sync/apps/bash/ ~yourlogin/sync/apps/ffox/ ~yourlogin/sync/data/

Setup

  1. Create the top-level synchronization directory on all clients and the Sync Server. Set proper permisions and ownership as appropriate. The permissions of the files and directories are preserved during synchronization.

    $ mkdir /sync
  2. On one of your clients, create the directory that will store the shell script. In this example, that directory is /sync/apps/bash/.

    $ mkdir /sync/apps $ mkdir /sync/apps/bash
  3. Create the following file. Modify the highlighted portions to your specific configuration.

    /sync/apps/bash/bashrc sync_rsync_options='-auv --exclude-from=/sync/apps/bash/sync-exclude' sync_directory_up='/sync/{mydocs,apps,data} sync-server.dns:/sync/' sync_directory_down='sync-server.dns:/sync/{mydocs,apps,data} /sync/' alias sync-up-pretend="rsync -n --delete-delay --delete-excluded ${sync_rsync_options} ${sync_directory_up}" alias sync-up-full="rsync --delete-delay --delete-excluded ${sync_rsync_options} ${sync_directory_up}" alias sync-up-update="rsync --delete-excluded ${sync_rsync_options} ${sync_directory_up}" alias sync-down-pretend="rsync -On --delete-delay ${sync_rsync_options} ${sync_directory_down}" alias sync-down-full="rsync -O --delete-delay ${sync_rsync_options} ${sync_directory_down}" alias sync-down-update="rsync -O ${sync_rsync_options} ${sync_directory_down}" unset sync_rsync_options sync_directory_up sync_directory_down

    This file provides you with six simple command alliases to do synchronization. Those will be explained later in the Usage section.

  4. Create an exclude pattern file. Its basic usage it to prevent temporary files and cached data from being synchronized and wasting bandwith and storage. If you want complete synchronization, leave the file blank. Here is an example file:

    /sync/apps/bash/sync-exclude #General excludes *~ #Mozilla Firefox Cache/ XUL.mfasl

Installation

  1. Copy the shell script folder to every client.

  2. Add the following line to the bottom of your .bashrc file.

    ~yourlogin/.bashrc ... source /sync/apps/bash/bashrc

    This will make the new synchronization command alliases available in all future bash sessions. You can run the above line as a command and the new alliases will be available immediatelly. Now you are ready to synchronize your data!

  3. Move directories and files under the top-level synchronization directory and create symlinks to data that needs to appear somewhere else.

Usage

  • sync-up-full

    Uploads all newly modified files to the Sync Server and deletes all files that are no longer present on the client from the Sync Server. CAUTION! It may delete important files from the Sync Server. If these files exist on other clients, a sync-up-full from one of those clients will restore them. Use with care.

  • sync-down-full

    Downloads all new newly modified files from the Sync Server to the client and deletes all files no longeer present on the Sync Server from the client. CAUTION! It may delete important files that have not yet been synchronized to the Sync Server with sync-up-update.

  • sync-up-update

    Uploads all newly modified files to the Sync Server.

  • sync-down-full

    Downloads all new newly modified files from the Sync Server.

  • sync-up-pretend, sync-down-pretent

    These commands will give you an overview of what will happen if you run sync-up-full or sync-down-full. No files are modified.

While this is a very simple synchronization setup, so far it has worked well for me. It is still a work in progress. I encourage you to read the rsync manual. It explains in detail the exclude patterns and the source and target specification rules.

February 16, 2008

Comercial Infrared (LIRC) on a Dell Inspiron 9100 - Part One

A couple of months ago I switched to using Gentoo Linux as my main operating system. My laptop, a Dell Inspiron 9100, has an infrared port, but I have never been able to use it for anything useful. I tried to set up LIRC (Linux Infrared Remote Control) in the past on other Linux distributions and even in Windows XP with WinLIRC, but had no success. It took a lot of reading and testing, but I finally have LIRC working. In this post, I describe how to set it up properly.

Hardware

The Dell Inspiron 9100 has a SMSC IrCC controller chip. These chips are present in many other laptops and this guide may help you even if you do not own the same laptop as me. The IrCC chips support both IRDA and CIR (Comercial Infrared) standards.

Configuring the hardware

Before installing any software, check which COMM port the infrared controller is assigned to. This setting is located in the BIOS, so reboot your laptop and locate it. Typically it is set to COMM2 and that is what I used. COMM2 also works with the IRDA driver, in case you may want to use that in the future. I tried using COMM1, but could not get it to work.

Installing LIRC

In order to receive remote control infrared signals in Linux, you need to install LIRC. In Gentoo, this involves emerging (installing) lirc or one of the higher level packages that depend on it. I personally use KDE so I would emerge kdelirc or the meta package kdeutils-meta with the lirc USE flag set.

Before you begin emerging, however, you need to set a couple of options in /etc/make.conf to properly install lirc. First, you need to define which devices you plan to use with lirc so the appropriate modules are build during the emerge process. This is controlled by the LIRC_DEVICES option in /etc/make.conf. You can set it to all and lirc will support all available devices. For our controller, only the SIR driver is needed, therefore: /etc/make.conf ... LIRC_DEVICES="sir" ... is sufficient.

For this particular device, I also enabled both the hardware-carrier and transmitter USE flags.

/etc/make.conf USE=" ... hardware-carrier transmitter ... "

I am not sure if they are necessary or useful, but I do not have time to test that given that I got it to work.

Now that these options are set, you can install lirc. In my configuration I set the lirc use flag and emerged kdeutils-meta.

/etc/make.conf USE=" ... lirc ... " # emerge kdeutils-meta

Configuration

After the packages have been successfully emerged, the LIRC kernel module, lirc_sir, needs to be configured.

Beforehand, however, make sure that the IRDA kernel module, smsc_ircc2, does not load automatically. This is unlikely, but it may happen that it is properly configured and able to load automatically on its own. Check with this command:

$ lsmod | grep smsc

If smsc_ircc2 is present, you need to blacklist it to prevent if from loading automatically. The lirc_sir and smsc_ircc2 modules cannot be loaded at the same time. To blacklist a module, edit /etc/modprobe.d/blacklist and add the following line, usually at the bottom:

/etc/modprobe.d/blacklist blacklist smsc-ircc2

After making sure that the IRDA module does not load, the LIRC module can be properly configured. Until recently I did not know about the modinfo command and I had no idea how to do this correctly. This command gives you information about the module and what options the module accepts. Here is an example output of the modinfo command on my lirc_sir module.

# modinfo lirc_sir filename: /lib/modules/2.6.22-suspend2-r2/misc/lirc_sir.ko license: GPL author: Milan Pikula description: Infrared receiver driver for SIR type serial ports depends: vermagic: 2.6.22-suspend2-r2 SMP mod_unload PENTIUM4 parm: io:I/O address base (0x3f8 or 0x2f8) (int) parm: irq:Interrupt (4 or 3) (int) parm: threshold:space detection threshold (3) (int) parm: debug:Enable debugging messages (bool)

Look at the "parm:" lines. These are the options that the lirc_sir module accepts. These can be specified while running the modprobe command. In order to load the lirc_sir module properly, the top three parameters need to be specified. When using COMM2, the following settings worked and properly loaded the module:

# modprobe lirc_sir io=0x2f8 irq=3 threshold=5

The first two parameters are specific to COMM2. The threshold parameter I have not explored in detail. I tried two, three (the default), four and five. Four and five were the two that worked so I chose five. You can experiment with those. I did not notice any negative site effects.

When the module loads successfully, you can test it with the mode2 program (run it as root). Just start the program and try pressing some keys on the remote while facing the receiver. You should see lines like to following start to appear.

# mode2 ... space 630 pulse 275 space 363 pulse 515 pulse 514 space 1763 pulse 514 space 1765 pulse 513 space 1798 ...

If the screen remains the same, then something is not correct. Verify the settings and maybe experiment with the threshold parameter. To terminate the program, press Ctrl-C. Now the module is properly configured. To preserve the settings, add a new file (I named mine lirc-sir) to /etc/modprobe.d/ with the following text.

/etc/modprobe.d/lirc-sir options lirc-sir io=0x2f8 irq=3 threshold=5

Then run modules-update to integrate the settings from this file.

# modules-update

Now, just typing modprobe lirc_sir will work. The options are looked up automatically. This module needs to be loaded before the lirc service is started. There are several ways to get this accomplished. The easy way it to just add lirc_sir to /etc/modules.autoload.d/kernel-2.6 or to the kernel-2.4 file if you do not use a 2.6 series kernel.

/etc/modules.autoload.d/kernel-2.6 ... lirc_sir

Starting LIRC

After the module is configured, the lirc daemon needs to be added to the start-up services. In Gentoo, the command is the following:

# rc-update add lircd default

In addition to lircd daemon, there is also a lircmd daemon. This daemon interprets button presses and sends them as mouse events to the system. I have not tried it yet, but it may make a nice follow-up post in the future.

From here you can follow the Configuring LIRC portion of the LIRC manual and everything should work.

In a follow up post I will demonstrate now to configure and use your remote in KDE.

References

Testing

The preview feature in Blogger does not let me see all the styles that are applied to a post. I guess I am going to have to make an testing post, like this one, so I can see how the styles will look.

Heading 1

text code text

February 1, 2008

The First Post

Hello and welcome to my personal blog. In this first post, I would like to quickly describe what you can expect to see here in the future. I plan to blog about:

  • Computing
  • Networking
  • Operating systems
  • Interesting software
  • Interesting websites
  • Programming
  • Productivity
  • Various personal projects and items of interest
This is neither a complete nor exclusive list of topics. I will try to tag posts adequately so if you choose to subscribe to an RSS feed, you get only what you are interested in. I hope you find the information on this blog useful.