rework script - add verbose option, fix database export section

This commit is contained in:
Matthew Saunders Brown 2023-09-01 15:32:35 -07:00
parent cbf10ae094
commit 09ec06aee4

View File

@ -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 <domain>"
echo "usage: $thisfilename -d <domain> [-v] [-h]"
echo ""
echo " -h Print this help."
echo " -d Domain to export settings for."
echo " -d <domain> Domain to export settings for."
echo " -v Verbose - output instructions for sycning website to new server, if verbose option was enabled."
exit
}
@ -31,30 +32,14 @@ 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)
# 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
write=1
else
echo "NOTICE: system users password not autodetected!"
password=`/usr/bin/pwgen 12 1`
write=0
fi
# create export dir
install --owner=$username --group=$username --mode=750 --directory /srv/www/$domain/.exp/
@ -82,8 +67,35 @@ if [[ -d /srv/www/$domain ]]; then
fi
# mysql
database=${domain//./dot}
database=${database//-/dash}
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
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
write=1
else
echo "NOTICE: system users password not autodetected!"
password=`/usr/bin/pwgen 12 1`
write=0
fi
# 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