Topic: MySQL User Creation Script Update
Hi all,
I have updated this script to match the standard install dirs ad mail folders
and also have updated the sql to match the database tables and values inserted when creating an account via iredadmin.
hope this is useful to someone.
cheers Kris
Create_mail_user_MySQL.sh
##########################################################
# --------- CHANGE THESE VALUES ----------
# Storage base directory used to store users' mail.
STORAGE_BASE_DIRECTORY="/var/vmail"
# STORAGE_NODE has been added as it sits in the database in a slightly different column
# (standard mail folder is still /var/vmail/vmail1/)
STORAGE_NODE="vmail1"
# Password setting.
# Note: password will be crypted in MD5.
DEFAULT_PASSWD='password'
USE_DEFAULT_PASSWD='YES' # If set to 'NO', password is the same as username.
# Default mail quota.
DEFAULT_QUOTA='100' # 100 -> 100M
# -------------- You may not need to change variables below -------------------
# Mailbox format: mbox, Maildir.
MAILBOX_FORMAT='Maildir'
# ---- Maildir settings ----
# Maildir style: hashed, normal.
# Hashed maildir style, so that there won't be many large directories
# in your mail storage file system. Better performance in large scale
# deployment.
# Format: e.g. username@domain.td
# hashed -> domain.ltd/u/us/use/username/
# normal -> domain.ltd/username/
# Default hash level is 3.
MAILDIR_STYLE='hashed' # hashed, normal.
# Time stamp, will be appended in maildir.
DATE="$(date +%Y.%m.%d.%H.%M.%S)"
# Path to SQL template file.
SQL="output.sql"
echo '' > ${SQL}
# Cyrpt the password.
if [ X"${USE_DEFAULT_PASSWD}" == X"YES" ]; then
export CRYPT_PASSWD="$(openssl passwd -1 ${DEFAULT_PASSWD})"
else
:
fi
generate_sql()
{
# Get domain name.
DOMAIN="$1"
shift 1
for i in $@; do
username="$i"
mail="${username}@${DOMAIN}"
if [ X"${USE_DEFAULT_PASSWD}" != X"YES" ]; then
export CRYPT_PASSWD="$(openssl passwd -1 ${username})"
else
:
fi
# Different maildir style: hashed, normal.
if [ X"${MAILDIR_STYLE}" == X"hashed" ]; then
length="$(echo ${username} | wc -L)"
str1="$(echo ${username} | cut -c1)"
str2="$(echo ${username} | cut -c2)"
str3="$(echo ${username} | cut -c3)"
if [ X"${length}" == X"1" ]; then
str2="${str1}"
str3="${str1}"
elif [ X"${length}" == X"2" ]; then
str3="${str2}"
else
:
fi
# Use mbox, will be changed later.
maildir="${DOMAIN}/${str1}/${str2}/${str3}/${username}-${DATE}"
else
# Use mbox, will be changed later.
maildir="${DOMAIN}/${username}-${DATE}"
fi
# Different maildir format: maildir, mbox.
if [ X"${MAILBOX_FORMAT}" == X"Maildir" ]; then
# Append slash to make it 'maildir' format.
maildir="${maildir}/"
else
# It's already mbox format.
:
fi
cat >> ${SQL} <<EOF
INSERT INTO mailbox (username, password, name, storagebasedirectory,storagenode, maildir, quota, domain, active, local_part, created)
VALUES ('${mail}', '${CRYPT_PASSWD}', '${username}', '${STORAGE_BASE_DIRECTORY}','${STORAGE_NODE}', '${maildir}', '${DEFAULT_QUOTA}', '${DOMAIN}', '1','${username}', NOW());
INSERT INTO alias (address, goto, domain, created, active) VALUES ('${mail}', '${mail}','${DOMAIN}', NOW(), 1);
EOF
done
}
if [ $# -lt 2 ]; then
echo "Usage: $0 domain_name username [user2 user3 user4 ...]"
else
# Generate SQL template.
generate_sql $@ && \
cat <<EOF
SQL template file was generated successfully, Please import it
*MANUALLY* after verify the records:
- ${SQL}
Steps to import these users looks like below:
# mysql -uroot -p
mysql> USE vmail;
mysql> SOURCE ${SQL};
EOF
fi
##################################################################
==== Provide basic information to help troubleshoot ====
- iRedMail version: iRedMail-0.7.3
- Linux/BSD distribution name and version: OEL 5.7
======
----
Spider Email Archiver: On-Premises, lightweight email archiving software developed by iRedMail team. Supports Amazon S3 compatible storage and custom branding.