-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnginx-staging-remove.sh
53 lines (42 loc) · 1.47 KB
/
nginx-staging-remove.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env bash
# Purpose: WordPress staging for nginx
# Source: https://guides.wp-bullet.com
# Adapted
# Author: Mike
MYSQLROOTPASS=
NGINXSITEPATH=/etc/nginx/sites-available
NGINXSITESENABLED=/etc/nginx/sites-enabled
SITEPATH=/var/www
SITELIST=($(ls -lh $NGINXSITEPATH | awk '{print $9}'))
#generate hash based on date and use first 8 characters for subdomain
#capture first parameter
VHOST="$1"
if [ -z "$VHOST" ]; then
echo "What is the name of your virtual host?"
echo "Virtual hosts found:\n ${SITELIST[@]} "
read VHOST
fi
if [ ! -f $NGINXSITEPATH/$VHOST ]; then
echo "$VHOST not found"
exit
fi
VHOSTPATH=$NGINXSITEPATH/$VHOST
EXTRACTEDPATH=$(grep "root " $VHOSTPATH | awk '{print $2}' | tr -d ";")
# server_name without www
EXTRACTEDDOMAIN=$(grep server_name $VHOSTPATH | awk '{ gsub("www\.", ""); print $2 }' | tr -d ";")
#extract database information
DB=$(grep DB_NAME $EXTRACTEDPATH/wp-config.php | awk -F ["\'"] '{ print $4 }')
DBUSER=$(grep DB_USER $EXTRACTEDPATH/wp-config.php | awk -F ["\'"] '{ print $4 }')
#create new db user, pass
mysql -u root -p${MYSQLROOTPASS} -e "DROP USER ${DBUSER}@localhost;"
mysql -u root -p${MYSQLROOTPASS} -e "DROP DATABASE ${DB};"
mysql -u root -p${MYSQLROOTPASS} -e "FLUSH PRIVILEGES;"
#remove nginx virtualhost
unlink $SITESENABLED/$VHOST
rm $VHOSTPATH
rm -rf $EXTRACTEDPATH
#reload nginx
service nginx reload
#print results
echo "Staging domain is $EXTRACTEDDOMAIN removed"
echo "Staging path is $EXTRACTEDPATH removed"