-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-restore.sh
More file actions
executable file
·508 lines (440 loc) · 15.1 KB
/
Copy pathwp-restore.sh
File metadata and controls
executable file
·508 lines (440 loc) · 15.1 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
#!/bin/bash
set -euo pipefail
# === WORDPRESS RESTORE SCRIPT ===
# This script restores WordPress sites from backups
# Run this script from the multi-wordpress-deployment directory
# === CONFIGURATION ===
# Load environment variables if .env file exists
if [ -f ".env" ]; then
source .env
fi
# Restore configuration
BACKUP_DIR="/var/backups/wordpress"
LOG_FILE="./wp-restore.log" # Local log file to avoid permission issues
WORDPRESS_SITES=("wp1" "wp2" "wp3" "wp4" "wp5")
# === LOGGING FUNCTION ===
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
# === ERROR HANDLING ===
error_exit() {
log "ERROR: $1"
exit 1
}
# === HELPER FUNCTIONS ===
# Function to list available backups
list_backups() {
local site="$1"
echo "Available backups for $site:"
echo "================================"
if [ -d "$BACKUP_DIR" ]; then
find "$BACKUP_DIR" -name "wordpress_${site}_*.tar.gz" -type f | sort -r | head -10 | while read -r backup; do
backup_name=$(basename "$backup")
backup_date=$(echo "$backup_name" | sed "s/wordpress_${site}_\(.*\)\.tar\.gz/\1/")
backup_size=$(du -sh "$backup" | cut -f1)
echo " $backup_date ($backup_size)"
done
else
echo " No backup directory found: $BACKUP_DIR"
fi
echo
}
# Function to get latest backup for a site
get_latest_backup() {
local site="$1"
find "$BACKUP_DIR" -name "wordpress_${site}_*.tar.gz" -type f | sort -r | head -1
}
# Function to validate backup file
validate_backup() {
local backup_file="$1"
if [ ! -f "$backup_file" ]; then
error_exit "Backup file not found: $backup_file"
fi
if [ ! -r "$backup_file" ]; then
error_exit "Cannot read backup file: $backup_file"
fi
# Check if it's a valid tar.gz file
if ! tar -tzf "$backup_file" >/dev/null 2>&1; then
error_exit "Invalid backup file format: $backup_file"
fi
log "✅ Backup file validated: $backup_file"
}
# Function to restore WordPress site files
restore_wp_files() {
local site="$1"
local backup_file="$2"
local target_dir="wp/$site"
log "Restoring WordPress site: $site from $backup_file"
# Create target directory
mkdir -p "$target_dir"
# Create temporary directory for extraction
local temp_dir="/tmp/wp_restore_${site}_$(date +%s)"
mkdir -p "$temp_dir"
# Extract backup to temporary directory
log "Extracting backup to temporary directory..."
if tar -xzf "$backup_file" -C "$temp_dir"; then
log "✅ Backup extracted successfully"
else
error_exit "Failed to extract backup file"
fi
# Restore plugins
if [ -d "$temp_dir/plugins" ]; then
log "Restoring plugins..."
if [ -d "$target_dir/plugins" ]; then
rm -rf "$target_dir/plugins"
fi
mv "$temp_dir/plugins" "$target_dir/"
log "✅ Plugins restored"
else
log "⚠️ No plugins found in backup"
fi
# Restore themes
if [ -d "$temp_dir/themes" ]; then
log "Restoring themes..."
if [ -d "$target_dir/themes" ]; then
rm -rf "$target_dir/themes"
fi
mv "$temp_dir/themes" "$target_dir/"
log "✅ Themes restored"
else
log "⚠️ No themes found in backup"
fi
# Restore uploads
if [ -d "$temp_dir/uploads" ]; then
log "Restoring uploads..."
if [ -d "$target_dir/uploads" ]; then
rm -rf "$target_dir/uploads"
fi
mv "$temp_dir/uploads" "$target_dir/"
log "✅ Uploads restored"
else
log "⚠️ No uploads found in backup"
fi
# Set proper permissions
log "Setting proper permissions..."
chmod -R 755 "$target_dir"
find "$target_dir" -type f -exec chmod 644 {} \;
# Clean up temporary directory
rm -rf "$temp_dir"
log "✅ WordPress site $site restored successfully"
}
# Function to restore database
restore_database() {
local site="$1"
log "Restoring MySQL database for $site"
# Check if MySQL container is running
if ! docker ps --format "{{.Names}}" | grep -q "^mysql$"; then
error_exit "MySQL container is not running"
fi
# Map site to database name
local db_name
case "$site" in
wp1) db_name="${WP1_DB_NAME:-wordpress1}" ;;
wp2) db_name="${WP2_DB_NAME:-wordpress2}" ;;
wp3) db_name="${WP3_DB_NAME:-wordpress3}" ;;
wp4) db_name="${WP4_DB_NAME:-wordpress4}" ;;
wp5) db_name="${WP5_DB_NAME:-wordpress5}" ;;
*) error_exit "Unknown site: $site" ;;
esac
# Find the latest database backup for this specific database
local db_backup=$(find "$BACKUP_DIR/mysql" -name "${db_name}_*.sql.gz" -type f | sort -r | head -1)
if [ -z "$db_backup" ]; then
error_exit "No database backup found for $db_name in $BACKUP_DIR/mysql"
fi
log "Using database backup: $(basename "$db_backup")"
# Get MySQL root password
MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD:-}
if [ -z "$MYSQL_ROOT_PASSWORD" ]; then
read -s -p "Enter MySQL root password: " MYSQL_ROOT_PASSWORD
echo
fi
# Copy backup to MySQL container
local container_backup="/tmp/restore_$(basename "$db_backup")"
docker cp "$db_backup" "mysql:$container_backup"
# Restore database
log "Restoring database $db_name..."
if docker exec mysql sh -c "gunzip -c '$container_backup' | mysql -uroot -p'$MYSQL_ROOT_PASSWORD'"; then
log "✅ Database $db_name restored successfully"
else
error_exit "Failed to restore database $db_name"
fi
# Clean up container backup
docker exec mysql rm -f "$container_backup"
}
# Function to restore full database backup
restore_full_database() {
log "Restoring full MySQL database backup (all databases)"
# Check if MySQL container is running
if ! docker ps --format "{{.Names}}" | grep -q "^mysql$"; then
error_exit "MySQL container is not running"
fi
# Find the latest full database backup
local db_backup=$(find "$BACKUP_DIR/mysql" -name "all_databases_*.sql.gz" -type f | sort -r | head -1)
if [ -z "$db_backup" ]; then
error_exit "No full database backup found in $BACKUP_DIR/mysql"
fi
log "Using full database backup: $(basename "$db_backup")"
# Get MySQL root password
MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD:-}
if [ -z "$MYSQL_ROOT_PASSWORD" ]; then
read -s -p "Enter MySQL root password: " MYSQL_ROOT_PASSWORD
echo
fi
# Copy backup to MySQL container
local container_backup="/tmp/restore_$(basename "$db_backup")"
docker cp "$db_backup" "mysql:$container_backup"
# Restore all databases
log "Restoring all databases..."
if docker exec mysql sh -c "gunzip -c '$container_backup' | mysql -uroot -p'$MYSQL_ROOT_PASSWORD'"; then
log "✅ All databases restored successfully"
else
error_exit "Failed to restore all databases"
fi
# Clean up container backup
docker exec mysql rm -f "$container_backup"
}
# Function to restart WordPress container
restart_wordpress_container() {
local site="$1"
local container_name="wordpress${site#wp}"
log "Restarting WordPress container: $container_name"
if docker restart "$container_name"; then
log "✅ WordPress container restarted successfully"
else
log "⚠️ Failed to restart WordPress container"
fi
}
# Function to show restore summary
show_restore_summary() {
local site="$1"
local backup_file="$2"
local restore_type="$3"
echo
echo "=== RESTORE SUMMARY ==="
echo "Site: $site"
echo "Backup: $(basename "$backup_file")"
echo "Backup Date: $(stat -c %y "$backup_file" | cut -d' ' -f1)"
echo "Backup Size: $(du -sh "$backup_file" | cut -f1)"
echo "Restore Type: $restore_type"
echo "Restore Time: $(date)"
echo
}
# === INTERACTIVE FUNCTIONS ===
# Function to select WordPress site
select_wordpress_site() {
echo "Available WordPress sites:"
echo "========================="
for i in "${!WORDPRESS_SITES[@]}"; do
echo "$((i+1)). ${WORDPRESS_SITES[$i]}"
done
echo
while true; do
read -p "Select WordPress site (1-${#WORDPRESS_SITES[@]}): " choice
if [[ "$choice" =~ ^[0-9]+$ ]] && [ "$choice" -ge 1 ] && [ "$choice" -le "${#WORDPRESS_SITES[@]}" ]; then
selected_site="${WORDPRESS_SITES[$((choice-1))]}"
break
else
echo "Invalid selection. Please enter a number between 1 and ${#WORDPRESS_SITES[@]}"
fi
done
echo "Selected site: $selected_site"
echo
}
# Function to select backup
select_backup() {
local site="$1"
echo "Backup selection options:"
echo "========================"
echo "1. Use latest backup"
echo "2. Select specific backup"
echo "3. List available backups"
echo
while true; do
read -p "Choose option (1-3): " option
case $option in
1)
selected_backup=$(get_latest_backup "$site")
if [ -n "$selected_backup" ]; then
echo "Selected latest backup: $(basename "$selected_backup")"
break
else
echo "No backups found for $site"
exit 1
fi
;;
2)
list_backups "$site"
read -p "Enter backup date (YYYYMMDD_HHMMSS): " backup_date
selected_backup="$BACKUP_DIR/wordpress_${site}_${backup_date}.tar.gz"
if [ -f "$selected_backup" ]; then
echo "Selected backup: $(basename "$selected_backup")"
break
else
echo "Backup not found: $selected_backup"
fi
;;
3)
list_backups "$site"
;;
*)
echo "Invalid option. Please enter 1, 2, or 3"
;;
esac
done
echo
}
# Function to select restore type
select_restore_type() {
echo "Restore type options:"
echo "===================="
echo "1. Files only (plugins, themes, uploads)"
echo "2. Database only (specific site)"
echo "3. Files and database (specific site)"
echo "4. Full database restore (all databases)"
echo
while true; do
read -p "Choose restore type (1-4): " restore_type
case $restore_type in
1)
selected_restore_type="files"
break
;;
2)
selected_restore_type="database"
break
;;
3)
selected_restore_type="full"
break
;;
4)
selected_restore_type="full-db"
break
;;
*)
echo "Invalid option. Please enter 1, 2, 3, or 4"
;;
esac
done
echo "Selected restore type: $selected_restore_type"
echo
}
# === MAIN RESTORE FUNCTION ===
main_restore() {
local site="$1"
local backup_file="$2"
local restore_type="$3"
log "Starting restore process for $site"
log "Backup file: $backup_file"
log "Restore type: $restore_type"
# Validate backup file
validate_backup "$backup_file"
# Perform restore based on type
case $restore_type in
"files")
restore_wp_files "$site" "$backup_file"
restart_wordpress_container "$site"
;;
"database")
restore_database "$site"
;;
"full")
restore_wp_files "$site" "$backup_file"
restore_database "$site"
restart_wordpress_container "$site"
;;
"full-db")
restore_full_database
restart_wordpress_container "$site"
;;
esac
show_restore_summary "$site" "$backup_file" "$restore_type"
log "Restore process completed successfully"
}
# === COMMAND LINE INTERFACE ===
if [ $# -eq 0 ]; then
# Interactive mode
echo "=== WordPress Restore Script ==="
echo "Interactive restore mode"
echo
select_wordpress_site
select_backup "$selected_site"
select_restore_type
# Confirm restore
echo "Restore confirmation:"
echo "==================="
echo "Site: $selected_site"
echo "Backup: $(basename "$selected_backup")"
echo "Type: $selected_restore_type"
echo
read -p "Proceed with restore? (y/N): " confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
main_restore "$selected_site" "$selected_backup" "$selected_restore_type"
else
echo "Restore cancelled"
exit 0
fi
else
# Command line mode
case $1 in
"list")
if [ $# -eq 2 ]; then
list_backups "$2"
else
echo "Usage: $0 list <site>"
echo "Example: $0 list wp1"
fi
;;
"latest")
if [ $# -eq 2 ]; then
latest_backup=$(get_latest_backup "$2")
if [ -n "$latest_backup" ]; then
echo "$latest_backup"
else
echo "No backups found for $2"
exit 1
fi
else
echo "Usage: $0 latest <site>"
echo "Example: $0 latest wp1"
fi
;;
"restore")
if [ $# -eq 4 ]; then
main_restore "$2" "$3" "$4"
else
echo "Usage: $0 restore <site> <backup_file> <restore_type>"
echo "Example: $0 restore wp1 /var/backups/wordpress/wordpress_wp1_20251026_223848.tar.gz files"
echo "Restore types: files, database, full"
fi
;;
"help"|"-h"|"--help")
echo "WordPress Restore Script"
echo "========================"
echo
echo "Interactive mode:"
echo " $0"
echo
echo "Command line mode:"
echo " $0 list <site> - List available backups for site"
echo " $0 latest <site> - Get latest backup file for site"
echo " $0 restore <site> <backup> <type> - Restore site from backup"
echo
echo "Restore types:"
echo " files - Restore plugins, themes, uploads only"
echo " database - Restore database only (specific site)"
echo " full - Restore files and database (specific site)"
echo " full-db - Restore all databases (complete system restore)"
echo
echo "Examples:"
echo " $0 list wp1"
echo " $0 latest wp1"
echo " $0 restore wp1 /var/backups/wordpress/wordpress_wp1_20251026_223848.tar.gz files"
;;
*)
echo "Unknown command: $1"
echo "Use '$0 help' for usage information"
exit 1
;;
esac
fi