SQLite basics

Using SQLite

  • To open a SQLite database.
sqlite3 /path/to/database/file
  • Show the tables in a database
.tables
  • To Exit SQLite
.quit

PHP & SQLite

Connect to the database

try{
$dbHandle = new PDO('sqlite:/var/www/lighttpd/noc/emailserver_stat.sqlite');
}catch( PDOException $exception ){
echo "Can NOT connect to database";
die($exception->getMessage());
}

Create a table if it doesn’t exist

$sqlCreateTable = 'CREATE TABLE status (date date NOT NULL default \'0000-00-00\', item varchar(255) NOT NULL, value varchar(255) NOT NULL)';
$dbHandle->exec($sqlCreateTable);

List the conent of a table

$search_date = "2009-02-11";
$sqlGetView = 'SELECT * FROM status WHERE date = "'.$search_date.'"';
$result = $dbHandle->query($sqlGetView);
echo "<table border='1'>";
 while ($entry = $result->fetch()) {
    echo "<tr><td> " . $entry['value'] . "</td><td>" . $entry['item']. "</td></tr>";
 } echo "</table>";

Display a single value from a table

$search_date = "2009-02-11";
$sqlGetView = 'SELECT value FROM status WHERE item = \'connections\' AND date = "'.$search_date.'"';
$result = $dbHandle->query($sqlGetView);
$pageView = $result->fetch();
$connections = $pageView['value'];echo "$search_date
";
echo 'Number Of Connections: '.$connections.'
';
Advertisements

Postfix mySQL to SQLite export/import scripts

postfixdb-export.sh:

#/bin/bash
TMPDIR=/tmp/postfix_sqlite
DATABASENAME=postfix.sqlite
MYSQLDATABASE=postfix
MYSQLUSER=postfix
MYSQLPASS=postfix
HTTPDIR=/var/www/mail.mattrude.com/noc

if [ ! -e $TMPDIR ]; then
 mkdir -p $TMPDIR
 chmod a+w $TMPDIR
else
 chmod a+w $TMPDIR
fi

if [ ! -e $HTTPDIR ]; then
 mkdir -p $HTTPDIR
 rm -rf $TMPDIR/csv_md5check.md5
else
 if [ ! -e $HTTPDIR/$DATABASENAME.gpg ]; then
  rm -rf $TMPDIR/csv_md5check.md5
 fi
fi

rm -rf $TMPDIR/alias.csv $TMPDIR/domain.csv $TMPDIR/mailbox.csv
echo "SELECT * FROM alias
INTO OUTFILE '$TMPDIR/alias.csv'
FIELDS TERMINATED BY '|'
LINES TERMINATED BY '\n'" |mysql $MYSQLDATABASE

echo "SELECT * FROM domain
INTO OUTFILE '$TMPDIR/domain.csv'
FIELDS TERMINATED BY '|'
LINES TERMINATED BY '\n'" |mysql $MYSQLDATABASE

echo "SELECT * FROM mailbox
INTO OUTFILE '$TMPDIR/mailbox.csv'
FIELDS TERMINATED BY '|'
LINES TERMINATED BY '\n'" |mysql $MYSQLDATABASE

if [ -e $TMPDIR/csv_md5check.md5 ]; then
 MD5STATUS=`md5sum -c $TMPDIR/csv_md5check.md5 |grep -v ".csv: OK" |wc -l` 2>&1
else
 MD5STATUS=999
fi

if [ $MD5STATUS != '0' ]; then
 rm -rf $TMPDIR/$DATABASENAME $TMPDIR/$DATABASENAME.md5 $TMPDIR/schema.txt
 md5sum $TMPDIR/*.csv > $TMPDIR/csv_md5check.md5
 echo "Database $MYSQLDATABASE is being updated"
 echo "
 CREATE TABLE alias (
  address varchar(255) NOT NULL,
  goto text NOT NULL,
  domain varchar(255) NOT NULL,
  created datetime NOT NULL default '0000-00-00 00:00:00',
  modified datetime NOT NULL default '0000-00-00 00:00:00',
  active tinyint(1) NOT NULL default '1');

 CREATE TABLE domain (
  domain varchar(255) NOT NULL,
  description varchar(255) NOT NULL,
  aliases int(10) NOT NULL default '0',
  mailboxes int(10) NOT NULL default '0',
  maxquota bigint(20) NOT NULL default '0',
  quota bigint(20) NOT NULL default '0',
  transport varchar(255) NOT NULL,
  backupmx tinyint(1) NOT NULL default '0',
  created datetime NOT NULL default '0000-00-00 00:00:00',
  modified datetime NOT NULL default '0000-00-00 00:00:00',
  active tinyint(1) NOT NULL default '1' );

 CREATE TABLE mailbox (
  username varchar(255) NOT NULL,
  password varchar(255) NOT NULL,
  name varchar(255) NOT NULL,
  maildir varchar(255) NOT NULL,
  mailstore varchar(255) NOT NULL default 'mdbox:',
  quota bigint(20) NOT NULL default '0',
  domain varchar(255) NOT NULL,
  created datetime NOT NULL default '0000-00-00 00:00:00',
  modified datetime NOT NULL default '0000-00-00 00:00:00',
  quota_usage varchar(255),
  quota_usage_date varchar(255),
  active tinyint(1) NOT NULL default '1',
  local_part varchar(255) NOT NULL );
 " > $TMPDIR/schema.txt

 echo ".read $TMPDIR/schema.txt" |sqlite3 $TMPDIR/$DATABASENAME
 echo ".import $TMPDIR/alias.csv alias" |sqlite3 $TMPDIR/$DATABASENAME
 echo ".import $TMPDIR/domain.csv domain" |sqlite3 $TMPDIR/$DATABASENAME
 echo ".import $TMPDIR/mailbox.csv mailbox" |sqlite3 $TMPDIR/$DATABASENAME

 rm -rf $HTTPDIR/$DATABASENAME $HTTPDIR/$DATABASENAME.gpg $HTTPDIR/$DATABASENAME.sig
 cp $TMPDIR/$DATABASENAME $HTTPDIR/$DATABASENAME
 cd $HTTPDIR/
 gpg -b $DATABASENAME
 gpg -se -r E2E70CD8 -r D2FAA865 -r 6503BE11 $DATABASENAME
 rm -f $HTTPDIR/$DATABASENAME
 #md5sum $DATABASENAME > $DATABASENAME.md5
 #bzip2 $DATABASENAME
 #ssh odin.mattrude.com /home/matt/bin/sbin/postfix_sqlite_import.sh
fi

postfixdb-import.sh:

#/bin/bash
DBNAME=postfix.sqlite
DBDIR=/etc/postfix/database
URL=https://mail.mattrude.com/noc/postfix.sqlite
TMPDIR=/tmp/$DBNAME
LOGDIR=$TMPDIR

if [ ! -e $DBDIR ]; then
 mkdir -p $DBDIR
 chmod 700 $DBDIR
 chown 0:0 $DBDIR
fi

if [ ! -e $TMPDIR ]; then
 mkdir -p $TMPDIR
fi

if [ ! -e $LOGDIR ]; then
 mkdir -p $LOGDIR
fi

rm -rf $LOGDIR/postfix_import*.log $TMPDIR/$DBNAME.sig $DBDIR/$DBNAME.sig
wget --no-check-certificate -P $DBDIR $URL.sig > $LOGDIR/postfix_import_sig_download.log  2>&1
SIGDOWNLOAD=$?

if [ $SIGDOWNLOAD != 0 ]; then
 echo "Did not download file $URL.sig; The host may be down or too busy."
 echo "Here is the download log:"
 cat $LOGDIR/postfix_import_sig_download.log
 echo ""
 echo "exiting script"
 exit 1
fi

cd $DBDIR
gpg --verify $DBNAME.sig > /dev/null 2>&1
MD5STATUS=`echo $?`
if [ $MD5STATUS != '0' ]; then
 echo "Update needed, downloading updated $DBNAME."
 cd $TMPDIR
 rm -f $TMPDIR/$DBNAME.sig
 mv $DBDIR/$DBNAME.sig $TMPDIR/$DBNAME.sig
 rm -rf $TMPDIR/$DBNAME.gpg
 wget --no-check-certificate -P $TMPDIR $URL.gpg > $LOGDIR/postfix_import_download.log 2>&1
 if [ -e $TMPDIR/$DBNAME.gpg ]; then
  cd $TMPDIR
  gpg $TMPDIR/$DBNAME.gpg >> $LOGDIR/postfix_import_download.log 2>&1
  echo "sig test 2"
  ls -lh
  gpg --verify $DBNAME.sig > /dev/null 2>&1
  SIGTEST=`echo $?`
  if [ $SIGTEST == '0' ]; then
   cd $DBDIR
   echo "Database file $DBNAME.gpg was successfully downloaded"
   ARCHIVEDATE=`date +%Y%m%d_%H%M`
   mv $DBDIR/$DBNAME $DBDIR/$DBNAME.$ARCHIVEDATE
   mv $TMPDIR/$DBNAME $DBDIR/$DBNAME
   bzip2 $DBDIR/$DBNAME.$ARCHIVEDATE
   chown 0:0 $DBDIR/$DBNAME
   chmod 666 $DBDIR/$DBNAME
   if [ -e $DBDIR/$DBNAME ]; then
    cd $DBDIR
    cp $TMPDIR/$DBNAME.sig $DBDIR/$DBNAME.sig
    MD5TEST2=`gpg --verify $DBNAME.sig |grep -v "gpg: Good signature" |wc -l`
    if [ $MD5TEST2 = '0' ]; then
     echo "$DBNAME was updated successfully!"
     rm -rf $DBDIR/$DBNAME.sig
    else
     echo "$DBNAME was downloaded successfully but the downloaded file dose not match the signature file."
     exit 1
    fi
   else
    echo "$DBNAME was not copied to the database location ($DBDIR)."
    exit 1
   fi
  else
   echo "Database $DBNAME was not updated, the signature check failed."
   echo "Here's the Signature Check Log:"
   cd $TMPDIR
   gpg --verify $DBNAME.sig
   rm -rf $TMPDIR/$DBNAME*
  fi
 else
  echo "Database $DBNAME was not updated, file was not downloaded."
  echo ""
  echo "Here's the download log:"
  cat $LOGDIR/postfix_import_download.log
  rm -rf $TMPDIR/$DBNAME*
  rm -rf $DBDIR/$DBNAME.sig
 fi
fi
rm -rf $TMPDIR/$DBNAME.sig $DBDIR/$DBNAME.sig

WordPress Auto Backup Script

A quick backup script for WordPress. If you pass a config file containing the block of variables, the script will auto run for your install of WordPress.

# /bin/bash
BKNAME=
DIR=
DBHOST=
DBNAME=
DBUSER=
DBPASS=

# Backup Varibles
BKDIR=

# Restore Variables
RSDIR=
RSARDIR=
RSUSER=
RSGROUP=

OFFSITE=

# Offsite Rsync
RSYNCHOST=
RSYNCUSERNAME=
RSYNCPASSWORD=

# Offiste SCP
SCPUSER=
SCPHOST=
SCPDIR=

if [ `date +%e` =  1 ]; then
 DAY=`date +%b`
 DATE=$DAY.`date +%Y-%m-%d`
else
 DAY=`date +%w`
 DATE=$DAY.`date +%Y-%m-%d`
fi

if [ -e $1 ]; then
 source $1
 CONFIGSTATUS="Using config file found at $1"
else
 echo "No Config file found"
 exit
fi

if [ ! -e $DB_HOST ]; then
  DBHOST=`grep "DB_HOST" $DIR/wp-config.php |sed "s/define('DB_HOST', '//" |sed "s/');//"`
  DBNAME=`grep "DB_NAME" $DIR/wp-config.php |sed "s/define('DB_NAME', '//" |sed "s/');//"`
  DBUSER=`grep "DB_USER" $DIR/wp-config.php |sed "s/define('DB_USER', '//" |sed "s/');//"`
  DBPASS=`grep "DB_PASS" $DIR/wp-config.php |sed "s/define('DB_PASSWORD', '//" |sed "s/');//"`
fi
BKDIRNAME=$BKNAME

if [ `date +%e` = 1 ]; then
 ARCHIVE=1
else
 if [ `date +%w` = 0 ]; then
  ARCHIVE=1
 else
  ARCHIVE=0
 fi
fi

if [ ! -e $BKDIR ];then
 mkdir -p $BKDIR
fi

chown -R root:root $DIR/
chown -R apache:apache $DIR/wp-content $DIR/wp-admin/update.php
chown apache:apache $DIR

if [ -e $DIR/sitemap.xml ]; then
  chown apache:apache $DIR/sitemap.xml
fi

if [ -e $DIR/sitemap.xml.gz ]; then
  chown apache:apache $DIR/sitemap.xml.gz
fi
case "$2" in
restore)
  rm -rf $RSARDIR/$BKNAME.$DAY.*
  rm -rf $RSDIR/$BKNAME
  rm -rf $DIR/$BKNAME.$DAY.*
  rm -rf $DIR/$DBNAME.*
  if [ ! -e $RSARDIR ]; then
   mkdir -p $RSARDIR
  fi
  export RSYNC_PASSWORD=$RSYNCPASSWORD
  rsync -rvzht --delete --stats $RSYNCUSERNAME@$RSYNCHOST::ibackup/odin/$BKNAME/$BKNAME.$DAY.* $RSARDIR/ --port=45873 >> $DIR/$BACKUPNAME.log 2>&1
  cd $RSARDIR
  md5sum -c $BKNAME.$DATE.tgz.md5 > /dev/null 2>&1
  RESTOREMD5=`echo $(($?))`
  if [ $RESTOREMD5 = 0 ]; then
   cd $RSDIR/
   cp $RSARDIR/$BKNAME.$DATE.tgz $RSDIR/$BKNAME.$DATE.tgz
   tar -xzf $RSDIR/$BKNAME.$DATE.tgz
   mv $RSDIR$DIR $RSDIR
   cd $RSDIR/$BKNAME
   md5sum -c $DBNAME.$DATE.sql.md5 > /dev/null 2>&1
   RESTORESQLMD5=`echo $(($?))`
   if [ $RESTORESQLMD5 = 0 ]; then
    cd $RSDIR
    mysql -u $DBUSER -p$DBPASS $DBNAME > $DIR/$BKNAME.$DATE.log 2>&1
  done
  mysqldump -h $DBHOST -u $DBUSER -p$DBPASS $DBNAME > $DIR/$DBNAME.$DATE.sql 2> $DIR/$BKNAME.$DATE.log
  a=$?
  mysqldump -h $DBHOST -u $DBUSER -p$DBPASS $DBNAME --xml > $DIR/$DBNAME.$DATE.xml 2> $DIR/$BKNAME.$DATE.log
  a2=$?
  cd $DIR/
  md5sum $DBNAME.$DATE.sql $DBNAME.$DATE.xml > $DBNAME.$DATE.sql.md5 2>> $DIR/$BKNAME.$DATE.log
  b=$?
  SQLERROR=`echo $(($a+$a2+$b))`
  if [ $SQLERROR != 0 ]; then
   BKNAME=$BKNAME-NOSQL
   echo "Exiting on SQL error"
   echo "DBHOST $DBHOST"
   echo "DBNAME $DBNAME"
   echo "DBUSER $DBUSER"
   echo "DBPASS $DBPASS"
   exit 1
  fi
  tar -czf $BKDIR/$BKNAME.$DATE.tgz --totals $DIR >> $DIR/$BKNAME.$DATE.log 2>&1
  c=$?
  cd $BKDIR/
  md5sum $BKNAME.$DATE.tgz > $BKNAME.$DATE.tgz.md5 2>> $DIR/$BKNAME.$DATE.log
  d=$?
  TOTAL=`ls -lh $BKDIR/$BKNAME.$DATE.tgz |awk '{ print $5 }'`
  ERROR=`echo $(($a+$a2+$b+$c+$d))`
  if [ $ERROR = 0 ]; then
   STATUS=Good
  else
   STATUS=Failed
  fi

  echo "" >> $DIR/$BKNAME.$DATE.log
  echo "Starting GPG encryption" >> $DIR/$BKNAME.$DATE.log
  for a in $GPGKEY
  do
   GPGKEYD="$GPGKEYD -r $a"
  done
  #gpg -e -r $GPGKEY $BKDIR/$BKNAME.$DATE.tgz
  echo "" >> $DIR/$BKNAME.$DATE.log
  if [ $ARCHIVE = '0' ]; then
   case "$OFFSITE" in
    rsync|RSYNC)
      export RSYNC_PASSWORD=$RSYNCPASSWORD
      rsync -rvzht --delete --stats $BKDIR/ $RSYNCUSERNAME@$RSYNCHOST::ibackup/odin/$BKDIRNAME --port=45873 --exclude=Archive/ >> $DIR/$BACKUPNAME.log 2>&1
    ;;

    scp|SCP)
      echo "" >> $DIR/$BKNAME.$DATE.log
      echo "Starting SCP Transmition" >> $DIR/$BKNAME.$DATE.log
      ssh $SCPUSER@$SCPHOST "mkdir -p $SCPDIR"
      ssh $SCPUSER@$SCPHOST "rm -f $SCPDIR/$BKNAME.$DAY.*"
      scp -q $BKDIR/$BKNAME.$DATE.tgz $SCPUSER@$SCPHOST:$SCPDIR/ 2>> $DIR/$BKNAME.$DATE.log
      scp -q $BKDIR/$BKNAME.$DATE.tgz.md5 $SCPUSER@$SCPHOST:$SCPDIR/ 2>> $DIR/$BKNAME.$DATE.log
      #ssh $SCPUSER@$SCPHOST "chmod 666 $SCPDIR/$BKNAME.$DAY.*"
      echo "" >> $DIR/$BKNAME.$DATE.log
    ;;
   esac
  fi

  echo "final error status is: $ERROR" >> $DIR/$BKNAME.$DATE.log
  sed '/tar: Removing leading */d' $DIR/$BKNAME.$DATE.log > $BKDIR/backuptmp.log
  #echo "INSERT INTO Backup_Log ( System, Backup_Job, Label, Output, Bytes, Status, Log ) VALUES ('`hostname -s`', 'Wiki', '$DIR', '$BKNAME.$DATE', '$TOTAL', '$STATUS', '`cat $BKDIR/backuptmp.log`');" |mysql -t -h localhost -u backup Status
  rm -rf $BKDIR/backuptmp.log
  rm -rf $DIR/$DBNAME.$DAY.*
  rm -rf $DIR/$BKNAME.$DAY.*
  ;;
*)
  echo "The valid commands are backup & restore"
  exit 1
  ;;
esac

Converting a MediaWiki database from MySQL to SQLite

So the plane here is to convert a fully working MediaWiki install running on MySQL to run on SQLite instead.  To do this you will need to install a 2nd MediaWIKI install on a test or development system.  Once you are done you can move your new MediaWiki install to where every you would like.

Backup up your Old MediaWiki Installation

To start, from within your working MediaWiki install, first back up your data.

php maintenance/dumpBackup.php --full --uploads > wiki-backup.xml

This will create a file named wiki-backup.xml in your MediaWiki’s root directory, copy that file to a safe place.  We aren’t going to touch the MySQL database until were done, but it’s always a good idea to have backups safe and sound in case you need them.

The backup script run above dose not backup your MediaWiki’s uploaded images and other files.  These files are stored in your ‘images’ folder in the root of MediaWiki’s directory.  You need to back those up also.

tar -czf wiki-images.tgz images/

You should now have everything you need from your old MediaWiki install. Next you will need to install MediaWiki in a new location on your sever (or a development server).

Installing your new SQLite MediaWiki site

You should download and install the newest version of MediaWiki.  I always you the development trunk since this is what’s used on Wikipedia.

During the install process you will be asked what database you would like to use, you much choose ‘SQLite’ since this is the point of reinstalling MediaWiki.  Bring your new install all the way so you have a new install running on your server.  MediaWiki will create a default home page for you and you should be able to modify that page.  If you are unable to get MediaWiki installed or if you have a problem modifying the Main Page after the install, please see the MediaWiki mailing lists or the FAQ for assistance.

Restoring your Data on your new SQLite site

After you have your new SQLite version of MediaWiki installed and working, you need to restore your data.  The database part of this is pretty strate forward.

Start by copying the xml file you created in the first step to your new MediaWiki install.  Then run the following:

php maintenance/importDump.php wiki-backup.xml

Depending on the amount of pages you have, this may take quite some time to process. Once this is done all your pages should be on your new install (expect the Main Page, you will need to copy that manually).
To restore you images and other uploaded files, first you need to extract the tarball you made earlier to temporary location.

mkdir temporary
cp wiki-images.tgz temporary
cd temporary/
tar -xzf wiki-images.tgz

This will create a bunch of folders in the temporary directory, you need to copy everything in those folders into a single folder. The name of the folder doesn’t matter, I’m using tempimages, but you may use what you would like.

cd ../
mkdir tempimages
cp temporary/images/*/*/* tempimages

Now that you have everything in a single folder, import the content of that folder into your new MediaWiki install.

php maintenance/importImages.php tempimages/

And that should do it, you should now have a fully working MediaWiki install using SQLite.

Installing Dovecot with SQLite Support

Following in line with my previous post on Installing Postfix with SQL Support. This post will describe installing Dovecot from source with full SQLite support.

Installing from Source

First start out by downloading the lastest version from Dovecot’s website (the current version as of the writing of the how-to is 1.2.8).

yum -y install sqlite sqlite-devel gcc make patch db4-devel cyrus-sasl-devel

Next download and untar the source code.

wget http://dovecot.org/releases/1.2/dovecot-1.2.8.tar.gz
tar -xzf dovecot-1.2.8.tar.gz
cd dovecot-1.2.8/

Next, you will need to configure the code before compiling.

./configure --with-sqlite
echo $?

Assuming the configure command finishes with out error (the last line should be a “0”). Compile and install Dovecot.

make && make install

Configuring Dovecot for SQLite

First we need to create or modify the dovecot config file for SQLite access.  If you are currenly using MySQL with Dovecot, switching to SQLite is pretty easy and strate forward.  Or you may just use the below dovecot config file.

### Dovecot configuration file ###
### /etc/dovecot.conf ###
protocols = pop3 imap
login_user = postfix
auth_cache_size = 128
auth_cache_ttl = 600
mail_debug = yes

mail_location = maildir:%h/

protocol imap {
 listen = *:143
}

protocol lda {
  postmaster_address = postmaster@mattrude.com
  hostname = odin.mattrude.com
  mail_plugin_dir = /usr/local/lib/dovecot/lda
  auth_socket_path = /var/run/dovecot/auth-master
}

auth default {
  mechanisms = plain login
  userdb sql {
    args = /etc/dovecot-sqlite.conf
  }
  passdb sql {
    args = /etc/dovecot-sqlite.conf
  }
  socket listen {
    master {
      path = /var/run/dovecot/auth-master
      user = virtualmail
      group = virtualmail
    }
    client {
      path = /var/spool/postfix/private/auth
      mode = 0660
      user = postfix
      group = postfix
    }
  }
}

After you have created the main Dovecot config file, you will need to add the SQLite config file (below).

### /etc/dovecot-sqlite.conf ###
driver = sqlite
connect = /etc/postfix/postfix.sqlite
password_query = SELECT password, username AS user
  FROM mailbox WHERE username = '%u' AND domain = '%d'
user_query = SELECT maildir, 1000 AS uid, 1000 AS gid FROM mailbox WHERE
  username = '%u' AND domain = '%d' AND active = '1'

After the config files have been created, we need to create the database file, here is where you will need SQLite installed on the system.

Building the SQLite Database

In order to use the SQLite function, you need a SQLite database. First using SQLite3 run

sqlite3 /etc/postfix/postfix.sqlite

To create the database, then you can copy and past the following scheme into the new database.

CREATE TABLE alias (
  address varchar(255) NOT NULL,
  goto text NOT NULL,
  domain varchar(255) NOT NULL,
  created datetime NOT NULL default '0000-00-00 00:00:00',
  modified datetime NOT NULL default '0000-00-00 00:00:00',
  active tinyint(1) NOT NULL default '1');

CREATE TABLE domain (
  domain varchar(255) NOT NULL,
  description varchar(255) NOT NULL,
  aliases int(10) NOT NULL default '0',
  mailboxes int(10) NOT NULL default '0',
  maxquota bigint(20) NOT NULL default '0',
  quota bigint(20) NOT NULL default '0',
  transport varchar(255) NOT NULL,
  backupmx tinyint(1) NOT NULL default '0',
  created datetime NOT NULL default '0000-00-00 00:00:00',
  modified datetime NOT NULL default '0000-00-00 00:00:00',
  active tinyint(1) NOT NULL default '1' );

CREATE TABLE mailbox (
  username varchar(255) NOT NULL,
  password varchar(255) NOT NULL,
  name varchar(255) NOT NULL,
  maildir varchar(255) NOT NULL,
  quota bigint(20) NOT NULL default '0',
  domain varchar(255) NOT NULL,
  created datetime NOT NULL default '0000-00-00 00:00:00',
  modified datetime NOT NULL default '0000-00-00 00:00:00',
  active tinyint(1) NOT NULL default '1',
  local_part varchar(255) NOT NULL );

Then close the database

.quit

Or you may download mine from below and use the same scheme work.

mkdir /var/run/dovecot

Dovecot INIT file

#!/bin/bash
#
#	/etc/rc.d/init.d/dovecot
#
# Starts the dovecot daemon
#
# chkconfig: - 65 35
# description: Dovecot Imap Server
# processname: dovecot
# Source function library.
. /etc/init.d/functions

test -x /usr/local/sbin/dovecot || exit 0

RETVAL=0
prog="Dovecot Imap"

start() {
       echo -n $"Starting $prog: "
	daemon /usr/local/sbin/dovecot
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/dovecot
	echo
}

stop() {
	echo -n $"Stopping $prog: "
	killproc /usr/local/sbin/dovecot
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/dovecot
	echo
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  reload|restart)
	stop
	start
	RETVAL=$?
	;;
  condrestart)
	if [ -f /var/lock/subsys/dovecot ]; then
	    stop
	    start
	fi
	;;
  status)
	status /usr/local/sbin/dovecot
	RETVAL=$?
	;;
  *)
	echo $"Usage: $0 {condrestart|start|stop|restart|reload|status}"
	exit 1
esacexit $RETVAL

Installing Postfix with SQLite Support

This How-to will show you how to set up Postfix with SQLite support on a Fedora 11 system.  This will require us to compile Postfix from scratch.

Installing the dependences

yum -y install sqlite sqlite-devel gcc make patch db4-devel cyrus-sasl-devel
echo "postfix:x:89:89::/var/spool/postfix:/sbin/nologin" >> /etc/passwd
echo "postdrop:x:90:90::/var/spool/postfix:/sbin/nologin" >> /etc/passwd
echo "postfix:x:89:" >> /etc/group
echo "postdrop:x:90:" >> /etc/group
ln -s /usr/lib/sasl2/ /usr/local/lib/sasl2

Download and Patch Postfix

Download and unpack the current Postfix Version.

wget http://postfix.energybeam.com/source/official/postfix-2.6.5.tar.gz
tar -xzf postfix-2.6.5.tar.gz
cd postfix-2.6.5

Download and Patch Postfix with the SQLite Postfix patch

wget http://www.treibsand.com/postfix-sqlite/postfix-2.6-20080216_sqlite.patch
patch -ul -d . -p1 < postfix-2.6-20080216_sqlite.patch
echo $?

Building Postfix

To Build with SQLite Support

make -f Makefile.init makefiles 'CCARGS=-DHAS_SQLITE -I/usr/local/include'
'AUXLIBS=-L/usr/local/lib -lsqlite3'
echo $?

Or to Build with SQLite & TLS Support
Requires:

yum -y install openssl-devel
make -f Makefile.init makefiles 'CCARGS=-DHAS_SQLITE -I/usr/local/include -DUSE_TLS'
'AUXLIBS=-L/usr/local/lib -lsqlite3 -lz -lm -lssl -lcrypto'
echo $?

To Build with SQLite, Dovecot, & TLS Support

yum -y install openssl-devel dovecot-devel
make makefiles 'CCARGS=-DHAS_SQLITE -I/usr/include/sasl/ -DUSE_SASL_AUTH -DUSE_CYRUS_SASL  -DDEF_SERVER_SASL_TYPE="dovecot" -DUSE_TLS' -I/usr/local/include
   'AUXLIBS=-L/usr/local/lib -lsqlite3 -lz -lm -lssl -lcrypto -lsasl2'
echo $?

Compiling Postfix

Now Compile Postfix

make
echo $?

And Install it

make install

Building the SQLite Database

In order to use the SQLite function, you need a SQLite database. First using SQLite3 run

sqlite3 /etc/postfix/postfix.sqlite

To create the database, then you can copy and past the following scheme into the new database.

CREATE TABLE alias (
  address varchar(255) NOT NULL,
  goto text NOT NULL,
  domain varchar(255) NOT NULL,
  created datetime NOT NULL default '0000-00-00 00:00:00',
  modified datetime NOT NULL default '0000-00-00 00:00:00',
  active tinyint(1) NOT NULL default '1');

CREATE TABLE domain (
  domain varchar(255) NOT NULL,
  description varchar(255) NOT NULL,
  aliases int(10) NOT NULL default '0',
  mailboxes int(10) NOT NULL default '0',
  maxquota bigint(20) NOT NULL default '0',
  quota bigint(20) NOT NULL default '0',
  transport varchar(255) NOT NULL,
  backupmx tinyint(1) NOT NULL default '0',
  created datetime NOT NULL default '0000-00-00 00:00:00',
  modified datetime NOT NULL default '0000-00-00 00:00:00',
  active tinyint(1) NOT NULL default '1' );

CREATE TABLE mailbox (
  username varchar(255) NOT NULL,
  password varchar(255) NOT NULL,
  name varchar(255) NOT NULL,
  maildir varchar(255) NOT NULL,
  quota bigint(20) NOT NULL default '0',
  domain varchar(255) NOT NULL,
  created datetime NOT NULL default '0000-00-00 00:00:00',
  modified datetime NOT NULL default '0000-00-00 00:00:00',
  active tinyint(1) NOT NULL default '1',
  local_part varchar(255) NOT NULL );

Then close the database

.quit

Or you may download mine from below and use the same scheme work.

Once you have your database file place it in /etc/postfix/ as something like /etc/postfix/postfix.sqlite then run

chown root:root /etc/postfix/postfix.sqlite
chmod 600 /etc/postfix/postfix.sqlite

Configuring Postfix

Now add the maps to you config.

/etc/postfix/main.cf

relay_domains = sqlite:/etc/postfix/sqlite_relay_domains_maps.cf
relay_recipient_maps = sqlite:/etc/postfix/sqlite_relay_recipient_maps.cf
virtual_alias_maps = sqlite:/etc/postfix/sqlite_virtual_alias_maps.cf
virtual_mailbox_domains = sqlite:/etc/postfix/sqlite_virtual_domains_maps.cf
virtual_mailbox_maps = sqlite:/etc/postfix/sqlite_virtual_mailbox_maps.cfvirtual_mailbox_base = /var/spool/virtualmailboxes
virtual_minimum_uid= 1000
virtual_uid_maps = static:1000
virtual_gid_maps = static:1000

sqlite_relay_domains_maps.cf

dbpath = /etc/postfix/postfix.sqlite
query = SELECT domain FROM domain WHERE domain='%s' AND backupmx = '1' AND active = '1'

sqlite_relay_recipient_maps.cf

dbpath = /etc/postfix/postfix.sqlite
query = SELECT goto FROM alias WHERE address='%s' AND active = 1

sqlite_virtual_alias_maps.cf

dbpath = /etc/postfix/postfix.sqlite
query = SELECT goto FROM alias WHERE address='%s' AND active = '1'

sqlite_virtual_domains_maps.cf

dbpath = /etc/postfix/postfix.sqlite
query = SELECT domain FROM domain WHERE domain='%s' AND backupmx = '0' AND active = '1'

sqlite_virtual_mailbox_maps.cf

dbpath = /etc/postfix/postfix.sqlite
query = SELECT maildir FROM mailbox WHERE username='%s' AND active = '1'

Setting up a fresh install of Postfix

mkdir /var/spool/virtualmailboxes/
echo "virtualmail:x:1000:1000::/var/spool/virtualmailboxes:/sbin/nologin" >> /etc/passwd
echo "virtualmail:x:1000:" >> /etc/group
chmod 700 /var/spool/virtualmailboxes/
chown -R virtualmail:virtualmail /var/spool/virtualmailboxes/
rm -f /usr/lib/sendmail
ln -s /usr/sbin/sendmail /usr/lib/sendmail

Adding SQLite entry’s

First add a Domain

echo "INSERT INTO domain ( domain, description, transport )
VALUES ( 'laptop.mattrude.com', 'laptops domain', 'virtual' );" |sqlite3 /etc/postfix/postfix.sqlite

Then add a user

echo "INSERT INTO mailbox ( username, password, name, maildir, domain, local_part )
VALUES ( 'matt@laptop.mattrude.com', 'password', 'Matt', 'laptop.mattrude.com/matt@laptop.mattrude.com/', 'laptop.mattrude.com', 'matt' );" |sqlite3 /etc/postfix/postfix.sqlite

Last we need to add the mailboxes alias

echo "INSERT INTO alias ( address, goto, domain )
VALUES ( 'matt@laptop.mattrude.com', 'matt@laptop.mattrude.com', 'laptop.mattrude.com' );" |sqlite3 /etc/postfix/postfix.sqlite

Next, contue on to my Dovecot SQLite how-to to finish your email server.