-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreporting.sh
633 lines (559 loc) · 20.7 KB
/
reporting.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
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
#!/bin/bash
# reporting.sh - Reporting functions for backup-webdev
# This file contains reporting-related functions used across scripts
# Source the shared utilities
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/utils.sh"
# Create a backup report
create_backup_report() {
local backup_dir=$1
local successful=$2
local failed=$3
local src_size=$4
local backup_size=$5
local start_time=$6
local end_time=$7
local backup_type=${8:-"full"}
local report_file=${9:-"$backup_dir/backup_report.html"}
# Calculate duration
local duration=$(( $(date -d "$end_time" +%s) - $(date -d "$start_time" +%s) ))
local duration_str=$(printf "%02d:%02d:%02d" $(($duration/3600)) $(($duration%3600/60)) $(($duration%60)))
# Create HTML report
cat > "$report_file" << EOF
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebDev Backup Report</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
margin: 0;
padding: 20px;
color: #333;
}
.container {
max-width: 800px;
margin: 0 auto;
background: #f9f9f9;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
h1 {
color: #2c3e50;
border-bottom: 2px solid #3498db;
padding-bottom: 10px;
}
h2 {
color: #2980b9;
}
.success {
color: #27ae60;
}
.failure {
color: #e74c3c;
}
.summary {
background: #ecf0f1;
padding: 15px;
border-radius: 5px;
margin-bottom: 20px;
}
table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
}
th, td {
padding: 12px 15px;
text-align: left;
border-bottom: 1px solid #ddd;
}
th {
background-color: #3498db;
color: white;
}
tr:hover {
background-color: #f5f5f5;
}
</style>
</head>
<body>
<div class="container">
<h1>WebDev Backup Report</h1>
<div class="summary">
<h2>Backup Summary</h2>
<p><strong>Date:</strong> $end_time</p>
<p><strong>Backup Type:</strong> ${backup_type^}</p>
<p><strong>Duration:</strong> $duration_str</p>
<p><strong>Projects Processed:</strong> $(($successful + $failed))</p>
<p><strong>Successfully Backed Up:</strong> <span class="success">$successful</span></p>
<p><strong>Failed Backups:</strong> <span class="failure">$failed</span></p>
<p><strong>Total Source Size:</strong> $(format_size "$src_size")</p>
<p><strong>Total Backup Size:</strong> $(format_size "$backup_size")</p>
EOF
# Add compression ratio if we have valid sizes
if [ "$backup_size" -gt 0 ] && [ "$src_size" -gt 0 ]; then
local ratio=$(awk "BEGIN {printf \"%.1f\", ($src_size/$backup_size)}")
echo "<p><strong>Overall Compression Ratio:</strong> ${ratio}x</p>" >> "$report_file"
fi
# Add backup location
echo "<p><strong>Backup Location:</strong> $backup_dir</p>" >> "$report_file"
echo "</div>" >> "$report_file"
# Add project details if stats file exists
local stats_file="$backup_dir/backup_stats.txt"
if [ -f "$stats_file" ]; then
echo "<h2>Project Details</h2>" >> "$report_file"
echo "<table>" >> "$report_file"
echo "<tr><th>Project</th><th>Source Dir</th><th>Source Size</th><th>Backup Size</th><th>Ratio</th></tr>" >> "$report_file"
while IFS=, read -r project src_dir src_size archive_size ratio; do
echo "<tr>" >> "$report_file"
echo "<td>$project</td>" >> "$report_file"
echo "<td>$src_dir</td>" >> "$report_file"
echo "<td>$(format_size "$src_size")</td>" >> "$report_file"
echo "<td>$(format_size "$archive_size")</td>" >> "$report_file"
echo "<td>${ratio}x</td>" >> "$report_file"
echo "</tr>" >> "$report_file"
done < "$stats_file"
echo "</table>" >> "$report_file"
fi
# Close HTML
cat >> "$report_file" << EOF
<footer>
<p>Generated by WebDev Backup Tool on $(date)</p>
</footer>
</div>
</body>
</html>
EOF
echo "$report_file"
}
# Create email report
create_email_report() {
local backup_dir=$1
local successful=$2
local failed=$3
local src_size=$4
local backup_size=$5
local start_time=$6
local end_time=$7
local backup_type=${8:-"full"}
# Calculate duration
local duration=$(( $(date -d "$end_time" +%s) - $(date -d "$start_time" +%s) ))
local duration_str=$(printf "%02d:%02d:%02d" $(($duration/3600)) $(($duration%3600/60)) $(($duration%60)))
# Create email content
local email_content="WebDev Backup Report - $(date '+%Y-%m-%d')\n"
email_content+="\n===== Backup Summary =====\n"
email_content+="Date: $end_time\n"
email_content+="Backup Type: ${backup_type^}\n"
email_content+="Duration: $duration_str\n"
email_content+="Projects Processed: $(($successful + $failed))\n"
email_content+="Successfully Backed Up: $successful\n"
email_content+="Failed Backups: $failed\n"
email_content+="Total Source Size: $(format_size "$src_size")\n"
email_content+="Total Backup Size: $(format_size "$backup_size")\n"
# Add compression ratio if we have valid sizes
if [ "$backup_size" -gt 0 ] && [ "$src_size" -gt 0 ]; then
local ratio=$(awk "BEGIN {printf \"%.1f\", ($src_size/$backup_size)}")
email_content+="Overall Compression Ratio: ${ratio}x\n"
fi
# Add backup location
email_content+="Backup Location: $backup_dir\n"
# Add status indication
if [ "$failed" -eq 0 ]; then
email_content+="\nStatus: SUCCESS - All projects backed up successfully.\n"
else
email_content+="\nStatus: WARNING - $failed projects failed to back up properly.\n"
email_content+="Please check the detailed report for more information.\n"
fi
echo -e "$email_content"
}
# Generate a backup history visualization
generate_history_chart() {
local history_log=$1
local output_file=$2
local max_entries=${3:-10}
# Ensure gnuplot is installed
if ! command -v gnuplot >/dev/null 2>&1; then
echo "Cannot create visualization - gnuplot not installed"
return 1
fi
# Create a temporary data file
local temp_data=$(mktemp)
# Extract dates and sizes
grep "Total Size:" "$history_log" | head -n "$max_entries" | \
sed -E 's/.*Total Size: ([0-9.]+) ([A-Z]+).*/\1 \2/' | \
awk '{
multiplier = 1;
if ($2 == "KB") multiplier = 1024;
else if ($2 == "MB") multiplier = 1024*1024;
else if ($2 == "GB") multiplier = 1024*1024*1024;
else if ($2 == "TB") multiplier = 1024*1024*1024*1024;
print NR, $1 * multiplier;
}' > "$temp_data"
# Create a gnuplot script
local gnuplot_script=$(mktemp)
cat > "$gnuplot_script" << EOF
set terminal png enhanced size 800,500
set output "$output_file"
set title "Backup Size History"
set xlabel "Backup Number (recent to oldest)"
set ylabel "Size (bytes)"
set grid
set style fill solid 0.5
set boxwidth 0.8
plot "$temp_data" using 1:2 with boxes title "Backup Size" linecolor rgb "#3498db"
EOF
# Run gnuplot
gnuplot "$gnuplot_script"
# Clean up temporary files
rm "$temp_data" "$gnuplot_script"
if [ -f "$output_file" ]; then
echo "$output_file"
return 0
else
echo "Failed to generate chart"
return 1
fi
}
# Create a simple default chart with an error message
create_default_chart() {
local chart_file=$1
local message=${2:-"No data available for chart"}
# Create a simple transparent PNG as placeholder
echo "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" | base64 -d > "$chart_file"
return 0
}
# Generate space forecast
generate_space_forecast_safe() {
local history_log=$1
local forecast_file=$2
local forecast_days=${3:-30}
# Check if gnuplot is available
if ! command -v gnuplot >/dev/null 2>&1; then
echo "Error: gnuplot is not installed. Cannot generate forecast."
create_default_chart "$forecast_file" "gnuplot not installed"
return 0
fi
# Ensure sufficient history exists (at least 2 entries)
local entry_count=$(grep -c "BACKUP:" "$history_log")
if [ "$entry_count" -lt 2 ]; then
echo "Insufficient backup history for forecasting (need at least 2 entries)"
create_default_chart "$forecast_file" "Need at least 2 backups for forecast"
return 0
fi
# Create a temporary data file
local temp_data=$(mktemp)
# Extract dates and sizes with error handling
grep -A2 "BACKUP:" "$history_log" | grep "Total Size:" | \
sed -E 's/.*Total Size: ([0-9.]+) ([A-Z]+).*/\1 \2/' | \
awk '{
if (NF < 2) {
# Skip invalid lines
next;
}
multiplier = 1;
if ($2 == "KB") multiplier = 1024;
else if ($2 == "MB") multiplier = 1024*1024;
else if ($2 == "GB") multiplier = 1024*1024*1024;
else if ($2 == "TB") multiplier = 1024*1024*1024*1024;
if ($1 ~ /^[0-9]+(\.[0-9]+)?$/) {
print NR, ($1 * multiplier);
}
}' > "$temp_data" 2>/dev/null
# Check if we have any valid data
if [ ! -s "$temp_data" ]; then
echo "No valid data for forecast chart"
create_default_chart "$forecast_file" "No valid size data found"
rm -f "$temp_data"
return 0
fi
# Calculate trend line using awk with error handling
local trend_data=$(mktemp)
awk '
BEGIN {
n = 0;
sum_x = 0;
sum_y = 0;
sum_xy = 0;
sum_xx = 0;
}
{
if (NF >= 2 && $1 ~ /^[0-9]+$/ && $2 ~ /^[0-9]+(\.[0-9]+)?$/) {
sum_x += $1;
sum_y += $2;
sum_xy += $1*$2;
sum_xx += $1*$1;
n++;
}
}
END {
if (n < 2) {
print "0 0";
exit;
}
denominator = (n*sum_xx - sum_x*sum_x);
# Avoid division by zero
if (denominator == 0) {
a = 0;
} else {
a = (n*sum_xy - sum_x*sum_y) / denominator;
}
# Avoid division by zero
if (n == 0) {
b = 0;
} else {
b = (sum_y - a*sum_x) / n;
}
# Output coefficients for forecast
print b, a;
}' "$temp_data" > "$trend_data" 2>/dev/null
# Get coefficients for report
local a=0
local b=0
if [ -s "$trend_data" ]; then
read b a < "$trend_data"
fi
# Validate coefficients
if ! [[ "$a" =~ ^-?[0-9]+(\.[0-9]+)?$ ]] || ! [[ "$b" =~ ^-?[0-9]+(\.[0-9]+)?$ ]]; then
a=0
b=0
fi
# Create a simple gnuplot script
local gnuplot_script=$(mktemp)
cat > "$gnuplot_script" << EOF
set terminal png enhanced size 800,500
set output "$forecast_file"
set title "Backup Size Forecast"
set xlabel "Backup Number"
set ylabel "Size (bytes)"
set grid
set key outside right top
set style line 1 lc rgb "#3498db" lt 1 lw 2 pt 7 ps 1.5
# Plot data points only first for safety
plot "$temp_data" using 1:2 with points ls 1 title "Actual Backup Sizes"
EOF
# Run gnuplot
gnuplot "$gnuplot_script"
# Clean up temporary files
rm "$temp_data" "$trend_data" "$gnuplot_script"
if [ -f "$forecast_file" ]; then
# Generate a text report
local forecast_report="${forecast_file%.png}_report.txt"
echo "Backup Size Forecast Report" > "$forecast_report"
echo "Generated: $(date)" >> "$forecast_report"
echo "Forecast Days: $forecast_days" >> "$forecast_report"
echo "Analysis based on $entry_count backup entries" >> "$forecast_report"
echo "" >> "$forecast_report"
# Calculate average growth per backup
if (( $(echo "$a > 0" | bc -l) )); then
echo "Trend: GROWING at approximately $(format_size $a) per backup" >> "$forecast_report"
elif (( $(echo "$a < 0" | bc -l) )); then
echo "Trend: SHRINKING at approximately $(format_size $(echo "$a * -1" | bc -l)) per backup" >> "$forecast_report"
else
echo "Trend: STABLE - no significant growth detected" >> "$forecast_report"
fi
# Generate some forecast points
echo "" >> "$forecast_report"
echo "Size Forecast:" >> "$forecast_report"
local current_size=$(tail -1 "$temp_data" | awk '{print $2}')
echo "Current backup size: $(format_size $current_size)" >> "$forecast_report"
local next_backup=$(echo "$a * ($entry_count + 1) + $b" | bc -l)
echo "Next backup (estimated): $(format_size $next_backup)" >> "$forecast_report"
local week_forecast=$(echo "$a * ($entry_count + 7) + $b" | bc -l)
echo "In 1 week (estimated): $(format_size $week_forecast)" >> "$forecast_report"
local month_forecast=$(echo "$a * ($entry_count + 30) + $b" | bc -l)
echo "In 1 month (estimated): $(format_size $month_forecast)" >> "$forecast_report"
echo "" >> "$forecast_report"
echo "Notes:" >> "$forecast_report"
echo "- This forecast is based on historical trends and actual growth may vary" >> "$forecast_report"
echo "- Storage requirements might increase if projects grow in size or new projects are added" >> "$forecast_report"
echo "- Consider implementing a backup rotation policy if storage is constrained" >> "$forecast_report"
echo "$forecast_file"
return 0
else
echo "Failed to generate forecast"
return 1
fi
}
# Create visual dashboard HTML with safe fallbacks
create_visual_dashboard() {
local output_dir=$1
local history_log=$2
local dashboard_file="${output_dir}/backup_dashboard.html"
# Create chart placeholder files
local history_chart="${output_dir}/backup_history_chart.png"
local forecast_chart="${output_dir}/backup_forecast_chart.png"
# Always create placeholder images first (to ensure we have something)
echo "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" | base64 -d > "$history_chart"
cp "$history_chart" "$forecast_chart"
# Skip gnuplot for safety - we'll use the simplified dashboard instead
# Charts can cause too many issues across different environments
# Get recent backup stats
local recent_backup=$(grep -A7 "BACKUP: SUCCESS" "$history_log" | head -7)
local backup_date=$(echo "$recent_backup" | grep -o "[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\} [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}")
local projects_count=$(echo "$recent_backup" | grep "Projects:" | grep -o "[0-9]* succeeded" | grep -o "[0-9]*")
local backup_size=$(echo "$recent_backup" | grep "Total Size:" | grep -o "[0-9.]* [A-Z]*")
# Create HTML dashboard
cat > "$dashboard_file" << EOF
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="refresh" content="300"> <!-- Refresh every 5 minutes -->
<title>WebDev Backup Dashboard</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
margin: 0;
padding: 20px;
color: #333;
background-color: #f5f5f5;
}
.container {
max-width: 1000px;
margin: 0 auto;
}
header {
background-color: #2c3e50;
color: white;
padding: 20px;
border-radius: 5px 5px 0 0;
text-align: center;
}
.dashboard-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px;
margin-top: 20px;
}
.card {
background: white;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
padding: 20px;
}
.card h2 {
color: #2980b9;
margin-top: 0;
border-bottom: 2px solid #3498db;
padding-bottom: 10px;
}
.stats {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 10px;
}
.stat-card {
background: #ecf0f1;
padding: 15px;
border-radius: 5px;
text-align: center;
}
.stat-value {
font-size: 24px;
font-weight: bold;
color: #2980b9;
margin: 10px 0;
}
.stat-label {
font-size: 14px;
color: #7f8c8d;
}
.chart-container {
width: 100%;
margin-top: 20px;
}
.chart-container img {
width: 100%;
height: auto;
border-radius: 5px;
}
footer {
text-align: center;
margin-top: 20px;
color: #7f8c8d;
font-size: 12px;
}
.last-update {
text-align: right;
margin-top: 10px;
font-size: 12px;
color: #7f8c8d;
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>WebDev Backup Dashboard</h1>
<p>Real-time monitoring and statistics</p>
</header>
<div class="dashboard-grid">
<div class="card">
<h2>Backup Status</h2>
<div class="stats">
<div class="stat-card">
<div class="stat-label">Last Backup</div>
<div class="stat-value">$backup_date</div>
</div>
<div class="stat-card">
<div class="stat-label">Projects</div>
<div class="stat-value">$projects_count</div>
</div>
<div class="stat-card">
<div class="stat-label">Latest Size</div>
<div class="stat-value">$backup_size</div>
</div>
<div class="stat-card">
<div class="stat-label">Status</div>
<div class="stat-value" style="color: #27ae60;">ACTIVE</div>
</div>
</div>
</div>
<div class="card">
<h2>Storage Forecast</h2>
<div class="chart-container">
<img src="backup_forecast_chart.png" alt="Storage Forecast">
<div style="text-align: center; margin-top: 10px;">
<p><em>Note: Install gnuplot for detailed charts</em></p>
</div>
</div>
</div>
<div class="card">
<h2>Backup History</h2>
<div class="chart-container">
<img src="backup_history_chart.png" alt="Backup History">
<div style="text-align: center; margin-top: 10px;">
<p><em>Note: Install gnuplot for detailed charts</em></p>
</div>
</div>
</div>
<div class="card">
<h2>Quick Actions</h2>
<ul>
<li><a href="backup_dashboard.html">Refresh Dashboard</a></li>
<li><a href="#" onclick="alert('This feature will be implemented in a future version')">Force Backup Now</a></li>
<li><a href="#" onclick="alert('This feature will be implemented in a future version')">Restore Wizard</a></li>
<li><a href="#" onclick="alert('This feature will be implemented in a future version')">Configure Notifications</a></li>
</ul>
</div>
</div>
<div class="last-update">
Last updated: $(date)
</div>
<footer>
WebDev Backup Tool Dashboard - Generated Automatically
</footer>
</div>
</body>
</html>
EOF
echo "$dashboard_file"
}
# End of reporting functions