From be63b2fb28f29eba98cbc10cb3d18fe6e4ca4ad3 Mon Sep 17 00:00:00 2001 From: Matthew Saunders Brown Date: Wed, 7 Dec 2022 09:49:19 -0800 Subject: [PATCH] add export/import scripts for migrating vmail domains --- bin/vmail-domains-exp.sh | 82 +++++++++++++++++++++++++++++++++++++ bin/vmail-domains-imp.sh | 88 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100755 bin/vmail-domains-exp.sh create mode 100755 bin/vmail-domains-imp.sh diff --git a/bin/vmail-domains-exp.sh b/bin/vmail-domains-exp.sh new file mode 100755 index 0000000..4e6a02b --- /dev/null +++ b/bin/vmail-domains-exp.sh @@ -0,0 +1,82 @@ +#!/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 diff --git a/bin/vmail-domains-imp.sh b/bin/vmail-domains-imp.sh new file mode 100755 index 0000000..d14401e --- /dev/null +++ b/bin/vmail-domains-imp.sh @@ -0,0 +1,88 @@ +#!/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 + + echo "ERROR: $domain already exists in vmail database." + exit 1 + +elif [[ -z $domains_id ]] ; then + + if [[ -d /var/vmail/$domain ]]; then + + # remove existing dump, if it exists + if [[ -f /var/vmail/$domain/vm_dmp.sql ]]; then + + eval mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE vmail < /var/vmail/$domain/vm_dmp.sql + + echo + echo "Import complete. If it's no longer need you can remove the dump file:" + echo "/var/vmail/$domain/vm_dmp.sql" + echo + + if [[ -f /var/vmail/$virtualhost/roundcubemail ]]; then + + echo "Found roundcubemail import file. You probably want to run this next:" + echo "vmail-roundcubemail-settings-import.php $domain" + + else + + echo "No roundcubemail file found. Did you remember to export & sync that too?" + + fi + + else + + echo "ERROR: Dump file /var/vmail/$domain/vm_dmp.sql does not exist." + exit 1 + + fi + + else + + echo "$domain exists in vmail db, but /var/vmail/$domain dir does not exist." + echo "This script excepts a sync of the /var/vmail/$domain dir and no records in the db." + exit 1 + + fi + +else + echo "ERROR: System error querying vmail database" + exit 1 +fi