Author Archives: Joe

Remote filesystems and mlocate

Have you ever worked in a clustered environment that provided remote home directories?  Furthermore, have you ever been annoyed by the fact mlocate does not descend into these remote file systems by default?  Me too.  I’ve written two small BASH scripts to address this problem.

updatedb_extern

#!/bin/bash

# Edit DEST to point to a local directory you have write access to
DEST=/path/to/local/database/directory

#Don't edit below this line (no point)
EXTERN=( "$@" )
DATABASES=()

#Did we receive any paths to process?
if [ -z "$EXTERN" ] ; then
	echo "No path(s) specified."
	exit 1
fi

#Simple adapation logic to ensure we will have a writable data area
if [ ! -d "$DEST" ] ; then
	mkdir -p $DEST 2>/dev/null
	if [ $? != 0 ] ; then
		DEST=/var/tmp/mlocate
		mkdir -p $DEST 2>/dev/null
		if [ $? != 0 ] ; then
			echo "No suitable path to store locate database."
			exit $?
		fi
	fi
fi

#Generate database names based on external path
#Example: /home/myuser becomes _home_myuser.db
for path in "${EXTERN[@]}"
do
	database=$(echo $path | sed -e "s|/|_|g")
	DATABASES[${#DATABASES[*]}]="$DEST/${database}.db"
done

#For each external path generate an mlocate database
MAX=${#EXTERN[@]}
for (( i=0; i<$MAX; i++))
do
	i_fake=$(( i + 1 ))
	path=${EXTERN[$i]}
	dbpath=${DATABASES[$i]}
	echo "[$(echo $(( $i_fake * 100 / $MAX )))%] $path -> $dbpath"
	updatedb -l 0 -o $dbpath -U $path
done
echo

The developers of mlocate were nice enough to implement the environment variable, LOCATE_PATH, to extend the database search path.  We’re going to use it to our advantage with the following script.

updatedb_extern_setup

#!/bin/bash

DEST=$1
if [ -z "$DEST" ] ; then
	echo "No database path specified."
	exit 1
fi

for db in $DEST/*.db
do
	DELIM=":"
	if [ -z "$LOCATE_PATH" ] ; then
		DELIM=
	fi

	LOCATE_PATH=${LOCATE_PATH}${DELIM}${db}
done
echo "$LOCATE_PATH"

 

Example Usage

In your ~/.bashrc or ~/.bash_profile (or ~/.profile):

export LOCATE_PATH=`update_extern_setup /path/to/local/database/directory`

In your crontab (e.g. crontab -e):

* 2 * * * updatedb_extern /home/username /some/remote/path

Manually:
updatedb_extern /home/username /some/remote/path