#!/bin/bash # # vmail-stack # https://git.stack-source.com/msb/vmail-stack # Copyright (c) 2022 Matthew Saunders Brown # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # load include file source $(dirname $0)/vmail.sh help() { thisfilename=$(basename -- "$0") echo "$thisfilename" echo "Add domain to vmail system" echo "" echo "usage: $thisfilename -d " echo "" echo " -h Print this help." echo " -d Domain to export from the vmail database." exit } vmail:getoptions "$@" # check for domain if [[ -z $domain ]]; then echo "ERROR: domain name is required" exit 1 fi # build query dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE" dbcmdopts="-s -r -N -e" # check if domain exists in vmail database dbquery="SELECT id FROM vm_domains WHERE domain='$domain';" domains_id=`$dbcmd $dbcmdopts "$dbquery"` if [[ "$domains_id" -gt '0' ]] ; then if [[ -d /var/vmail/$domain ]]; then # remove existing dump, if it exists if [[ -f /var/vmail/$domain/vm_dmp.sql ]]; then rm /var/vmail/$domain/vm_dmp.sql fi # create new empty dump file install --owner vmail --group vmail --mode=640 /dev/null /var/vmail/$domain/vm_dmp.sql # create array of vm db tables vmDbTableArray=("vm_domains" "vm_mboxes" "vm_aliases" "vm_autoresponders" "vm_filters" "vm_forwards") # dump data from each table for vmDbTable in ${vmDbTableArray[@]}; do eval mysqldump --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE --no-create-info --extended-insert=FALSE --complete-insert --compact --databases vmail --tables $vmDbTable --where="\"domain='$domain'"\"|sed -e "s/(\`id\`,/(/"|sed -e "s/([0-9]*,/(/" >> /var/vmail/$domain/vm_dmp.sql done # handle special cases vm_greylisting & sa_userpref eval mysqldump --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE --no-create-info --extended-insert=FALSE --complete-insert --compact --databases vmail --tables vm_greylisting --where="\"recipient LIKE '%@$domain'"\"|sed -e "s/(\`id\`,/(/"|sed -e "s/([0-9]*,/(/" >> /var/vmail/$domain/vm_dmp.sql eval mysqldump --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE --no-create-info --extended-insert=FALSE --complete-insert --compact --databases vmail --tables sa_userpref --where="\"username LIKE '%@$domain'"\"|sed -e "s/, \`prefid\`)/)/"|sed -e "s/,[0-9]*);/);/" >> /var/vmail/$domain/vm_dmp.sql echo echo "/var/vmail/$domain/vm_dmp.sql created." echo "You probably want to export the rouncubemail db too:" echo "vmail-roundcubemail-settings-export.php $domain" echo "Then sync /var/vmail/$domain/ to the new server and on the new server run:" echo "vmail-domains-imp.sh -d $domain" else echo "$domain exists in vmail db, but /var/vmail/$domain dir does not exist." exit 1 fi elif [[ -z $domains_id ]] ; then echo "ERROR: $domain does not exist in vmail database." exit 1 else echo "ERROR: System error querying vmail database" exit 1 fi