Linux.com

find /var/www/ -type f >> FullFileList.txt

Link to this post 14 Aug 10

It is good to see that your filesystem finally stabilized. Below are my scripts to setup the schema and tables, the another script to verify the files and place them into th database, it includes a verification mechanism so you can use it for updating the index.

As with anything else, this is my first attempt at this script and there may be better ways, but we have to start somewhere.

Create schema and tables

#!/bin/bash
#
# This script creates the mysql schema and tables to file indexing

USER="root"
PW="password"

DBNAME="fileindex"

mysql -u $USER --password=$PW --execute="create database $DBNAME;"
mysql -u $USER --password=$PW --database=$DBNAME --execute="create table files (file_id int not null auto_increment primary key, dir_id int, filename varchar(100),verified bool);"
mysql -u $USER --password=$PW --database=$DBNAME --execute="create table dirs (dir_id int not null auto_increment primary key, dirname varchar(100),verified bool);"

Write the files and directories into the database

#!/bin/bash

# Index all files and directories from a directory into mysql

USR="user"
PW="password"
DBNAME="fileindex"
BASELOC="/var/www"


# Mark all entries as unverified so the missing files will be tagged
CMD="UPDATE dirs SET verified=false"
mysql -u $USR --password=$PW --database=$DBNAME --execute="$CMD"
CMD="UPDATE files SET verified=false"
mysql -u $USR --password=$PW --database=$DBNAME --execute="$CMD"

# Start storing the directories
for LOC in $(find $BASELOC -type d)
do
CMD="SELECT dir_id from dirs where dirname='$LOC'"
OUT=`mysql -u $USR --password=$PW --database=$DBNAME --execute="$CMD"`
if [ -z "$OUT" ]; then
CMD="INSERT INTO dirs (dirname,verified) VALUES('$LOC',true)"
else
CMD="UPDATE dirs SET verified=true WHERE dirname='$LOC'"
fi
mysql -u $USR --password=$PW --database=$DBNAME --execute="$CMD"
done

# Start storing the files
for LOC in $(find $BASELOC -type f)
do
DNAME=`dirname "$LOC"`
LNAME=`basename "$LOC"`

CMD="SELECT dir_id from dirs where dirname='$DNAME'"
DNUM=`mysql -u $USR --password=$PW --database=$DBNAME --execute="$CMD"`
DNUM=`echo "$DNUM"|grep -v dir_id`

CMD="SELECT file_id from files where filename='$LOC' and dir_id='$DNUM"
OUT=`mysql -u $USR --password=$PW --database=$DBNAME --execute="$CMD"`
if [ -z "$OUT" ]; then
CMD="INSERT INTO files (dir_id,filename,verified) VALUES('$DNUM','$LNAME',true)"
else
CMD="UPDATE files SET verified=true WHERE filename='$LNAME' and dir_id='$DNUM'"
fi
mysql -u $USR --password=$PW --database=$DBNAME --execute="$CMD"
done

# Remove the missing files and directories
CMD="DELETE dirs.* from dirs where verified=false"
mysql -u $USR --password=$PW --database=$DBNAME --execute="$CMD"
CMD="DELETE files.* from files where verified=false"
mysql -u $USR --password=$PW --database=$DBNAME --execute="$CMD"

Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board