-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
393 lines (319 loc) · 13.9 KB
/
docker-entrypoint.sh
File metadata and controls
393 lines (319 loc) · 13.9 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
#!/bin/bash
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
log_important() {
echo -e "${BLUE}[IMPORTANT]${NC} $1"
}
# Function to download MyBB
download_mybb() {
local version=$1
local download_url="https://github.com/mybb/mybb/releases/download/mybb_${version}/mybb_${version}.zip"
log_info "Downloading MyBB version ${version}..."
# Download MyBB
if ! wget -q --show-progress -O /tmp/mybb.zip "$download_url"; then
log_error "Failed to download MyBB version ${version}"
log_info "Trying alternative URL format..."
# Try alternative URL format (some versions use different naming)
download_url="https://resources.mybb.com/downloads/mybb_${version}.zip"
if ! wget -q --show-progress -O /tmp/mybb.zip "$download_url"; then
log_error "Failed to download MyBB. Please check if version ${version} exists."
log_info "Available versions: https://github.com/mybb/mybb/releases"
return 1
fi
fi
return 0
}
# Function to extract MyBB to a target directory
extract_mybb() {
local target_dir=$1
log_info "Extracting MyBB..."
# Extract to temporary directory
rm -rf /tmp/mybb_extract
unzip -q /tmp/mybb.zip -d /tmp/mybb_extract
# Find the Upload directory (MyBB packages contain Upload folder)
if [ -d "/tmp/mybb_extract/Upload" ]; then
cp -r /tmp/mybb_extract/Upload/* "$target_dir/"
elif [ -d "/tmp/mybb_extract/upload" ]; then
cp -r /tmp/mybb_extract/upload/* "$target_dir/"
else
# Some versions might extract directly
cp -r /tmp/mybb_extract/*/* "$target_dir/" 2>/dev/null || cp -r /tmp/mybb_extract/* "$target_dir/"
fi
# Cleanup
rm -rf /tmp/mybb.zip /tmp/mybb_extract
log_info "MyBB extracted successfully"
}
# Function to install MyBB (fresh install)
install_mybb() {
extract_mybb "/var/www/html"
}
# Function to create backup before upgrade
create_backup() {
local backup_dir="/var/www/html/admin/backups"
local timestamp=$(date +%Y%m%d_%H%M%S)
local backup_name="pre_upgrade_${timestamp}"
log_info "Creating backup before upgrade..."
mkdir -p "$backup_dir"
# Backup critical configuration files
if [ -f /var/www/html/inc/config.php ]; then
cp /var/www/html/inc/config.php "$backup_dir/${backup_name}_config.php"
log_info "Backed up: config.php"
fi
if [ -f /var/www/html/inc/settings.php ]; then
cp /var/www/html/inc/settings.php "$backup_dir/${backup_name}_settings.php"
log_info "Backed up: settings.php"
fi
# Create a list of installed plugins
if [ -d /var/www/html/inc/plugins ]; then
ls -1 /var/www/html/inc/plugins/ > "$backup_dir/${backup_name}_plugins_list.txt" 2>/dev/null || true
log_info "Backed up: plugins list"
fi
# Backup custom themes list
if [ -d /var/www/html/images ]; then
ls -1 /var/www/html/images/ > "$backup_dir/${backup_name}_themes_list.txt" 2>/dev/null || true
log_info "Backed up: themes list"
fi
# Store current version info if available
if [ -f /var/www/html/inc/class_core.php ]; then
grep -o "public \$version = '[^']*'" /var/www/html/inc/class_core.php > "$backup_dir/${backup_name}_version.txt" 2>/dev/null || true
fi
log_info "Backup created in: $backup_dir"
echo "$backup_dir/${backup_name}" > /tmp/last_backup_path
}
# Function to perform safe upgrade
upgrade_mybb() {
local target_version=$1
log_info "=========================================="
log_info " MyBB SAFE UPGRADE MODE"
log_info "=========================================="
log_info "Target version: ${target_version}"
# Check if MyBB is actually installed
if [ ! -f /var/www/html/inc/config.php ]; then
log_error "No existing MyBB installation found!"
log_error "Cannot upgrade - config.php is missing."
log_error "Use normal install mode instead (remove UPGRADE_MODE)."
return 1
fi
# Check if config.php has database settings (not empty template)
if ! grep -q "database" /var/www/html/inc/config.php 2>/dev/null; then
log_error "config.php appears to be empty or unconfigured."
log_error "Cannot upgrade an unconfigured installation."
return 1
fi
# Step 1: Create backup
log_info ""
log_info "Step 1/5: Creating backup..."
create_backup
# Step 2: Save files that must be preserved
log_info ""
log_info "Step 2/5: Preserving critical files..."
mkdir -p /tmp/mybb_preserve
# Preserve config.php (CRITICAL - contains database settings)
cp /var/www/html/inc/config.php /tmp/mybb_preserve/config.php
log_info "Preserved: inc/config.php"
# Preserve settings.php if it exists
if [ -f /var/www/html/inc/settings.php ]; then
cp /var/www/html/inc/settings.php /tmp/mybb_preserve/settings.php
log_info "Preserved: inc/settings.php"
fi
# Preserve uploads directory (user avatars, attachments)
if [ -d /var/www/html/uploads ]; then
cp -r /var/www/html/uploads /tmp/mybb_preserve/uploads
log_info "Preserved: uploads/ directory"
fi
# Preserve custom plugins (optional - admin may want fresh plugins)
if [ "${PRESERVE_PLUGINS:-true}" = "true" ] && [ -d /var/www/html/inc/plugins ]; then
cp -r /var/www/html/inc/plugins /tmp/mybb_preserve/plugins
log_info "Preserved: inc/plugins/ directory"
fi
# Preserve custom themes/images
if [ "${PRESERVE_THEMES:-true}" = "true" ] && [ -d /var/www/html/images ]; then
cp -r /var/www/html/images /tmp/mybb_preserve/images
log_info "Preserved: images/ directory"
fi
# Preserve language customizations
if [ -d /var/www/html/inc/languages ]; then
cp -r /var/www/html/inc/languages /tmp/mybb_preserve/languages
log_info "Preserved: inc/languages/ directory"
fi
# Step 3: Download new version
log_info ""
log_info "Step 3/5: Downloading MyBB ${target_version}..."
if ! download_mybb "${target_version}"; then
log_error "Download failed! Aborting upgrade."
log_info "Your current installation is unchanged."
rm -rf /tmp/mybb_preserve
return 1
fi
# Step 4: Extract new version (overwrite core files)
log_info ""
log_info "Step 4/5: Installing new version..."
extract_mybb "/var/www/html"
# Step 5: Restore preserved files
log_info ""
log_info "Step 5/5: Restoring preserved files..."
# Restore config.php (CRITICAL)
cp /tmp/mybb_preserve/config.php /var/www/html/inc/config.php
log_info "Restored: inc/config.php"
# Restore settings.php
if [ -f /tmp/mybb_preserve/settings.php ]; then
cp /tmp/mybb_preserve/settings.php /var/www/html/inc/settings.php
log_info "Restored: inc/settings.php"
fi
# Restore uploads
if [ -d /tmp/mybb_preserve/uploads ]; then
rm -rf /var/www/html/uploads
cp -r /tmp/mybb_preserve/uploads /var/www/html/uploads
log_info "Restored: uploads/ directory"
fi
# Restore plugins if preserved
if [ -d /tmp/mybb_preserve/plugins ]; then
# Merge plugins - keep new core plugins, restore custom ones
cp -rn /tmp/mybb_preserve/plugins/* /var/www/html/inc/plugins/ 2>/dev/null || true
log_info "Restored: custom plugins"
fi
# Restore themes/images if preserved
if [ -d /tmp/mybb_preserve/images ]; then
# Merge - keep new default images, restore custom themes
cp -rn /tmp/mybb_preserve/images/* /var/www/html/images/ 2>/dev/null || true
log_info "Restored: custom themes/images"
fi
# Restore languages
if [ -d /tmp/mybb_preserve/languages ]; then
cp -rn /tmp/mybb_preserve/languages/* /var/www/html/inc/languages/ 2>/dev/null || true
log_info "Restored: language files"
fi
# Cleanup
rm -rf /tmp/mybb_preserve
# Set permissions
set_permissions
# Success message
log_info ""
log_info "=========================================="
log_info " UPGRADE FILES INSTALLED SUCCESSFULLY"
log_info "=========================================="
log_important ""
log_important "╔════════════════════════════════════════════════════════════╗"
log_important "║ ACTION REQUIRED: Complete the upgrade! ║"
log_important "╠════════════════════════════════════════════════════════════╣"
log_important "║ ║"
log_important "║ 1. Visit: http://your-server/install/upgrade.php ║"
log_important "║ 2. Follow the upgrade wizard ║"
log_important "║ 3. Delete install folder when complete: ║"
log_important "║ docker exec mybb-forum rm -rf /var/www/html/install ║"
log_important "║ ║"
log_important "╚════════════════════════════════════════════════════════════╝"
log_important ""
# Remove the installer lock file so upgrade wizard can run
if [ -f /var/www/html/install/lock ]; then
rm -f /var/www/html/install/lock
log_info "Removed install/lock file for upgrade wizard"
fi
# Create a flag file to remind about upgrade
echo "Upgrade to version ${target_version} started at $(date)" > /var/www/html/UPGRADE_IN_PROGRESS.txt
echo "Visit /install/upgrade.php to complete the upgrade" >> /var/www/html/UPGRADE_IN_PROGRESS.txt
echo "Delete this file after completing the upgrade" >> /var/www/html/UPGRADE_IN_PROGRESS.txt
return 0
}
# Function to set permissions
set_permissions() {
log_info "Setting file permissions..."
# Set ownership
chown -R www-data:www-data /var/www/html
# Set directory permissions
find /var/www/html -type d -exec chmod 755 {} \;
# Set file permissions
find /var/www/html -type f -exec chmod 644 {} \;
# Make specific directories writable (required by MyBB)
chmod -R 777 /var/www/html/cache 2>/dev/null || true
chmod -R 777 /var/www/html/uploads 2>/dev/null || true
chmod -R 777 /var/www/html/inc/settings.php 2>/dev/null || true
chmod -R 777 /var/www/html/inc/config.php 2>/dev/null || true
chmod -R 777 /var/www/html/admin/backups 2>/dev/null || true
# Create config.php if it doesn't exist (for installer)
if [ ! -f /var/www/html/inc/config.php ]; then
touch /var/www/html/inc/config.php
chmod 666 /var/www/html/inc/config.php
chown www-data:www-data /var/www/html/inc/config.php
fi
log_info "Permissions set successfully"
}
# Main execution
main() {
log_info "Starting MyBB Docker container..."
log_info "MyBB Version: ${MYBB_VERSION}"
# Check for upgrade mode FIRST
if [ "${UPGRADE_MODE:-false}" = "true" ]; then
log_warn "UPGRADE MODE ENABLED"
if upgrade_mybb "${MYBB_VERSION}"; then
log_info "Upgrade preparation complete."
else
log_error "Upgrade failed!"
exit 1
fi
# Check for fresh install or forced reinstall
elif [ ! -f /var/www/html/index.php ] || [ "${FORCE_REINSTALL:-false}" = "true" ]; then
if [ "${FORCE_REINSTALL:-false}" = "true" ]; then
log_warn "FORCE REINSTALL enabled - this will overwrite existing files!"
fi
log_info "Installing MyBB..."
# Download and install MyBB
if download_mybb "${MYBB_VERSION}"; then
install_mybb
set_permissions
log_info "MyBB installation complete!"
log_info "Please visit http://your-server/install/ to complete the setup."
else
log_error "MyBB installation failed!"
exit 1
fi
else
log_info "MyBB already installed. Skipping download."
# Check for upgrade reminder
if [ -f /var/www/html/UPGRADE_IN_PROGRESS.txt ]; then
log_warn "=========================================="
log_warn " UPGRADE IN PROGRESS - ACTION REQUIRED"
log_warn "=========================================="
cat /var/www/html/UPGRADE_IN_PROGRESS.txt
log_warn "=========================================="
fi
# Still set permissions in case of volume mount issues
set_permissions
fi
# Wait for database if DB_HOST is set
if [ -n "${DB_HOST}" ]; then
log_info "Waiting for database at ${DB_HOST}:${DB_PORT:-3306}..."
max_tries=30
counter=0
while ! php -r "new mysqli('${DB_HOST}', '${DB_USER:-root}', '${DB_PASSWORD:-}', '', ${DB_PORT:-3306});" 2>/dev/null; do
counter=$((counter + 1))
if [ $counter -ge $max_tries ]; then
log_warn "Could not connect to database after ${max_tries} attempts. Continuing anyway..."
break
fi
log_info "Database not ready. Waiting... (${counter}/${max_tries})"
sleep 2
done
if [ $counter -lt $max_tries ]; then
log_info "Database is ready!"
fi
fi
log_info "Starting Apache..."
# Execute the main command
exec "$@"
}
main "$@"