Skip to content

Instantly share code, notes, and snippets.

@lgrz
Created February 5, 2010 04:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save lgrz/295489 to your computer and use it in GitHub Desktop.
Save lgrz/295489 to your computer and use it in GitHub Desktop.
bash scripts - apache, mysql
#!/usr/bin/env bash
#
# db_add
# A script to configure a new database
#
if [[ $# -lt 2 ]]
then
echo "usage: db_add <db-name> <db-server> [db-host]"
echo "example 1 connect from localhost: db_add \"db-name\" \"db-server\" \"localhost\""
echo "example 2 connect from any host: db_add \"db-name\" \"db-server\" \"%\""
exit 1
fi
# default to localhost
DB_HOST="localhost"
# Are we using a host other than the default?
if [ -z "$3" ]; then
$DB_HOST=$3
fi
# load config
if [ ! -f "$HOME/.db_add" ]; then
echo "Config file does not exist creating $HOME/.db_add"
touch "$HOME/.db_add"
# Create config template
cat >> "$HOME/.db_add" <<EOCONFIG
#!/usr/bin/env bash
# User that can create databases and grant privileges
USER="username"
PASS="userpass"
# Generic database user
DB_USER="db_user"
EOCONFIG
echo "Setup MySQL access and default user config file:"
echo "$HOME/.db_add"
exit 1
else
source "$HOME/.db_add"
fi
# Were the variables in the config file?
if [[ -z "$USER" || -z "$PASS" || -z "$DB_USER" ]]; then
echo "Config check failed:"
echo "$HOME/.db_add"
exit 1
fi
# Check if the host is up
nc -z $2 3306 > /dev/null
if [ $? -ne 0 ]
then
echo "Can't connect to host: $2"
exit 1
fi
# Test credentials
mysql -u$USER -p$PASS -h$2 -e "SHOW DATABASES;" > /dev/null
if [ $? -ne 0 ]; then
# mysql error should be printed on screen by stderr
exit 1
fi
# Lets do this
mysql -u$USER -p$PASS -h$2 -e "CREATE DATABASE $1 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"
mysql -u$USER -p$PASS -h$2 -e "GRANT ALL ON $1.* TO '$DB_USER'@'$DB_HOST';"
mysql -u$USER -p$PASS -h$2 -e "FLUSH PRIVILEGES;"
echo "Database $1 created on $2."
#!/usr/bin/env bash
#
# db_drop
# Delete a database and revoke privileges from user in config file
#
if [[ $# -ne 3 ]]
then
echo "usage: db_drop <db-name> <db-server> <@host>"
echo "example 1 connect from localhost: db_drop \"db-name\" \"db-server\" \"localhost\""
echo "example 2 connect from any host: db_drop \"db-name\" \"db-server\" \"%\""
exit 1
fi
CONFIGPATH="$HOME/.db_add"
# default to localhost
DB_HOST="localhost"
# Are we using a host other than the default?
if [ -z "$3" ]; then
$DB_HOST=$3
fi
# load config
if [ ! -f "$CONFIGPATH" ]; then
echo "Config file does not exist $CONFIGPATH"
echo "run db_add to generate config file"
exit 1
else
source "$CONFIGPATH"
fi
# Were the variables in the config file?
if [[ -z "$USER" || -z "$PASS" || -z "$DB_USER" ]]; then
echo "Config check failed:"
echo "$CONFIGPATH"
exit 1
fi
# Check if the host is up
nc -z $2 3306 > /dev/null
if [ $? -ne 0 ]; then
echo "Can't connect to host: $2"
exit 1
fi
# Test credentials
mysql -u$USER -p$PASS -h$2 -e "SHOW DATABASES;" > /dev/null
if [ $? -ne 0 ]; then
# mysql error should be printed on screen by stderr
exit 1
fi
# Lets do this
mysql -u$USER -p$PASS -h$2 -e "REVOKE ALL ON $1.* FROM '$DB_USER'@'$DB_HOST';"
mysql -u$USER -p$PASS -h$2 -e "FLUSH PRIVILEGES;"
mysql -u$USER -p$PASS -h$2 -e "DROP DATABASE $1;"
if [[ $? -eq 0 ]]; then
echo "Database $1 dropped on $2."
else
echo "Nothing to drop for $1 on $2."
fi
#!/usr/bin/env bash
#
# vhost_add
# A script to configure a new virtual host
#
PREFIX=""
CONFIGPATH="$HOME/.vhost_add"
if [[ $# -lt 2 ]] ;then
echo "usage: $0 <hostname> <ip-address>"
echo "i.e.: $0 \"site.dev\" \"127.0.0.1\""
echo "note: ip-address is for the /etc/hosts file"
exit
fi
# load config
if [ ! -f "$CONFIGPATH" ]; then
echo "Config file does not exist creating $CONFIGPATH"
touch "$CONFIGPATH"
# Create config template
cat >> "$CONFIGPATH" <<EOCONFIG
#!/usr/bin/env bash
# The path to your sites folder, all sites will be created under this prefix
PREFIX=""
EOCONFIG
echo "Config file created please setup your sites prefix:"
echo "$CONFIGPATH"
exit 1
else
source "$CONFIGPATH"
fi
# Check for root priveldges
if [ -z "$SUDO_USER" ]; then
echo "Must run as root"
exit
fi
if [ -z "$PREFIX" ]; then
echo "PREFIX not set in $CONFIGPATH"
exit 1
fi
# Strip trailing slash from PREFIX, assume not using "/" as prefix
PREFIX=${PREFIX%/}
# Make sure the directory isn't already there
if [ -d "$PREFIX/$1" ]; then
echo "The directory $PREFIX/$1 already exists."
exit 1
fi
# Strip extension for folder names
FOLDER=${1/.*/}
# Create directory under Sites
mkdir -p "$PREFIX/$FOLDER/public"
echo "<html><title>Hello $1</title><body><h2>Hello $1</h2></body></html>" > "$PREFIX/$FOLDER/public/index.html"
# Change owner/group
OWNER=$(echo $HOME | cut -d / -f 3)
GROUP=$(groups $OWNER | cut -d ' ' -f 1)
chown -R $OWNER:$GROUP "$PREFIX/$FOLDER"
# Add entry to hosts file
echo $2$'\t'$1 >> /etc/hosts
# Add virtual host to apache hosts file
cat >> /private/etc/apache2/extra/httpd-vhosts.conf <<EOF
# $1
<VirtualHost *:80>
ServerName $1
DocumentRoot "$PREFIX/$FOLDER/public"
</VirtualHost>
EOF
# Restart Apache
apachectl graceful
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment