#!/bin/bash # # vhost-stack # https://git.stack-source.com/msb/vhost-stack # Copyright (c) 2023 Matthew Saunders Brown # 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 "Create archive of virtualhost on this server. Includes website files, server configs and MySQL databases." echo "" echo "usage: $thisfilename -d [OPTIONS]" echo "" echo " -h Print this help." echo " -d Domain name of VirtualHost to create archive for." exit } vhost:getoptions "$@" # check for domain (virtualhost) if [[ -z $domain ]]; then echo "domain is required" exit fi if [[ -d /srv/www/$domain ]]; then username=$(stat -c '%U' /srv/www/$domain) else echo "virtualhost for $domain does not exist" exit 1 fi if [[ -d /srv/www/$domain/.archive/ ]]; then echo "/srv/www/$domain/.archive/ already exists." echo "Remove /srv/www/$domain/.archive/ and run again if desired." exit 1 else mkdir /srv/www/$domain/.archive/ fi cp --archive --parents /home/$username /srv/www/$domain/.archive/ # apache config if [[ -f /etc/apache2/sites-available/$domain.conf ]]; then cp --archive --parents /etc/apache2/sites-*/$domain.conf /srv/www/$domain/.archive/ fi # letsencrypt certificate if [[ -f /etc/letsencrypt/renewal/$domain.conf ]]; then cp --archive --parents /etc/letsencrypt/archive/$domain/ /srv/www/$domain/.archive/ cp --archive --parents /etc/letsencrypt/live/$domain/ /srv/www/$domain/.archive/ cp --archive --parents /etc/letsencrypt/renewal/$domain.conf /srv/www/$domain/.archive/ fi # letsencrypt pem file if [[ -f /etc/ssl/letsencrypt/$domain.pem ]]; then cp --archive --parents /etc/ssl/letsencrypt/$domain.pem /srv/www/$domain/.archive/ fi # varnish config if [[ -f /etc/varnish/sites.d/$domain.vcl ]]; then cp --archive --parents /etc/varnish/sites.d/$domain.vcl /srv/www/$domain/.archive/ fi # php-fpm 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/.archive/ fi # mysql database(s) 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/.archive/mysql/ for database in "${database_array[@]}"; do mysqldump --opt --quote-names --events --databases $database | gzip > /srv/www/$domain/.archive/mysql/$database.sql.gz done fi if [[ ! -d /opt/archives ]]; then mkdir /opt/archives fi tar --directory=/srv/www/ -czf /opt/archives/$domain.tar.gz $domain