vmail-stack/bin/vmail-domains-exp.sh

118 lines
4.4 KiB
Bash
Raw Normal View History

#!/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"
2023-01-01 16:38:49 -08:00
echo "Export vmail settings, for backups and/or migrating to a new server."
echo ""
echo "usage: $thisfilename -d <domain>"
echo ""
echo " -h Print this help."
2023-01-01 16:38:49 -08:00
echo " -d Domain to export settings for."
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
2023-01-01 16:38:49 -08:00
if [[ -d /var/vmail/$domain/.exp/ ]]; then
# remove existing export data
rm -r /var/vmail/$domain/.exp/
fi
2023-01-01 16:38:49 -08:00
# create new export dir
install --owner=vmail --group=vmail --mode=750 --directory /var/vmail/$domain/.exp/
# create new empty dump file
2023-01-01 16:38:49 -08:00
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
2023-01-01 16:38:49 -08:00
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/.exp/vm_dmp.sql
done
# handle special cases vm_greylisting & sa_userpref
2023-01-01 16:38:49 -08:00
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/.exp/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/.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
2023-01-02 11:58:36 -08:00
servername=`hostname -d`
echo
2023-01-01 16:38:49 -08:00
echo "Vmail configs for $domain have been exported."
2023-01-02 11:58:36 -08:00
echo "To migrate to a new server run these commands (as root) from the new server:"
echo "rsync -v --archive --relative --rsh=/usr/bin/ssh root@$servername:/var/vmail/$domain /"
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