125 lines
4.6 KiB
Bash
Executable File
125 lines
4.6 KiB
Bash
Executable File
#!/bin/bash
|
|
#
|
|
# vmail-stack
|
|
# https://git.stack-source.com/msb/vmail-stack
|
|
# Copyright (c) 2022 Matthew Saunders Brown <matthewsaundersbrown@gmail.com>
|
|
# 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 "Export vmail settings, for backups and/or migrating to a new server."
|
|
echo ""
|
|
echo "usage: $thisfilename -d <domain> [-v] [-h]"
|
|
echo ""
|
|
echo " -h Print this help."
|
|
echo " -d Domain to export settings for."
|
|
echo " -v Verbose - output instructions for sycning vmail domain to new server, if verbose option was enabled."
|
|
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
|
|
|
|
if [[ -d /var/vmail/$domain/.exp/ ]]; then
|
|
# remove existing export data
|
|
rm -r /var/vmail/$domain/.exp/
|
|
fi
|
|
|
|
# create new export dir
|
|
install --owner=vmail --group=vmail --mode=750 --directory /var/vmail/$domain/.exp/
|
|
|
|
# create new empty dump file
|
|
install --owner=vmail --group=vmail --mode=640 /dev/null /var/vmail/$domain/.exp/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 vmail $vmDbTable --where="\"domain='$domain'"\"|sed -e "s/(\`id\`,/(/"|sed -e "s/([0-9]*,/(/" >> /var/vmail/$domain/.exp/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 vmail vm_greylisting --where="\"recipient LIKE '%@$domain'"\"|sed -e "s/(\`id\`,/(/"|sed -e "s/([0-9]*,/(/" >> /var/vmail/$domain/.exp/vm_dmp.sql
|
|
eval mysqldump --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE --no-create-info --extended-insert=FALSE --complete-insert --compact vmail sa_userpref --where="\"username LIKE '%@$domain'"\"|sed -e "s/, \`prefid\`)/)/"|sed -e "s/,[0-9]*);/);/" >> /var/vmail/$domain/.exp/vm_dmp.sql
|
|
|
|
# apache webmail config
|
|
if [[ -f /etc/apache2/sites-available/mail.$domain.conf ]]; then
|
|
cp --archive --parents /etc/apache2/sites-*/mail.$domain.conf /var/vmail/$domain/.exp/
|
|
fi
|
|
|
|
# letsencrypt certificate
|
|
if [[ -f /etc/letsencrypt/renewal/mail.$domain.conf ]]; then
|
|
cp --archive --parents /etc/letsencrypt/archive/mail.$domain/ /var/vmail/$domain/.exp/
|
|
cp --archive --parents /etc/letsencrypt/live/mail.$domain/ /var/vmail/$domain/.exp/
|
|
cp --archive --parents /etc/letsencrypt/renewal/mail.$domain.conf /var/vmail/$domain/.exp/
|
|
fi
|
|
|
|
# letsencrypt pem file
|
|
if [[ -f /etc/ssl/letsencrypt/mail.$domain.pem ]]; then
|
|
cp --archive --parents /etc/ssl/letsencrypt/mail.$domain.pem /var/vmail/$domain/.exp/
|
|
fi
|
|
|
|
# dkim files
|
|
if [[ -f /etc/ssl/dkim/$domain.dkim ]]; then
|
|
cp --archive --parents /etc/ssl/dkim/$domain.* /var/vmail/$domain/.exp/
|
|
fi
|
|
|
|
# dovecot config
|
|
if [[ -f /etc/dovecot/sites.d/mail.$domain.conf ]]; then
|
|
cp --archive --parents /etc/dovecot/sites.d/mail.$domain.conf /var/vmail/$domain/.exp/
|
|
fi
|
|
|
|
if [[ -x /usr/local/bin/vmail-roundcubemail-settings-export.php ]]; then
|
|
/usr/local/bin/vmail-roundcubemail-settings-export.php $domain
|
|
fi
|
|
|
|
# output instructions for sycning website to new server, if verbose option was enabled.
|
|
if [[ -n $verbose ]]; then
|
|
|
|
servername=`hostname -f`
|
|
|
|
echo
|
|
echo "Vmail configs for $domain have been exported."
|
|
echo "To migrate to a new server run these commands (as root) from the new server:"
|
|
echo
|
|
echo "rsync -v --archive --relative --rsh=/usr/bin/ssh root@$servername:/var/vmail/$domain /"
|
|
echo "vmail-domains-imp.sh -d $domain"
|
|
|
|
fi
|
|
|
|
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
|