-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreset-databases.sh
More file actions
executable file
·167 lines (144 loc) · 3.24 KB
/
Copy pathreset-databases.sh
File metadata and controls
executable file
·167 lines (144 loc) · 3.24 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/bash
set -e
usage() {
cat <<'USAGE'
Usage:
./reset-databases.sh [all|mysql|mariadb|postgre|postgres] [...]
Examples:
./reset-databases.sh
./reset-databases.sh mysql
./reset-databases.sh mariadb postgre
./reset-databases.sh all
USAGE
}
# Force sudo to ask password
sudo -k
echo "========================================="
echo " Docker Database RESET Utility"
echo "========================================="
if [ ! -f .env ]; then
echo "❌ .env not found. Run from project root."
exit 1
fi
set -a
source .env
set +a
RESET_MYSQL=false
RESET_MARIADB=false
RESET_POSTGRES=false
if [ "$#" -eq 0 ]; then
RESET_MYSQL=true
RESET_MARIADB=true
RESET_POSTGRES=true
else
for target in "$@"; do
case "$target" in
all)
RESET_MYSQL=true
RESET_MARIADB=true
RESET_POSTGRES=true
;;
mysql)
RESET_MYSQL=true
;;
mariadb)
RESET_MARIADB=true
;;
postgre|postgres)
RESET_POSTGRES=true
;;
-h|--help|help)
usage
exit 0
;;
*)
echo "❌ Unknown target: $target"
usage
exit 1
;;
esac
done
fi
if [ "$RESET_MYSQL" = false ] && [ "$RESET_MARIADB" = false ] && [ "$RESET_POSTGRES" = false ]; then
echo "❌ Nothing selected to reset"
usage
exit 1
fi
echo
echo "⚠️ THIS WILL DELETE DATABASE DATA"
echo
if [ "$RESET_MYSQL" = true ]; then
echo "MySQL Data Path: ${MYSQL_DATA_PATH}"
fi
if [ "$RESET_MARIADB" = true ]; then
echo "MariaDB Data Path: ${MARIADB_DATA_PATH}"
fi
if [ "$RESET_POSTGRES" = true ]; then
echo "PostgreSQL Data: ${POSTGRES_DATA_PATH}"
fi
echo
read -p "Type 'RESET' to continue: " CONFIRM
[ "$CONFIRM" = "RESET" ] || exit 0
SERVICES=()
if [ "$RESET_MYSQL" = true ]; then
SERVICES+=("mysql")
fi
if [ "$RESET_MARIADB" = true ]; then
SERVICES+=("mariadb")
fi
if [ "$RESET_POSTGRES" = true ]; then
SERVICES+=("postgres" "pgbouncer")
fi
echo
echo "🛑 Stopping selected containers..."
docker compose stop "${SERVICES[@]}"
reset_dir() {
local dir="$1"
if [ -d "$dir" ]; then
echo "🔥 Removing $dir"
sudo rm -rf "$dir"
sudo mkdir -p "$dir"
else
echo "📁 Creating $dir"
sudo mkdir -p "$dir"
fi
}
if [ "$RESET_MYSQL" = true ]; then
reset_dir "$MYSQL_DATA_PATH"
fi
if [ "$RESET_MARIADB" = true ]; then
reset_dir "$MARIADB_DATA_PATH"
fi
if [ "$RESET_POSTGRES" = true ]; then
reset_dir "$POSTGRES_DATA_PATH"
fi
echo
echo "🔐 Fixing ownership..."
if [ "$RESET_MYSQL" = true ]; then
sudo chown -R 999:999 "$(dirname "$MYSQL_DATA_PATH")"
fi
if [ "$RESET_MARIADB" = true ]; then
sudo chown -R 999:999 "$(dirname "$MARIADB_DATA_PATH")"
fi
if [ "$RESET_POSTGRES" = true ]; then
sudo chown -R 999:999 "$(dirname "$POSTGRES_DATA_PATH")"
fi
echo
echo "🔐 Setting directory permissions..."
if [ "$RESET_MYSQL" = true ]; then
sudo chmod 750 "$MYSQL_DATA_PATH"
fi
if [ "$RESET_MARIADB" = true ]; then
sudo chmod 750 "$MARIADB_DATA_PATH"
fi
if [ "$RESET_POSTGRES" = true ]; then
sudo chmod 700 "$POSTGRES_DATA_PATH"
fi
echo
echo "🚀 Starting selected containers..."
docker compose up -d "${SERVICES[@]}"
echo
echo "✅ RESET COMPLETE"
if [ "$RESET_POSTGRES" = true ]; then
echo "ℹ️ PostgreSQL init scripts WILL run again"
fi