Bash Completion for the gem Command

The gem command is the command line user interface to the RubyGems system for managing Ruby packages.

This command has a few sub-commands itself and a long list of options that differ per sub-command. Remembering and typing them can be tedious, but thankfully, if you are using the bash command shell, it can help with this task. It only has to be told how.

I’ve packaged the how in this file. Copy it to /etc/bash_completion.d and hope that your bash installation picks it up the next time you start a shell.

On Debian/Linux system that’s just how it works. If this does not work on your particular version of Linux or Unix, you’ll have to look up how completions are handled on your system.

Timestamping files from commandline and Konqueror

In order to keep multiple versions of a file, I sometimes add a timestamp to the filename. Doing this by hand is tedious and error-prone, but this chore can be automated by a small shell script.

/usr/local/bin/timestamp

#! /bin/sh

for oldname ; do
  ts=`date +'%Y%m%d%H%M' --reference="$oldname"`

  newname=`echo $oldname | sed -nre "s/^(\\.?)([^\\.]+)(-[[:digit:]]{12})(\\..*)\$/\\1\\2-$ts\\4/p"`
  if [ -z "$newname" ]; then
    newname=`echo $oldname | sed -nre "s/^(\\.?)([^\\.]+)(\\..*)\$/\\1\\2-$ts\\3/p"`
  fi
  if [ -z "$newname" ]; then
    newname=`echo $oldname | sed -nre "s/^(\\.?)([^\\.]+)(-[[:digit:]]{12})\$/\\1\\2-$ts/p"`
  fi
  if [ -z "$newname" ]; then
    newname=`echo $oldname | sed -nre "s/^(\\.?)([^\\.]+)\$/\\1\\2-$ts/p"`
  fi

  if [ -f "$newname" ]; then
    if [ "$newname" != "$oldname" ]; then
      echo "Can't rename $oldname to $newname: file already exists."
    fi
  else
    echo mv "$oldname" "$newname"
    mv "$oldname" "$newname"
  fi
done

As I’m using KDE as my desktop, I like to have the timestamping integrated. Fortunately, it’s easy.

~/.kde/share/apps/konqueror/servicemenus

Actions=timestamp
ServiceTypes=all/all

[Desktop Action timestamp]
Exec=/usr/local/bin/timestamp %u
Icon=clock
Name=Timestamp

See Creating Konqueror Service Menus for a general introduction.

Update, 2007-02-06

I’ve updated the script to insert the timestamp before all the first extension and to also work with filename starting with a ‘.‘.

RSnapshotting on your own schedule

I’m using rsnapshot for making regular backups of my Linux box. Neither the computer, nor the external backup disk is running all the time, therefore it would not work to simply have cron run rsnapshot at fixed times.

I want a little more flexibility than that. When the computer is off, there’s no point in making a snapshot. Fine, there’s no work left to handle that case. But in cases where the computer is running, but the backup disk is not, I want it to be recorded that something should have been done–if only the disk had been on. Then, next time the disk is running again, the recorded (queued) tasks are executed in order.

As there are multiple processes involved, for enqueueing tasks and for dequeueing them, and as the later can take some time, there’s a bit of mutual exclusing involved. Luckily, Debian GNU/Linux provides very useful helper programs in the lockfile-progs package.

/usr/local/lib/snapshot/dequeue

#! /bin/sh

QUEUEFILE=/var/lib/snapshot/queue
LOCKFILE=/var/lib/snapshot/LCK..queue

lockfile-create $LOCKFILE
if ! test -f $QUEUEFILE || ! grep -Fqs -- "$*" $QUEUEFILE ; then
  echo $* >> $QUEUEFILE
fi
lockfile-remove $LOCKFILE

/usr/local/lib/snapshot/dequeue

#! /bin/sh

SNAPDIR=/var/cache/rsnapshot
QUEUEFILE=/var/lib/snapshot/queue
LOCKFILE=/var/lib/snapshot/LCK..queue
PIDFILE=/var/run/snapshot.pid

test -f $PIDFILE && exit 0
test -s $QUEUEFILE || exit 0

echo $$ > $PIDFILE

was_mounted=0
if egrep -qs "\\B$SNAPDIR\\b" /etc/mtab; then
  was_mounted=1
  mount -o remount,rw $SNAPDIR 2> /dev/null
else
  mount -o rw $SNAPDIR 2> /dev/null
fi

if [ $? != 0 ]; then
  rm -f $PIDFILE
  exit 0
fi

cleanup ()
{
  if [ $was_mounted == 1 ]; then
    mount -o remount,ro $SNAPDIR
  else
    umount $SNAPDIR
  fi
  rm -f $PIDFILE
}

trap "cleanup" EXIT TERM INT

while true; do
  lockfile-create $LOCKFILE
  if [ -f $QUEUEFILE ] ; then
    ARGS=`head -n1 $QUEUEFILE`
    sed -i '1d' $QUEUEFILE
  fi
  lockfile-remove $LOCKFILE

  if [ -n "$ARGS" ]; then
    nice rsnapshot $ARGS
  else
    break
  fi

done

exit 0

Things are tied together by several cron jobs that define when things should, if possible, happen. Of course, this is highly dependent on the individual rsnapshot configuration.

/etc/cron.d/snapshot

7   12,20       * * *   root    /usr/local/lib/snapshot/enqueue hourly
11  20          * * *   root    /usr/local/lib/snapshot/enqueue daily
13  20          * * 1   root    /usr/local/lib/snapshot/enqueue weekly
17  20          1 * *   root    /usr/local/lib/snapshot/enqueue monthly
*/5  *          * * *   root    /usr/local/lib/snapshot/dequeue