add vhost-exp.sh

This commit is contained in:
Matthew Saunders Brown 2023-03-30 15:01:51 -07:00
parent a61ceec6c1
commit d6a484f95a

139
bin/vhost-exp.sh Executable file
View File

@ -0,0 +1,139 @@
#!/bin/bash
#
# vhost-stack
# https://git.stack-source.com/msb/vhost-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)/vhost.sh
help()
{
thisfilename=$(basename -- "$0")
echo "$thisfilename"
echo "Export vhost 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 settings for."
exit
}
vhost:getoptions "$@"
# check for domain
if [[ -z $domain ]]; then
echo "ERROR: domain name is required"
exit 1
fi
if [[ -d /srv/www/$domain ]]; then
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
# system username
username=$(stat -c '%U' /srv/www/$domain)
# check for .passwd
if [[ -f /home/$username/.passwd ]]; then
password=$(cat /home/$username/.passwd | cut -d : -f 2)
if [[ -f /root/.vhost.ini ]]; then
vhost::set-opensslpass
password=`echo "$password" | openssl aes-256-cbc -d -a -pass pass:$opensslpass -pbkdf2`
fi
else
echo "NOTICE: system users password not autodetected!"
password=`/usr/bin/pwgen 12 1`
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}
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)
# elif wp-config.php
else
echo "NOTICE: db user & password not autodetected!"
dbuser=$username@$domain
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:"
echo ""
echo "/usr/local/bin/vhost-user-add.sh -u $username -p \"$password\" -w"
if [[ -d /usr/jails/$username ]]; then
echo "/usr/local/bin/vhost-user-jail.sh -u $username >/dev/null 2>/dev/null &"
fi
echo "/usr/local/bin/vhost-add.sh -d $domain -u $username -w"
echo "/usr/local/bin/vhost-mysql-db-add.sh -d $domain -u $dbuser -p $dbpass -w"
servername=`hostname -f`
echo "rsync -vn --archive --exclude='.passwd' --rsh=/usr/bin/ssh root@$servername:/home/$username/ /home/$username/"
echo "rsync -vn --archive --exclude='.my.cnf' --rsh=/usr/bin/ssh root@$servername:/srv/www/$domain/ /srv/www/$domain/"
#db import
if [[ -f /srv/www/$domain/.exp/$database.sql ]]; then
echo "mysql $database < /srv/www/$domain/.exp/$database.sql"
fi
# /etc/ configs
if [[ -d /srv/www/$domain/.exp/etc ]]; then
echo "cp -a /srv/www/$domain/.exp/etc/* /etc/"
# reload apache
if [[ -f /srv/www/$domain/.exp/etc/apache2/sites-enabled/$domain.conf ]]; then
echo "systemctl reload apache2.service"
fi
# reload php
if [[ -f /srv/www/$domain/.exp/etc/php/$phpVersion/fpm/pool.d/$username.conf ]]; then
echo "systemctl reload php$phpVersion-fpm"
fi
fi
fi
else
echo "Virtualhost for $domain does not exist."
exit
fi