Last active
January 5, 2018 22:36
-
-
Save Grayest/25041cd6e33a4536a6908c94872fd439 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
read -p "Enter your Site Name to be created (include .com):" SITENAME | |
#1. Create apache2 site and it's folders | |
mkdir /var/www/html/$SITENAME | |
mkdir /var/www/html/$SITENAME/public_html/ | |
chmod -R 755 /var/www/html/$SITENAME/public_html/ | |
cat > /etc/apache2/sites-available/$SITENAME.conf << EOF | |
<VirtualHost *:80> | |
ServerAdmin admin@$SITENAME | |
ServerName $SITENAME | |
ServerAlias www.$SITENAME | |
DocumentRoot /var/www/html/$SITENAME/public_html/ | |
<Directory /> | |
Options FollowSymLinks | |
AllowOverride None | |
</Directory> | |
<Directory /var/www/html/$SITENAME.com/public_html/> | |
Options Indexes FollowSymLinks MultiViews | |
AllowOverride All | |
Order allow,deny | |
allow from all | |
</Directory> | |
</Virtualhost> | |
EOF | |
a2ensite $SITENAME | |
service apache2 reload | |
service apache2 restart | |
echo "Apache site creation finished." | |
#2. Copy Wordpress Site | |
# ============================================================ | |
# CONFIGURATION GOES HERE | |
# ============================================================ | |
SOURCE_SITE='blog-fimo.com'; | |
SOURCE_FOLDER='/var/www/html/blog-fimo.com/public_html/'; | |
SOURCE_DATABASE='blog-fimo'; | |
SOURCE_DATABASE_USER='root'; | |
SOURCE_DATABASE_PASS='root'; | |
# this is not using /tmp/ because some databases are larger than /tmp partition | |
TEMP_FILE='/var/www/html/blog-fimo.com/wpdb.sql'; | |
DESTINATION_SITE=$SITENAME; | |
DESTINATION_FOLDER='/var/www/html/'$SITENAME'/public_html/'; | |
DBNAME=`echo $SITENAME | cut --delimiter=. --fields=1 --output-delimiter=/`; | |
DESTINATION_DATABASE=$DBNAME; | |
DESTINATION_DATABASE_USER='root'; | |
DESTINATION_DATABASE_PASS='root'; | |
DESTINATION_CHOWN_USER_GROUP='www-data'; | |
# you may want to lock .htaccess down with "DENY FROM ALL" | |
# you can add an easy exception - such as ALLOW FROM my.own.hostname.net | |
#ADD_TO_HTACCESS=' | |
#deny from all | |
#allow from my.intranet.connection | |
#'; | |
# ============================================================ | |
# END CONFIGURATION - DON'T TOUCH BELOW THIS LINE | |
# ============================================================ | |
IFS=$'\n'; | |
# ============================================================ | |
# step 1: clear destination folder and database | |
# ============================================================ | |
echo "removing / recreating ${DESTINATION_FOLDER}"; | |
#rm -rf ${DESTINATION_FOLDER} | |
#mkdir ${DESTINATION_FOLDER} | |
mysql -u${DESTINATION_DATABASE_USER} -p${DESTINATION_DATABASE_PASS} -e "CREATE DATABASE ${DESTINATION_DATABASE}" | |
tables=\ | |
`echo "show tables from ${DESTINATION_DATABASE}" | \ | |
mysql -u${DESTINATION_DATABASE_USER} -p${DESTINATION_DATABASE_PASS} | \ | |
grep -v "Tables_in_${DESTINATION_DATABASE}"` | |
IFS=$' '; | |
count_tables=`echo ${tables} | cat -n | tail -n 1 | cut -f1`; | |
IFS=$'\n'; | |
echo "removing ${count_tables} tables from destination database ${DESTINATION_DATABASE}"; | |
for table in $tables | |
do | |
echo \ | |
"drop table ${DESTINATION_DATABASE}.${table}" | \ | |
mysql -u${DESTINATION_DATABASE_USER} -p${DESTINATION_DATABASE_PASS}; | |
done | |
# ============================================================ | |
# step 2: import database and files | |
# ============================================================ | |
echo "grabbing snapshot of source database from ${SOURCE_DATABASE}"; | |
/usr/bin/mysqldump \ | |
${SOURCE_DATABASE} \ | |
-u${SOURCE_DATABASE_USER} \ | |
-p${SOURCE_DATABASE_PASS} \ | |
> ${TEMP_FILE} | |
echo `stat ${TEMP_FILE} -c "%n %s"` " bytes"; | |
echo "importing snapshot into destination database ${DESTINATION_DATABASE}" | |
mysql -D \ | |
${DESTINATION_DATABASE} \ | |
-u${DESTINATION_DATABASE_USER} \ | |
-p${DESTINATION_DATABASE_PASS} \ | |
< ${TEMP_FILE} | |
echo "copying files from ${SOURCE_FOLDER}* to ${DESTINATION_FOLDER}"; | |
cp -R -p ${SOURCE_FOLDER}* ${DESTINATION_FOLDER} | |
echo "chown'ing ${DESTINATION_FOLDER} as ${DESTINATION_CHOWN_USER_GROUP}"; | |
chown -R ${DESTINATION_CHOWN_USER_GROUP} ${DESTINATION_FOLDER}* | |
echo "copying .htaccess manually"; | |
cp ${SOURCE_FOLDER}.htaccess ${DESTINATION_FOLDER}.htaccess | |
#echo " adding to .htaccess: ${ADD_TO_HTACCESS}"; | |
#echo "${ADD_TO_HTACCESS}" >> ${DESTINATION_FOLDER}.htaccess | |
# ============================================================ | |
# step 3: disable caching plugins, if any | |
# ============================================================ | |
if [ -d ${DESTINATION_FOLDER}wp-content/plugins/wp-super-cache/ ]; then | |
echo "removing caching plugin ${DESTINATION_FOLDER}wp-content/plugins/wp-super-cache/"; | |
rm -rf ${DESTINATION_FOLDER}wp-content/plugins/wp-super-cache/ | |
fi | |
# ============================================================ | |
# step 4: rewrite wp-config.php | |
# ============================================================ | |
echo "rewriting wp-config.php with new information"; | |
sed \ | |
"s/'DB_NAME', '${SOURCE_DATABASE}'/'DB_NAME', '${DESTINATION_DATABASE}'/g" \ | |
${SOURCE_FOLDER}wp-config.php | \ | |
sed \ | |
"s/'DB_USER', '${SOURCE_DATABASE_USER}'/'DB_USER', '${DESTINATION_DATABASE_USER}'/g" | \ | |
sed \ | |
"s/'DB_PASSWORD', '${SOURCE_DATABASE_PASS}'/'DB_PASSWORD', '${DESTINATION_DATABASE_PASS}'/g" | \ | |
sed \ | |
"s/'DOMAIN_CURRENT_SITE', '${SOURCE_SITE}'/'DOMAIN_CURRENT_SITE', '${DESTINATION_SITE}'/g" \ | |
> ${DESTINATION_FOLDER}wp-config.php | |
# ============================================================ | |
# step 5: update destination database to reflect new URL | |
# ============================================================ | |
#echo "changing mu blogs tables: source site->destination site"; | |
#echo "update ${DESTINATION_DATABASE}.wp_blogs set domain='${DESTINATION_SITE}'" \ | |
# | mysql -u${DESTINATION_DATABASE_USER} -p${DESTINATION_DATABASE_PASS}; | |
#echo "update ${DESTINATION_DATABASE}.wp_site set domain='${DESTINATION_SITE}'" \ | |
# | mysql -u${DESTINATION_DATABASE_USER} -p${DESTINATION_DATABASE_PASS}; | |
#echo \ | |
#"update | |
# ${DESTINATION_DATABASE}.wp_sitemeta | |
#set | |
# meta_value='http://${DESTINATION_SITE}' | |
#where | |
# meta_key='siteurl'" \ | |
# | mysql \ | |
# -u${DESTINATION_DATABASE_USER} \ | |
# -p${DESTINATION_DATABASE_PASS}; | |
echo "changing mu options tables: source site->destination site"; | |
echo "update ${DESTINATION_DATABASE}.wp_options set option_value=replace( | |
option_value, | |
'http://${SOURCE_SITE}/', | |
'http://${DESTINATION_SITE}/' | |
) | |
where | |
option_name in | |
('siteurl', 'home', 'fileupload_url')" \ | |
| mysql \ | |
-u${DESTINATION_DATABASE_USER} \ | |
-p${DESTINATION_DATABASE_PASS}; | |
#tables=`echo "show tables from ${DESTINATION_DATABASE}" \ | |
# | mysql -u${DESTINATION_DATABASE_USER} \ | |
# -p${DESTINATION_DATABASE_PASS} \ | |
# | grep "_options"` | |
#for table in $tables | |
#do | |
# echo \ | |
#"update | |
## ${DESTINATION_DATABASE}.${table} | |
#set | |
# option_value=replace( | |
# option_value, | |
# 'http://${SOURCE_SITE}/', | |
# 'http://${DESTINATION_SITE}/' | |
# ) | |
#where | |
# option_name in | |
# ('siteurl', 'home', 'fileupload_url')" \ | |
# | mysql \ | |
# -u${DESTINATION_DATABASE_USER} \ | |
# -p${DESTINATION_DATABASE_PASS}; | |
#done | |
#3.Update route53 of AWS | |
## Allows for creation of "Basic" DNS records in a Route53 hosted zone | |
IP="34.236.115.119" | |
aws route53 create-hosted-zone --name $SITENAME --caller-reference 2014-04-01-18:47 --hosted-zone-config Comment="created from script" | |
function main() { | |
record_name=$SITENAME. | |
record_value=$IP | |
[[ -z $record_name ]] && echo "record_name is: $record_name" && exit 1 | |
[[ -z $record_value ]] && echo "record_value is: $record_value" && exit 1 | |
## set some defaults if variables haven't been overridden on script execute | |
zone_name=${zone_name:-$SITENAME.} | |
action=${action:-CREATE} | |
record_type=${record_type:-A} | |
ttl=${ttl:-60} | |
wait_for_sync=${wait_for_sync:-false} | |
change_id=$(submit_resource_record_change_set) || exit 1 | |
echo "Record change submitted! Change Id: $change_id" | |
if $wait_for_sync; then | |
echo -n "Waiting for all Route53 DNS to be in sync..." | |
until [[ $(get_change_status $change_id) == "INSYNC" ]]; do | |
echo -n "." | |
sleep 5 | |
done | |
echo "!" | |
echo "Your record change has now propogated." | |
fi | |
echo 'Thank you for using "The Cloud".' | |
} | |
function change_batch() { | |
jq -c -n "{\"Changes\": [{\"Action\": \"$action\", \"ResourceRecordSet\": {\"Name\": \"$record_name\", \"Type\": \"$record_type\", \"TTL\": $ttl, \"ResourceRecords\": [{\"Value\": \"$record_value\"} ] } } ] }" | |
} | |
function get_change_status() { | |
aws route53 get-change --id $1 | jq -r '.ChangeInfo.Status' | |
} | |
function hosted_zone_id() { | |
aws route53 list-hosted-zones | jq -r ".HostedZones[] | select(.Name == \"${zone_name}\") | .Id" | cut -d'/' -f3 | |
} | |
function submit_resource_record_change_set() { | |
aws route53 change-resource-record-sets --hosted-zone-id $(hosted_zone_id) --change-batch $(change_batch) | jq -r '.ChangeInfo.Id' | cut -d'/' -f3 | |
} | |
function usage() { | |
echo "usage: $0 <record_name> <record_value>" | |
echo "" | |
echo "possible env config settings and their defaults:" | |
echo " - action=CREATE" | |
echo " - ttl=300" | |
echo " - record_type=A" | |
echo " - wait_for_sync=false" | |
echo "" | |
} | |
main $SITENAME $IP |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment