Database Hooks

database_create_(pre|post).sh

This script is called before/after a database is created.

Environment variables

  • username: DA user, owner of database
  • database: database name
  • user: database user
  • passwd: database user password
  • login_as(0|1): set to 1 if a Reseller is using the "login as" feature
  • system_password: password of the logged in user

database_delete_(pre|post).sh

This script is called before/after a MySQL database is deleted.

Environment variables

  • username: owner of database
  • database: database name

database_destroy_user_post.sh

The hook script called when deleting a Database User.

Note that database_destroy_user_post.sh will not be called when a database and its users are being deleted.

It's only called when a single DB user is removed from a database.

This means that any code used in database_user_create_post.sh will have to be doubled in database_delete_post.sh.

Environment variables

  • username: DA username
  • database: name of the db
  • user: name of the user created

database_user_create_post.sh

The hook script called after a database user is created. This script would not be called when a database and its user is being created.

Environment variables

  • username: DA user. owner of database
  • database: database name
  • user: database user
  • passwd: database user password

database_user_password_change_(pre|post).sh

These are custom pre/post scripts that are called if a DB User's password is set.

Environment variables

  • username: DirectAdmin user/owner of the database
  • name: database name
  • user: database user
  • passwd: new user password

Examples

Create an sql backup prior to database deletion

One could use the database_delete_pre.sh to create an sql backup prior to database deletion to prevent loss of data due to accidental deletion. Note, though, that you would likely want to implement a way to purge these stored sql files every so often via cron to prevent them from accumulating and filling your disk space.

!#/bin/bash

DIR="/home/$username/deleted_dbs"

if [ ! -d $DIR ] ; then 
  mkdir -p $DIR
fi

mysqldump --defaults-extra-file=/usr/local/directadmin/conf/my.cnf $database > $DIR/$database.sql

retval=$?
if [ $retval -eq 0 ]; then
 exit 0;
else 
 exit 1;
fi
Last Updated: