From 09ec06aee484416db760de4f164d1e6d5bbb283d Mon Sep 17 00:00:00 2001 From: Matthew Saunders Brown Date: Fri, 1 Sep 2023 15:32:35 -0700 Subject: [PATCH] rework script - add verbose option, fix database export section --- bin/vhost-exp.sh | 93 ++++++++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 42 deletions(-) diff --git a/bin/vhost-exp.sh b/bin/vhost-exp.sh index 7317e16..5a0def8 100755 --- a/bin/vhost-exp.sh +++ b/bin/vhost-exp.sh @@ -14,10 +14,11 @@ help() echo "$thisfilename" echo "Export vhost settings, for backups and/or migrating to a new server." echo "" - echo "usage: $thisfilename -d " + echo "usage: $thisfilename -d [-v] [-h]" echo "" echo " -h Print this help." - echo " -d Domain to export settings for." + echo " -d Domain to export settings for." + echo " -v Verbose - output instructions for sycning website to new server, if verbose option was enabled." exit } @@ -31,15 +32,54 @@ fi if [[ -d /srv/www/$domain ]]; then + # check for and remove existing export data if [[ -d /srv/www/$domain/.exp/ ]]; then - # existing export data - echo "Export dir already exists. If you want to re-create export first run:" - echo "rm -r /srv/www/$domain/.exp/" - exit - else + rm -r /srv/www/$domain/.exp/ + fi - # system username - username=$(stat -c '%U' /srv/www/$domain) + # system username + username=$(stat -c '%U' /srv/www/$domain) + + # create export dir + install --owner=$username --group=$username --mode=750 --directory /srv/www/$domain/.exp/ + + # apache config + if [[ -f /etc/apache2/sites-available/$domain.conf ]]; then + cp --archive --parents /etc/apache2/sites-*/$domain.conf /srv/www/$domain/.exp/ + fi + + # letsencrypt certificate + if [[ -f /etc/letsencrypt/renewal/$domain.conf ]]; then + cp --archive --parents /etc/letsencrypt/archive/$domain/ /srv/www/$domain/.exp/ + cp --archive --parents /etc/letsencrypt/live/$domain/ /srv/www/$domain/.exp/ + cp --archive --parents /etc/letsencrypt/renewal/$domain.conf /srv/www/$domain/.exp/ + fi + + # letsencrypt pem file + if [[ -f /etc/ssl/letsencrypt/$domain.pem ]]; then + cp --archive --parents /etc/ssl/letsencrypt/$domain.pem /srv/www/$domain/.exp/ + fi + + # php config + vhost::set-phpVersion + if [[ -f /etc/php/$phpVersion/fpm/pool.d/$username.conf ]]; then + cp --archive --parents /etc/php/$phpVersion/fpm/pool.d/$username.conf /srv/www/$domain/.exp/ + fi + + # mysql + basedatabase=${domain//./dot} + basedatabase=${basedatabase//-/dash} + database_array=(`mysql -s -N -e "SELECT SCHEMA_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = '$basedatabase' OR SCHEMA_NAME LIKE '$basedatabase\_%'" | tr '\n' ' ' | xargs`) + + if [[ ${#database_array[@]} -gt 0 ]]; then + mkdir /srv/www/$domain/.exp/mysql/ + for database in "${database_array[@]}"; do + mysqldump --opt --quote-names --events --databases $database > /srv/www/$domain/.exp/mysql/$database.sql + done + fi + + # output instructions for sycning website to new server, if verbose option was enabled. + if [[ -n $verbose ]]; then # check for .passwd if [[ -f /home/$username/.passwd ]]; then @@ -55,35 +95,7 @@ if [[ -d /srv/www/$domain ]]; then write=0 fi - # create export dir - install --owner=$username --group=$username --mode=750 --directory /srv/www/$domain/.exp/ - - # apache config - if [[ -f /etc/apache2/sites-available/$domain.conf ]]; then - cp --archive --parents /etc/apache2/sites-*/$domain.conf /srv/www/$domain/.exp/ - fi - - # letsencrypt certificate - if [[ -f /etc/letsencrypt/renewal/$domain.conf ]]; then - cp --archive --parents /etc/letsencrypt/archive/$domain/ /srv/www/$domain/.exp/ - cp --archive --parents /etc/letsencrypt/live/$domain/ /srv/www/$domain/.exp/ - cp --archive --parents /etc/letsencrypt/renewal/$domain.conf /srv/www/$domain/.exp/ - fi - - # letsencrypt pem file - if [[ -f /etc/ssl/letsencrypt/$domain.pem ]]; then - cp --archive --parents /etc/ssl/letsencrypt/$domain.pem /srv/www/$domain/.exp/ - fi - - # php config - vhost::set-phpVersion - if [[ -f /etc/php/$phpVersion/fpm/pool.d/$username.conf ]]; then - cp --archive --parents /etc/php/$phpVersion/fpm/pool.d/$username.conf /srv/www/$domain/.exp/ - fi - - # mysql - database=${domain//./dot} - database=${database//-/dash} + # get/set mysql user/pass info if [[ -f /srv/www/$domain/.my.cnf ]]; then dbuser=$(grep ^user= /srv/www/$domain/.my.cnf |cut -d = -f 2) dbpass=$(grep ^password= /srv/www/$domain/.my.cnf |cut -d = -f 2) @@ -94,10 +106,6 @@ if [[ -d /srv/www/$domain ]]; then dbpass=password fi - if [[ -d /var/lib/mysql/$database ]]; then - mysqldump --opt $database > /srv/www/$domain/.exp/$database.sql - fi - echo echo "Vhost configs for $domain have been exported." echo "To migrate to a new server run these commands (as root) from the new server:" @@ -123,6 +131,7 @@ if [[ -d /srv/www/$domain ]]; then if [[ -d /srv/www/$domain/.exp/etc ]]; then echo "cp -a /srv/www/$domain/.exp/etc/* /etc/" fi + fi else