-
Notifications
You must be signed in to change notification settings - Fork 2
/
mage-dbpurge.sh
executable file
·71 lines (51 loc) · 1.97 KB
/
mage-dbpurge.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/bash
CONFIG_FILE="./app/etc/local.xml"
TMP_FILE="./var/.tmp.local.xml"
DUMP_STRING=""
if [ ! -f "$CONFIG_FILE" ]; then
echo "$CONFIG_FILE does not exist"
exit
fi
sed -ne '/default_setup/,/\/default_setup/p' $CONFIG_FILE > $TMP_FILE
function getParam()
{
RETVAL=$(grep -Eoh "<$1>(<!\[CDATA\[)?(.*)(\]\]>)?<\/$1>" $TMP_FILE | sed "s#<$1><!\[CDATA\[##g;s#\]\]><\/$1>##g")
if [[ "$2" == "sanitise" ]]; then
RETVAL=$(echo "$RETVAL" | sed 's/"/\\\"/g')
fi
echo -e "$RETVAL"
}
DBHOST=$(getParam "host")
DBUSER=$(getParam "username")
DBPASS=$(getParam "password" "sanitise" )
DBNAME=$(getParam "dbname")
TABLE_PREFIX=$(getParam "table_prefix")
[ -f $TMP_FILE ] && rm $TMP_FILE
echo "The database selected is: $DBNAME"
echo -n "Are you sure you want to purge? [y/N]: "
read CONFIRM; if [[ ! "$CONFIRM" == "y" ]]; then echo "You dodged a bullet there!"; exit; fi
echo -n "Last chance, are you 110% sure you want to purge $DBNAME? [y/N]: "
read CONFIRM; if [[ ! "$CONFIRM" == "y" ]]; then echo "You dodged a bullet there!"; exit; fi
TABLES=( $(mysql -h$DBHOST -u$DBUSER -p"$DBPASS" $DBNAME -e 'show tables' | awk '{ print $1}' | grep -v '^Tables' ) )
for TABLE in ${TABLES[@]}; do
DUMP_STRING="SET FOREIGN_KEY_CHECKS=0; DROP VIEW $TABLE; SET FOREIGN_KEY_CHECKS=1;"
mysql --force -h"$DBHOST" -u"$DBUSER" -p"$DBPASS" $DBNAME -e "$DUMP_STRING"
DUMP_STRING="SET FOREIGN_KEY_CHECKS=0; DROP TABLE $TABLE; SET FOREIGN_KEY_CHECKS=1;"
mysql --force -h"$DBHOST" -u"$DBUSER" -p"$DBPASS" $DBNAME -e "$DUMP_STRING"
done
cat <<EOT
#######################################
MYSQL DB PURGE COMPLETE
#######################################
EOT
cat <<EOT
########################################
(_)
___ ___ _ __ __ _ ___ ___ _
/ __|/ _ \| '_ \ / _' / __/ __| |
\__ \ (_) | | | | (_| \__ \__ \ |
|___/\___/|_| |_|\__'_|___/___/_|
Want truly optimised Magento hosting?
Try http://www.sonassihosting.com ...
#########################################
EOT