-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsc.sh
More file actions
509 lines (456 loc) · 17.8 KB
/
sc.sh
File metadata and controls
509 lines (456 loc) · 17.8 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#!/bin/bash
# =============================================================================
# DATABASE SERVER SETUP SCRIPT - SNAP DOCKER
# Tüm database sunucuları için tek script
# =============================================================================
set -e
# RENK KODLARI
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
log() { echo -e "${BLUE}[INFO]${NC} $1"; }
success() { echo -e "${GREEN}[✓]${NC} $1"; }
error() { echo -e "${RED}[✗]${NC} $1"; exit 1; }
warning() { echo -e "${YELLOW}[!]${NC} $1"; }
# =============================================================================
# KONFIGÜRASYON - BURADAN DEĞİŞTİR
# =============================================================================
SERVER_IP="${1:-$(hostname -I | awk '{print $1}')}"
SERVER_NAME="${2:-Database}"
# .env'den oku ya da ortam değişkeni olarak ver:
# DB_PASSWORD=... ./sc.sh
DB_PASSWORD="${DB_PASSWORD:?DB_PASSWORD env var veya .env zorunlu}"
NGINX_USER="${NGINX_USER:-root}"
NGINX_PASS="${NGINX_PASS:-${DB_PASSWORD}}"
echo ""
echo -e "${GREEN}================================================${NC}"
echo -e "${GREEN} DATABASE SERVER SETUP - SNAP DOCKER${NC}"
echo -e "${GREEN}================================================${NC}"
echo ""
echo "Server IP: $SERVER_IP"
echo "Server Name: $SERVER_NAME"
echo "DB Password: $DB_PASSWORD"
echo "Nginx Auth: $NGINX_USER / $NGINX_PASS"
echo ""
read -p "Devam edilsin mi? (yes/no): " confirm
if [ "$confirm" != "yes" ]; then
echo "İptal edildi."
exit 0
fi
# =============================================================================
# 1. DİZİNLERİ OLUŞTUR
# =============================================================================
log "Dizinler oluşturuluyor..."
mkdir -p /opt/databases/mariadb/config
mkdir -p /opt/databases/nginx/html
mkdir -p /opt/databases/pgadmin
mkdir -p /opt/databases/logs
mkdir -p /opt/databases/backups
mkdir -p /opt/databases/backups/mariadb/full
mkdir -p /opt/databases/backups/postgresql/full
mkdir -p /opt/databases/backups/mongodb/full
mkdir -p /opt/databases/backups/redis/full
mkdir -p /opt/databases/redisinsight-data
success "Dizinler oluşturuldu"
# =============================================================================
# 2. MARIADB CONFIG
# =============================================================================
log "MariaDB config oluşturuluyor..."
cat > /opt/databases/mariadb/config/my.cnf << 'EOF'
[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
[client]
default-character-set=utf8mb4
EOF
success "MariaDB config hazır"
# =============================================================================
# 3. NGINX CONFIG
# =============================================================================
log "Nginx config oluşturuluyor..."
cat > /opt/databases/nginx/nginx.conf << 'EOF'
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript application/javascript application/xml+rss application/json;
server {
listen 80;
server_name _;
auth_basic "Database Administration Panel";
auth_basic_user_file /etc/nginx/.htpasswd;
location / {
root /usr/share/nginx/html;
index index.html;
try_files $uri $uri/ =404;
}
location /health {
auth_basic off;
access_log off;
return 200 "healthy\n";
add_header Content-Type text/plain;
}
location ~* \.(css|js|jpg|jpeg|png|gif|ico|svg)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
}
}
EOF
success "Nginx config hazır"
# =============================================================================
# 4. NGINX INDEX.HTML (DASHBOARD)
# =============================================================================
log "Dashboard oluşturuluyor..."
cat > /opt/databases/nginx/html/index.html << EOF
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${SERVER_NAME} Database Administration Panel</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
background: rgba(255, 255, 255, 0.95);
border-radius: 15px;
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
overflow: hidden;
}
.header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 30px;
text-align: center;
}
.header h1 { font-size: 2.5em; margin-bottom: 10px; }
.header p { font-size: 1.1em; opacity: 0.9; }
.main-content { padding: 40px; }
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 25px;
margin-top: 30px;
}
.card {
background: white;
border: 1px solid #e1e8ed;
border-radius: 12px;
padding: 25px;
text-align: center;
transition: all 0.3s ease;
position: relative;
overflow: hidden;
}
.card::before {
content: '';
position: absolute;
top: 0; left: 0; right: 0;
height: 4px;
background: linear-gradient(90deg, #667eea, #764ba2);
}
.card:hover {
transform: translateY(-5px);
box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
}
.card-icon { font-size: 3em; margin-bottom: 15px; }
.card h3 { color: #2c3e50; margin-bottom: 10px; font-size: 1.4em; }
.card p { color: #7f8c8d; margin-bottom: 20px; line-height: 1.6; }
.card a {
display: inline-block;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 12px 25px;
text-decoration: none;
border-radius: 25px;
font-weight: 600;
transition: all 0.3s ease;
text-transform: uppercase;
letter-spacing: 0.5px;
font-size: 0.9em;
}
.card a:hover {
transform: scale(1.05);
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}
.status {
margin-top: 15px;
padding: 5px 12px;
border-radius: 15px;
font-size: 0.8em;
font-weight: 600;
background: #d4edda;
color: #155724;
}
.info-section {
background: #f8f9fa;
padding: 30px;
margin-top: 40px;
border-radius: 10px;
border-left: 5px solid #667eea;
}
.info-section h3 { color: #2c3e50; margin-bottom: 15px; }
.connection-info {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
margin-top: 20px;
}
.connection-card {
background: white;
padding: 20px;
border-radius: 8px;
border: 1px solid #dee2e6;
}
.connection-card h4 { color: #495057; margin-bottom: 10px; }
.connection-details {
font-family: 'Courier New', monospace;
font-size: 0.9em;
color: #6c757d;
line-height: 1.8;
}
.welcome-message {
background: #e7f3ff;
padding: 20px;
margin-bottom: 30px;
border-radius: 8px;
border-left: 4px solid #0066cc;
}
.welcome-message h3 { color: #0066cc; margin-bottom: 10px; }
.welcome-message p { color: #495057; margin: 0; }
@media (max-width: 768px) {
.header h1 { font-size: 2em; }
.main-content { padding: 20px; }
.grid { grid-template-columns: 1fr; }
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🗄️ ${SERVER_NAME} Database Administration</h1>
<p>Centralized Database Management Panel</p>
</div>
<div class="main-content">
<div class="welcome-message">
<h3>🎉 Hoş Geldiniz!</h3>
<p>Tüm veritabanı yönetim araçlarınıza buradan güvenli şekilde erişebilirsiniz.</p>
</div>
<div class="grid">
<div class="card">
<div class="card-icon">📊</div>
<h3>MariaDB</h3>
<p>MySQL/MariaDB Database Management with phpMyAdmin</p>
<a href="http://${SERVER_IP}:8081" target="_blank">Open phpMyAdmin</a>
<div class="status">● Online</div>
</div>
<div class="card">
<div class="card-icon">🐘</div>
<h3>PostgreSQL</h3>
<p>Advanced PostgreSQL database administration via pgAdmin</p>
<a href="http://${SERVER_IP}:8082" target="_blank">Open pgAdmin</a>
<div class="status">● Online</div>
</div>
<div class="card">
<div class="card-icon">🍃</div>
<h3>MongoDB</h3>
<p>NoSQL document database management with Mongo Express</p>
<a href="http://${SERVER_IP}:8083" target="_blank">Open Mongo Express</a>
<div class="status">● Online</div>
</div>
<div class="card">
<div class="card-icon">🔴</div>
<h3>Redis</h3>
<p>In-memory data structure store management</p>
<a href="http://${SERVER_IP}:8084" target="_blank">Open Redis Commander</a>
<div class="status">● Online</div>
</div>
<div class="card">
<div class="card-icon">🔧</div>
<h3>Universal Admin</h3>
<p>Multi-database administration tool</p>
<a href="http://${SERVER_IP}:8085" target="_blank">Open Adminer</a>
<div class="status">● Online</div>
</div>
<div class="card">
<div class="card-icon">📈</div>
<h3>Health Monitor</h3>
<p>Database server health monitoring</p>
<a href="/health" target="_blank">Check Health</a>
<div class="status">● Monitoring</div>
</div>
</div>
<div class="info-section">
<h3>📋 Database Connection Information</h3>
<p>Use these connection details for your applications:</p>
<div class="connection-info">
<div class="connection-card">
<h4>MariaDB</h4>
<div class="connection-details">
Host: ${SERVER_IP}<br>
Port: 3306<br>
User: root / appuser<br>
DB: defaultdb
</div>
</div>
<div class="connection-card">
<h4>PostgreSQL</h4>
<div class="connection-details">
Host: ${SERVER_IP}<br>
Port: 5432<br>
User: root / appuser<br>
DB: defaultdb
</div>
</div>
<div class="connection-card">
<h4>MongoDB</h4>
<div class="connection-details">
Host: ${SERVER_IP}<br>
Port: 27017<br>
User: root / appuser<br>
Auth DB: admin
</div>
</div>
<div class="connection-card">
<h4>Redis</h4>
<div class="connection-details">
Host: ${SERVER_IP}<br>
Port: 6379<br>
User: default / appuser
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
EOF
success "Dashboard hazır"
# =============================================================================
# 5. PGADMIN SERVERS.JSON
# =============================================================================
log "pgAdmin config oluşturuluyor..."
cat > /opt/databases/pgadmin_servers.json << 'EOF'
{
"Servers": {
"1": {
"Name": "PostgreSQL",
"Group": "Servers",
"Host": "postgresql",
"Port": 5432,
"MaintenanceDB": "postgres",
"Username": "root",
"SSLMode": "prefer"
}
}
}
EOF
success "pgAdmin config hazır"
# =============================================================================
# 6. İZİNLERİ DÜZELT
# =============================================================================
log "İzinler düzeltiliyor..."
chown -R 5050:5050 /opt/databases/pgadmin
chmod 755 /opt/databases/logs
chmod 755 /opt/databases/backups
chmod 644 /opt/databases/nginx/nginx.conf
chmod 755 /opt/databases/nginx/html
chmod 644 /opt/databases/nginx/html/index.html
chown -R 1001:1001 /opt/databases/redisinsight-data
chmod +x /opt/databases/*.sh 2>/dev/null || true
success "İzinler düzeltildi"
# =============================================================================
# 7. DOCKER COMPOSE DURDUR
# =============================================================================
log "Docker Compose durduruluyor..."
cd /opt/databases 2>/dev/null || true
docker-compose down --remove-orphans 2>/dev/null || true
success "Docker Compose durduruldu"
# =============================================================================
# 8. SNAP DİZİNİNE TAŞI
# =============================================================================
log "Snap dizinine taşınıyor..."
mkdir -p /var/snap/docker/common/databases
cp -r /opt/databases/* /var/snap/docker/common/databases/
cd /var/snap/docker/common/databases
success "Snap dizinine taşındı"
# =============================================================================
# 9. HTPASSWD OLUŞTUR (DOCKER İLE)
# =============================================================================
log "Nginx kullanıcıları oluşturuluyor..."
# htpasswd dosyasını docker ile oluştur — APP_PASSWORD env var üzerinden
docker run --rm httpd:alpine htpasswd -nb "${NGINX_USER}" "${NGINX_PASS}" > /var/snap/docker/common/databases/nginx/.htpasswd
docker run --rm httpd:alpine htpasswd -nb appuser "${APP_PASSWORD:?APP_PASSWORD env var zorunlu}" >> /var/snap/docker/common/databases/nginx/.htpasswd
chmod 644 /var/snap/docker/common/databases/nginx/.htpasswd
success "Nginx kullanıcıları oluşturuldu (root, appuser)"
# =============================================================================
# 10. İZİNLERİ TEKRAR DÜZELT
# =============================================================================
log "Son izin düzenlemeleri..."
chown -R root:root /var/snap/docker/common/databases/
chown -R 5050:5050 /var/snap/docker/common/databases/pgadmin
chmod 755 /var/snap/docker/common/databases/nginx
chmod 755 /var/snap/docker/common/databases/nginx/html
chmod 644 /var/snap/docker/common/databases/nginx/html/index.html
chown -R 1001:1001 /opt/databases/redisinsight-data
success "İzinler tamamlandı"
# =============================================================================
# 11. DOCKER COMPOSE BAŞLAT
# =============================================================================
log "Docker Compose başlatılıyor..."
cd /var/snap/docker/common/databases
docker-compose up -d
success "Docker Compose başlatıldı"
# =============================================================================
# 12. KONTROL
# =============================================================================
echo ""
log "Container durumları kontrol ediliyor..."
sleep 5
docker-compose ps
# =============================================================================
# 13. SONUÇ
# =============================================================================
echo ""
echo -e "${GREEN}================================================${NC}"
echo -e "${GREEN} KURULUM TAMAMLANDI!${NC}"
echo -e "${GREEN}================================================${NC}"
echo ""
echo "Dashboard URL: http://${SERVER_IP}"
echo ""
echo "Nginx Giriş Bilgileri:"
echo " - root / ${DB_PASSWORD}"
echo " - appuser / [APP_PASSWORD env var değeri]"
echo ""
echo "Database Panelleri:"
echo " - phpMyAdmin: http://${SERVER_IP}:8081"
echo " - pgAdmin: http://${SERVER_IP}:8082"
echo " - Mongo Express: http://${SERVER_IP}:8083"
echo " - Redis Insight: http://${SERVER_IP}:8084"
echo " - Adminer: http://${SERVER_IP}:8085"
echo ""
echo "Sonraki Adımlar:"
echo " 1. ./setup_db_users.sh # Kısıtlı kullanıcı oluştur"
echo " 2. crontab crontab.txt # Backup cron'u yükle"
echo " 3. ./backup.sh stats # Backup durumunu kontrol et"
echo ""
echo -e "${GREEN}================================================${NC}"