From 9374261c80305b0c51d8c35c127a5dab58d8e362 Mon Sep 17 00:00:00 2001 From: Matthew Saunders Brown Date: Sun, 1 Jan 2023 16:38:49 -0800 Subject: [PATCH] finish up domain export/import tools --- bin/vmail-domains-exp.sh | 58 ++++++++++++++++----- bin/vmail-domains-imp.sh | 36 ++++++++----- bin/vmail-roundcubemail-settings-export.php | 17 +++++- bin/vmail-roundcubemail-settings-import.php | 9 ++-- bin/vmail.sh | 6 ++- 5 files changed, 94 insertions(+), 32 deletions(-) diff --git a/bin/vmail-domains-exp.sh b/bin/vmail-domains-exp.sh index 4e6a02b..05dc044 100755 --- a/bin/vmail-domains-exp.sh +++ b/bin/vmail-domains-exp.sh @@ -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 " 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 diff --git a/bin/vmail-domains-imp.sh b/bin/vmail-domains-imp.sh index 223310c..c2d83c0 100755 --- a/bin/vmail-domains-imp.sh +++ b/bin/vmail-domains-imp.sh @@ -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 - echo - echo "Import complete. If it's no longer need you can remove the dump file:" - echo "/var/vmail/$domain/vm_dmp.sql" - echo + # import vmail sql data + eval mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE vmail < /var/vmail/$domain/.exp/vm_dmp.sql - if [[ -f /var/vmail/$domain/roundcubemail ]]; then + # roundcube settings + if [[ -f /var/vmail/$domain/.exp/roundcubemail.json ]]; then + /usr/local/bin/vmail-roundcubemail-settings-import.php $domain + fi - echo "Found roundcubemail import file. You probably want to run this next:" - echo "vmail-roundcubemail-settings-import.php $domain" + # /etc/ configs + if [[ -d /var/vmail/$domain/.exp/etc ]]; then + cp -a /var/vmail/$domain/.exp/etc/* /etc/ + fi + + echo + 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 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 diff --git a/bin/vmail-roundcubemail-settings-export.php b/bin/vmail-roundcubemail-settings-export.php index 72662ed..e9a84e0 100755 --- a/bin/vmail-roundcubemail-settings-export.php +++ b/bin/vmail-roundcubemail-settings-export.php @@ -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'); diff --git a/bin/vmail-roundcubemail-settings-import.php b/bin/vmail-roundcubemail-settings-import.php index 2d29adc..14c4fbd 100755 --- a/bin/vmail-roundcubemail-settings-import.php +++ b/bin/vmail-roundcubemail-settings-import.php @@ -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"; diff --git a/bin/vmail.sh b/bin/vmail.sh index 6a2da97..057a80b 100755 --- a/bin/vmail.sh +++ b/bin/vmail.sh @@ -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