finish up domain export/import tools

This commit is contained in:
Matthew Saunders Brown 2023-01-01 16:38:49 -08:00
parent 0703ab4cc4
commit 9374261c80
5 changed files with 94 additions and 32 deletions

View File

@ -12,12 +12,12 @@ help()
{
thisfilename=$(basename -- "$0")
echo "$thisfilename"
echo "Add domain to vmail system"
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."
echo " -d Domain to export from the vmail database."
echo " -d Domain to export settings for."
exit
}
@ -41,29 +41,61 @@ 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
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/vm_dmp.sql
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 --databases vmail --tables $vmDbTable --where="\"domain='$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 $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 --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
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
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 configs for $domain have been exported."
echo "To migrate to a new server sync /var/vmail/$domain/ to the new server and on the new server run:"
echo "vmail-domains-imp.sh -d $domain"
else

View File

@ -46,30 +46,42 @@ 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
if [[ -d /var/vmail/$domain/.exp ]]; then
eval mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE vmail < /var/vmail/$domain/vm_dmp.sql
# check for vmail sql file
if [[ -f /var/vmail/$domain/.exp/vm_dmp.sql ]]; then
# import vmail sql data
eval mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE vmail < /var/vmail/$domain/.exp/vm_dmp.sql
# roundcube settings
if [[ -f /var/vmail/$domain/.exp/roundcubemail.json ]]; then
/usr/local/bin/vmail-roundcubemail-settings-import.php $domain
fi
# /etc/ configs
if [[ -d /var/vmail/$domain/.exp/etc ]]; then
cp -a /var/vmail/$domain/.exp/etc/* /etc/
fi
echo
echo "Import complete. If it's no longer need you can remove the dump file:"
echo "/var/vmail/$domain/vm_dmp.sql"
echo "Vmail import complete. If it's no longer need you can remove the data dir:"
echo "/var/vmail/$domain/.exp/"
echo
echo "Assuming /etc/ configs were imported you should now reload services with:"
echo "systemctl reload apache2.service dovecot.service"
echo
if [[ -f /var/vmail/$domain/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?"
echo "ERROR: Vmail SQL file does not exist."
exit 1
fi
else
echo "ERROR: Dump file /var/vmail/$domain/vm_dmp.sql does not exist."
echo "Vmail dir for $domain exists, but there is no .exp data dir."
exit 1
fi

View File

@ -21,7 +21,17 @@ if (!is_dir("/var/vmail/$virtualhost")) {
exit;
}
if (!is_writable("/var/vmail/$virtualhost")) {
if (is_writable("/var/vmail/$virtualhost")) {
if (is_dir("/var/vmail/$virtualhost/.exp")) {
if (is_file("/var/vmail/$virtualhost/.exp/roundcubemail.json")) {
unlink("/var/vmail/$virtualhost/.exp/roundcubemail.json");
}
} else {
mkdir("/var/vmail/$virtualhost/.exp", 0750);
chown("/var/vmail/$virtualhost/.exp", 'vmail');
chgrp("/var/vmail/$virtualhost/.exp", 'vmail');
}
} else {
$user = posix_getpwuid(posix_geteuid());
$username = $user['name'];
echo "/var/vmail/$virtualhost is not writable by this user ($username).\n";
@ -111,4 +121,7 @@ while ($row_users = mysqli_fetch_assoc($dbResult)) {
}
file_put_contents("/var/vmail/$virtualhost/roundcubemail", serialize($roundcubemail));
file_put_contents("/var/vmail/$virtualhost/.exp/roundcubemail.json", json_encode($roundcubemail));
chmod("/var/vmail/$virtualhost/.exp/roundcubemail.json", 0640);
chown("/var/vmail/$virtualhost/.exp/roundcubemail.json", 'vmail');
chgrp("/var/vmail/$virtualhost/.exp/roundcubemail.json", 'vmail');

View File

@ -16,8 +16,8 @@ if ($virtualhost == "") {
exit;
}
if (is_readable("/var/vmail/$virtualhost/roundcubemail")) {
$roundcubemail = unserialize(file_get_contents("/var/vmail/$virtualhost/roundcubemail"));
if (is_readable("/var/vmail/$virtualhost/.exp/roundcubemail.json")) {
$roundcubemail = json_decode(file_get_contents("/var/vmail/$virtualhost/.exp/roundcubemail.json"));
} else {
echo "$virtualhost roundcubemail file does not exist or is not readable by you\n";
exit;
@ -174,5 +174,6 @@ foreach ($roundcubemail as $virtualhost=>$users_array) {
}
}
echo "Import complete. If it's no longer need you can\n";
echo "remove the file /var/vmail/$virtualhost/roundcubemail\n";
echo "\n";
echo "Roundcube import complete. If it's no longer needed you can\n";
echo "remove the file /var/vmail/$virtualhost/.exp/roundcubemail.json\n";

View File

@ -12,7 +12,11 @@ readonly VMAIL_DIR=/var/vmail
readonly MYSQL_CONNECTION_INFO_FILE=$VMAIL_DIR/.my.cnf
# switch to required user
if [[ $(basename $0) == "vmail-dkim-"* ]]; then
if [[ $(basename $0) == "vmail-domains-exp.sh" ]]; then
if [[ "$USER" != "root" ]]; then
exec sudo -u root $0 "$@"
fi
elif [[ $(basename $0) == "vmail-dkim-"* ]]; then
if [[ "$USER" != "Debian-exim" ]]; then
exec sudo -u Debian-exim -g ssl-cert $0 "$@"
fi