-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_database.sh
104 lines (101 loc) · 3.79 KB
/
delete_database.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
#set -x
clear
#--------------------------------------------------------------------
# By Keld Norman, Aug 2020
#--------------------------------------------------------------------
MYSQL_PASSWORD="toor" # Your database password
#--------------------------------------------------------------------
# BANNER for the 1337'ish ness
#--------------------------------------------------------------------
cat << "EOF"
^ ^
| |
+ THE
| DANISH
A CELLTOWER
=== GPS POSITION
/EEE\ RETRIEVER
//EEE\\ SCRIPT
__//_____\\___ 2000 DATABASE DELETER.....
EOF
#--------------------------------------------------------------------
# PATH TO UTILITIES
#--------------------------------------------------------------------
MYSQL_CLIENT="/usr/bin/mysql"
MYSQL_INIT_SCRIPT="/etc/init.d/mysql"
#--------------------------------------------------------------------
# VARIABLES
#--------------------------------------------------------------------
TOWER_DATA_FILE="/tmp/antenner.json"
TOWER_DATA_GPS_COORDINATES_FILE="/tmp/antenner.gps"
SOURCE_DIRECTORY_FOR_CROCODILEHUNTER="./crocodilehunter/src"
#--------------------------------------------------------------------
# CHECK IF MYSQL IS INSTALLED AND RUNNING
#--------------------------------------------------------------------
printf " - Checking if MySQL is running..\n"
if [ ! -x ${MYSQL_CLIENT} ]; then
printf "\n ### ERROR - I cant find the utility called ${MYSQL_CLIENT}\n\n Please alter the path to it in this script\n\n"
exit
fi
if [ ! -f ${MYSQL_INIT_SCRIPT} ]; then
printf "\n ### ERROR - I cant find the mysql server start/stop utility ${MYSQL_INIT_SCRIPT}\n\n Please alter the path to it in this script\n\n"
exit
fi
${MYSQL_INIT_SCRIPT} status 2>&1 | grep -q "stopped\|dead"
if [ $? -eq 0 ]; then
printf " - ### WARNING - MySQL not running - attempting to start it..\n"
${MYSQL_INIT_SCRIPT} start > /dev/null 2>&1
${MYSQL_INIT_SCRIPT} status 2>&1 | grep -q "stopped\|dead"
if [ $? -eq 0 ]; then
printf "\n ### ERROR - Starting MySQL failed - exiting script\n\n"
exit
fi
fi
#--------------------------------------------------------------------
function select_database {
#--------------------------------------------------------------------
printf " - Retrieving a list of used databases..\n"
DATABASES=$(${MYSQL_CLIENT} -p${MYSQL_PASSWORD} --disable-column-names -e "show databases;"|egrep -v -e "information_schema|mysql|performance_schema")
COUNT_DATABASES=$(echo "${DATABASES}"|wc -w)
if [ ${COUNT_DATABASES} -eq 0 ]; then
printf " - ### ERROR - No databases found to delete!\n\n"
exit
fi
if [ ${COUNT_DATABASES} -eq 1 ]; then
DATABASE=${DATABASES}
read -p " Only one database found: \"${DATABASE}\" do you want to delete it (y/n)?: \n" -n 1 -r
if [[ ! $REPLY =~ ^[Nn]$ ]]; then
exit 1
fi
else
printf "\n Found ${COUNT_DATABASES} database(s)\n\n"
PS3="
Please select the database you want to delete: "
select SELECT_DATABASE in ${DATABASES}; do
if [ "${SELECT_DATABASE:-empty}" != "empty" ]; then
DATABASE="${SELECT_DATABASE}"
break
else
echo -e "\033[2A "
fi
done
fi
printf "\n - Database selected: \"${DATABASE}\"\n"
}
#--------------------------------------------------------------------
function delete_database {
#--------------------------------------------------------------------
printf " - Deleting database..\n\n"
${MYSQL_CLIENT} -p${MYSQL_PASSWORD} -e "drop database ${DATABASE};"
if [ $? -ne 0 ]; then
printf "\n ### ERROR - Failed to delete database ${DATABASE} !\n\n"
else
printf " ### SUCCESS: Database ${DATABASE} deleted.\n\n"
fi
}
#--------------------------------------------------------------------
# MAIN
#--------------------------------------------------------------------
select_database
delete_database