Advertisement
Guest User

Untitled

a guest
Oct 16th, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.28 KB | None | 0 0
  1. # Consts:
  2. bindDir="/etc/bind"
  3. bindMainConfig="$bindDir/named.conf.local"
  4. apSiteDir="/var/www"
  5. apBaseDir="/etc/apache2"
  6.  
  7. # Check configuration:
  8. if [[ ! -d $bindDir ]] || [[ ! -f $bindMainConfig ]] ||
  9.         [[ ! -d $apSiteDir ]] || [[ ! -d $apBaseDir ]]; then
  10.     echo "[ FAIL ] Check configuration paths, some of them are invalids"
  11.     exit
  12. fi
  13.  
  14. if [[ "$1" == "--help" ]]; then
  15.     echo "-db <database name> create a database with the given name and user with password for it"
  16.     echo "  You can specify a username and password using the --user and --password"
  17.  
  18.     echo "-a <aliase> will add to your domain"
  19.     echo "  TIPS: This parameter can be used repeatedly"
  20.  
  21.     exit
  22. fi
  23.  
  24. # Check that required and validate action:
  25. if [[ $# < 2 ]] || ([[ "$1" != "add" ]] && [[ "$1" != "del" ]]); then
  26.     echo "Usage: $0 <action: add|del> <domain> [options]"
  27.     echo "Try '$0 --help' for more information"
  28.     exit
  29. fi
  30. action="$1"; shift
  31.  
  32. # Validate domain:
  33. if [[ $(echo "$1" | grep -E '^[a-zA-Z0-9_-]+\.[a-zA-Z]+$') != $1 ]]; then
  34.     echo "[ FAIL ] Domain name ('$1') not valid"
  35.     exit
  36. fi
  37. domain="$1"; shift
  38.  
  39. # Parse args:
  40. while [[ $# > 1 ]]; do
  41.     case "$1" in
  42.         -a | --aliase)
  43.             if [[ $(echo "$2" | grep -E '^[a-zA-Z0-9]+\.[a-zA-Z0-9_-]+\.[a-zA-Z]+$') == $2 ]]; then
  44.                 aliases+=("$2")
  45.             else
  46.                 echo -n "Alias '$2' was DROPPED, want to continue? (y/N) "
  47.                 read answer
  48.                 if [[ $answer != "y" ]] && [[ $answer != "Y" ]]; then
  49.                     exit
  50.                 fi
  51.             fi
  52.             shift
  53.             ;;
  54.         -db)
  55.             dbName="$2"
  56.             shift
  57.             ;;
  58.         --user)
  59.             dbUser="$2"
  60.             shift
  61.             ;;
  62.         --password)
  63.             dbPwd="$2"
  64.             shift
  65.             ;;
  66.     esac
  67.     shift
  68. done
  69.  
  70. # Verify database name if available:
  71. if [[ $dbName ]]; then
  72.     if [[ $(echo "$dbName" | grep -E '^[a-zA-Z0-9]+$') != $dbName ]]; then
  73.         echo "[ FAIL ] Database name ('$dbName') not valid"; exit
  74.     fi
  75.  
  76.     # And check mysql:
  77.     if [[ ! $(mysql --help 2> /dev/null) ]]; then
  78.         echo "[ FAIL ] MySQL not found"; exit
  79.     fi
  80.  
  81.     # Also setup username and password if none:
  82.     if [[ ! $dbUser ]]; then
  83.         dbUser=$dbName
  84.     fi
  85.     if [[ ! $dbPwd ]]; then
  86.         dbPwd=$domain
  87.     fi
  88. fi
  89.  
  90. # Delete command:
  91. if [[ $action == "del" ]]; then
  92.  
  93.     # Remove from apache:
  94.     if [ -f $apBaseDir/sites-enabled/$domain.conf ]; then
  95.         a2dissite $domain > /dev/null
  96.         service apache2 reload
  97.         rm -r $apSiteDir/$domain
  98.         rm $apBaseDir/sites-available/$domain.conf
  99.         echo "[ OK ] Domain '$domain' successfully DELETED from APACHE!"
  100.     fi
  101.  
  102.     # From BIND:
  103.     if [ -f $bindDir/db.$domain.conf ]; then
  104.         sed "/zone \"$domain\"/,/}/d" -i $bindMainConfig
  105.         service bind9 restart
  106.         rm $bindDir/db.$domain.conf
  107.         echo "[ OK ] Domain '$domain' successfully DELETED from BIND!"
  108.     fi
  109.  
  110.     # From MySQL:
  111.     if [[ $dbName ]]; then
  112.         mysql -u root -e "drop user $dbUser@$dbName" 2> /dev/null
  113.         mysql -u root -e "drop database $dbName" 2> /dev/null
  114.         echo "[ OK ] Database '$dbName' dropped!"
  115.     fi; exit
  116. fi
  117.  
  118. # Now will ADD action ----------------------------------------
  119.  
  120. # If already in use:
  121. message="[ FAIL ] Domain '$domain' already in use"
  122. if [[ $(cat $apBaseDir/sites-enabled/* | grep -v '^\s*#' | grep $domain) ]]; then
  123.     echo $message; exit
  124. fi
  125.  
  126. for alias in ${aliases[@]}; do
  127.     if [[ $(cat $apBaseDir/sites-enabled/* | grep -v '^\s*#' | grep $alias) ]]; then
  128.         echo "[ FAIL ] Alias '$alias' already in use"; exit
  129.     fi
  130. done
  131.  
  132. if [[ $(cat $bindDir/* | grep -v '^\s*#' | grep $domain) ]]; then
  133.     echo $message; exit
  134. fi
  135.  
  136. if [[ -d $apSiteDir/$domain ]] || [[ -f $apBaseDir/sites-available/$domain.conf ]] ||
  137.         [[ -f $bindDir/db.$domain.conf ]]; then
  138.     echo $message; exit
  139. fi
  140.  
  141.  
  142. # Create that needed ----------------------------------------
  143.  
  144. # MySQL:
  145. if [[ $dbName ]]; then
  146.     if [[ $(mysql -u root -e "create database $dbName" 2>&1) ]]; then
  147.         echo "[ FAIL ] Database '$dbName' already exists!"; exit
  148.     fi
  149.     if [[ $(mysql -u root -e "create user $dbUser@$dbName identified by '$dbPwd'" 2>&1) ]]; then
  150.         echo "[ FAIL ] User '$dbUser' already exists!"
  151.         mysql -u root -e "drop database $dbName"; exit
  152.     fi
  153.     mysql -u root -e "grant all privileges on $dbName.* to $dbUser@$dbName"
  154.     mysql -u root -e "flush privileges"
  155.     echo "[ OK ] Database '$dbName' successfully created"
  156.     echo "  User = '$dbUser' | Password = '$dbPwd'"
  157. fi
  158.  
  159. # Apache:
  160. apBaseDir="$apBaseDir/sites-available"
  161. mkdir $apSiteDir/$domain
  162. echo -e "<?php\n    phpinfo();\n?>" >> $apSiteDir/$domain/index.php
  163.  
  164. echo "<VirtualHost *:80>" >> $apBaseDir/$domain.conf
  165. echo "  ServerName $domain" >> $apBaseDir/$domain.conf
  166. echo "  ServerAdmin mail@$domain" >> $apBaseDir/$domain.conf
  167. echo "  DocumentRoot $apBaseDir/$domain" >> $apBaseDir/$domain.conf
  168. echo "  ErrorLog \${APACHE_LOG_DIR}/$domain.error.log" >> $apBaseDir/$domain.conf
  169. echo "  CustomLog \${APACHE_LOG_DIR}/$domain.access.log combined" >> $apBaseDir/$domain.conf
  170.  
  171. for alias in ${aliases[@]}; do
  172.     echo "  ServerAlias $alias" >> $apBaseDir/$domain.conf
  173. done
  174. echo "</VirtualHost>" >> $apBaseDir/$domain.conf
  175.  
  176. # Bind:
  177. cp $bindDir/db.local $bindDir/db.$domain.conf
  178. sed -i -- "s/localhost/$domain/g" $bindDir/db.$domain.conf
  179.  
  180. echo "zone \"$domain\" {" >> $bindMainConfig
  181. echo "  type master;" >> $bindMainConfig
  182. echo "  file \"$bindDir/db.$domain.conf\";" >> $bindMainConfig
  183. echo "};" >> $bindMainConfig
  184.  
  185. # Enable this now:
  186. a2ensite $domain > /dev/null
  187. service apache2 reload
  188. service bind9 reload
  189.  
  190. echo "[ OK ] Domain '$domain' successfully ADDED!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement