Mini Project: Log Report & Backup
In this project you'll build two small but realistic admin tools, step by step:
logreport.shreads a web server's access log and writes a traffic report: requests, visitors, data sent, error rate, top pages and status codes.backup.sharchives the logs and the report into a dated.tar.gzand deletes old backups, keeping only the newest few.
Along the way you'll use most of the course: awk, sort | uniq -c, here-documents, functions, getopts, strict mode, tar, find/xargs and cron.
Every step is a complete program. Each one first writes the same small sample log (12 requests over two days), because each playground starts in an empty home directory. On a real server you'd point the scripts at /var/log/nginx/access.log.
Step 1: Understand the log format
Web servers such as nginx and Apache write one line per request in the combined log format. Let's look at a line and find out which awk field holds what:
cat > access.log <<'LOG'
203.0.113.5 - - [14/Mar/2026:09:12:01 +0000] "GET /index.html HTTP/1.1" 200 5120
203.0.113.5 - - [14/Mar/2026:09:12:03 +0000] "GET /css/site.css HTTP/1.1" 200 1800
198.51.100.7 - - [14/Mar/2026:11:40:22 +0000] "GET /products HTTP/1.1" 200 8400
198.51.100.7 - - [14/Mar/2026:11:41:05 +0000] "POST /cart HTTP/1.1" 500 310
192.0.2.44 - - [14/Mar/2026:18:03:59 +0000] "GET /admin HTTP/1.1" 403 210
203.0.113.5 - - [15/Mar/2026:08:01:10 +0000] "GET /index.html HTTP/1.1" 200 5120
192.0.2.44 - - [15/Mar/2026:08:15:42 +0000] "GET /old-page HTTP/1.1" 404 190
198.51.100.7 - - [15/Mar/2026:10:22:31 +0000] "GET /products HTTP/1.1" 200 8400
198.51.100.9 - - [15/Mar/2026:10:22:35 +0000] "GET /index.html HTTP/1.1" 200 5120
198.51.100.9 - - [15/Mar/2026:10:23:01 +0000] "POST /cart HTTP/1.1" 200 640
203.0.113.5 - - [15/Mar/2026:12:00:00 +0000] "GET /products HTTP/1.1" 200 8400
192.0.2.44 - - [15/Mar/2026:12:30:12 +0000] "POST /login HTTP/1.1" 401 220
LOG
wc -l < access.log
head -n 1 access.log
head -n 1 access.log | awk '{ for (i = 1; i <= NF; i++) printf "$%d = %s\n", i, $i }'12
203.0.113.5 - - [14/Mar/2026:09:12:01 +0000] "GET /index.html HTTP/1.1" 200 5120
$1 = 203.0.113.5
$2 = -
$3 = -
$4 = [14/Mar/2026:09:12:01
$5 = +0000]
$6 = "GET
$7 = /index.html
$8 = HTTP/1.1"
$9 = 200
$10 = 5120
The useful fields are $1 (client IP), $4 (date and time, with a leading [), $6/$7 (method with a leading ", and path), $9 (HTTP status) and $10 (bytes sent).
Step 2: The key numbers
One awk pass computes all totals. Unique visitors are counted with an associative array: seen[$1]++ == 0 is true only the first time an IP appears:
cat > access.log <<'LOG'
203.0.113.5 - - [14/Mar/2026:09:12:01 +0000] "GET /index.html HTTP/1.1" 200 5120
203.0.113.5 - - [14/Mar/2026:09:12:03 +0000] "GET /css/site.css HTTP/1.1" 200 1800
198.51.100.7 - - [14/Mar/2026:11:40:22 +0000] "GET /products HTTP/1.1" 200 8400
198.51.100.7 - - [14/Mar/2026:11:41:05 +0000] "POST /cart HTTP/1.1" 500 310
192.0.2.44 - - [14/Mar/2026:18:03:59 +0000] "GET /admin HTTP/1.1" 403 210
203.0.113.5 - - [15/Mar/2026:08:01:10 +0000] "GET /index.html HTTP/1.1" 200 5120
192.0.2.44 - - [15/Mar/2026:08:15:42 +0000] "GET /old-page HTTP/1.1" 404 190
198.51.100.7 - - [15/Mar/2026:10:22:31 +0000] "GET /products HTTP/1.1" 200 8400
198.51.100.9 - - [15/Mar/2026:10:22:35 +0000] "GET /index.html HTTP/1.1" 200 5120
198.51.100.9 - - [15/Mar/2026:10:23:01 +0000] "POST /cart HTTP/1.1" 200 640
203.0.113.5 - - [15/Mar/2026:12:00:00 +0000] "GET /products HTTP/1.1" 200 8400
192.0.2.44 - - [15/Mar/2026:12:30:12 +0000] "POST /login HTTP/1.1" 401 220
LOG
awk '{
requests++
bytes += $10
if (seen[$1]++ == 0) visitors++
if ($9 >= 400) errors++
}
END {
printf "requests: %d\n", requests
printf "visitors: %d\n", visitors
printf "data: %.1f KiB\n", bytes / 1024
printf "errors: %d (%.1f%%)\n", errors, 100 * errors / requests
}' access.logrequests: 12
visitors: 4
data: 42.9 KiB
errors: 4 (33.3%)
Step 3: Rankings
The classic sort | uniq -c | sort -rn pipeline ranks pages and status codes. -k1,1nr -k2,2 sorts by count, and alphabetically on ties, so the order is always the same:
cat > access.log <<'LOG'
203.0.113.5 - - [14/Mar/2026:09:12:01 +0000] "GET /index.html HTTP/1.1" 200 5120
203.0.113.5 - - [14/Mar/2026:09:12:03 +0000] "GET /css/site.css HTTP/1.1" 200 1800
198.51.100.7 - - [14/Mar/2026:11:40:22 +0000] "GET /products HTTP/1.1" 200 8400
198.51.100.7 - - [14/Mar/2026:11:41:05 +0000] "POST /cart HTTP/1.1" 500 310
192.0.2.44 - - [14/Mar/2026:18:03:59 +0000] "GET /admin HTTP/1.1" 403 210
203.0.113.5 - - [15/Mar/2026:08:01:10 +0000] "GET /index.html HTTP/1.1" 200 5120
192.0.2.44 - - [15/Mar/2026:08:15:42 +0000] "GET /old-page HTTP/1.1" 404 190
198.51.100.7 - - [15/Mar/2026:10:22:31 +0000] "GET /products HTTP/1.1" 200 8400
198.51.100.9 - - [15/Mar/2026:10:22:35 +0000] "GET /index.html HTTP/1.1" 200 5120
198.51.100.9 - - [15/Mar/2026:10:23:01 +0000] "POST /cart HTTP/1.1" 200 640
203.0.113.5 - - [15/Mar/2026:12:00:00 +0000] "GET /products HTTP/1.1" 200 8400
192.0.2.44 - - [15/Mar/2026:12:30:12 +0000] "POST /login HTTP/1.1" 401 220
LOG
echo "Top pages:"
awk '{ print $7 }' access.log | sort | uniq -c | sort -k1,1nr -k2,2 | head -n 3
echo "Status codes:"
awk '{ print $9 }' access.log | sort | uniq -c | sort -k2,2n
echo "Requests per day:"
awk '{ print substr($4, 2, 11) }' access.log | uniq -c
echo "Failed requests:"
awk '$9 >= 400 { printf " %s %s %s from %s\n", $9, substr($6, 2), $7, $1 }' access.logTop pages:
3 /index.html
3 /products
2 /cart
Status codes:
8 200
1 401
1 403
1 404
1 500
Requests per day:
5 14/Mar/2026
7 15/Mar/2026
Failed requests:
500 POST /cart from 198.51.100.7
403 GET /admin from 192.0.2.44
404 GET /old-page from 192.0.2.44
401 POST /login from 192.0.2.44
substr($4, 2, 11) cuts 14/Mar/2026 out of [14/Mar/2026:09:12:01. uniq -c works without sort here because the log is already in time order.
Step 4: A report function
Now we combine the pieces into a function that writes a formatted report with a here-document. Command substitutions inside the unquoted here-document fill in the numbers:
cat > access.log <<'LOG'
203.0.113.5 - - [14/Mar/2026:09:12:01 +0000] "GET /index.html HTTP/1.1" 200 5120
203.0.113.5 - - [14/Mar/2026:09:12:03 +0000] "GET /css/site.css HTTP/1.1" 200 1800
198.51.100.7 - - [14/Mar/2026:11:40:22 +0000] "GET /products HTTP/1.1" 200 8400
198.51.100.7 - - [14/Mar/2026:11:41:05 +0000] "POST /cart HTTP/1.1" 500 310
192.0.2.44 - - [14/Mar/2026:18:03:59 +0000] "GET /admin HTTP/1.1" 403 210
203.0.113.5 - - [15/Mar/2026:08:01:10 +0000] "GET /index.html HTTP/1.1" 200 5120
192.0.2.44 - - [15/Mar/2026:08:15:42 +0000] "GET /old-page HTTP/1.1" 404 190
198.51.100.7 - - [15/Mar/2026:10:22:31 +0000] "GET /products HTTP/1.1" 200 8400
198.51.100.9 - - [15/Mar/2026:10:22:35 +0000] "GET /index.html HTTP/1.1" 200 5120
198.51.100.9 - - [15/Mar/2026:10:23:01 +0000] "POST /cart HTTP/1.1" 200 640
203.0.113.5 - - [15/Mar/2026:12:00:00 +0000] "GET /products HTTP/1.1" 200 8400
192.0.2.44 - - [15/Mar/2026:12:30:12 +0000] "POST /login HTTP/1.1" 401 220
LOG
top_n() { # top_n FIELD N: the N most frequent values of a field
awk -v f="$1" '{ print $f }' access.log | sort | uniq -c | sort -k1,1nr -k2,2 | head -n "$2" |
awk '{ printf " %-12s %3d\n", $2, $1 }'
}
write_report() {
local log=$1
cat <<EOF
TRAFFIC REPORT for $log
=================================
Requests: $(wc -l < "$log")
Visitors: $(awk '{ print $1 }' "$log" | sort -u | wc -l)
Errors: $(awk '$9 >= 400' "$log" | wc -l)
Top pages:
$(top_n 7 3)
Top visitors:
$(top_n 1 2)
EOF
}
write_report access.log > report.txt
cat report.txtTRAFFIC REPORT for access.log
=================================
Requests: 12
Visitors: 4
Errors: 4
Top pages:
/index.html 3
/products 3
/cart 2
Top visitors:
203.0.113.5 4
192.0.2.44 3
Step 5: The finished logreport.sh
A real tool needs options, input validation and strict error handling. This version takes -n N (how many top entries), -o FILE (where to write the report, default: the screen) and the log file as its argument:
cat > access.log <<'LOG'
203.0.113.5 - - [14/Mar/2026:09:12:01 +0000] "GET /index.html HTTP/1.1" 200 5120
203.0.113.5 - - [14/Mar/2026:09:12:03 +0000] "GET /css/site.css HTTP/1.1" 200 1800
198.51.100.7 - - [14/Mar/2026:11:40:22 +0000] "GET /products HTTP/1.1" 200 8400
198.51.100.7 - - [14/Mar/2026:11:41:05 +0000] "POST /cart HTTP/1.1" 500 310
192.0.2.44 - - [14/Mar/2026:18:03:59 +0000] "GET /admin HTTP/1.1" 403 210
203.0.113.5 - - [15/Mar/2026:08:01:10 +0000] "GET /index.html HTTP/1.1" 200 5120
192.0.2.44 - - [15/Mar/2026:08:15:42 +0000] "GET /old-page HTTP/1.1" 404 190
198.51.100.7 - - [15/Mar/2026:10:22:31 +0000] "GET /products HTTP/1.1" 200 8400
198.51.100.9 - - [15/Mar/2026:10:22:35 +0000] "GET /index.html HTTP/1.1" 200 5120
198.51.100.9 - - [15/Mar/2026:10:23:01 +0000] "POST /cart HTTP/1.1" 200 640
203.0.113.5 - - [15/Mar/2026:12:00:00 +0000] "GET /products HTTP/1.1" 200 8400
192.0.2.44 - - [15/Mar/2026:12:30:12 +0000] "POST /login HTTP/1.1" 401 220
LOG
cat > logreport.sh <<'SCRIPT'
#!/usr/bin/env bash
set -euo pipefail
usage() { echo "Usage: $(basename "$0") [-n TOP] [-o OUTPUT] LOGFILE" >&2; exit 2; }
die() { echo "logreport: $*" >&2; exit 1; }
top=3
output=""
while getopts ":n:o:h" opt; do
case $opt in
n) [[ $OPTARG =~ ^[1-9][0-9]*$ ]] || die "-n needs a positive number"; top=$OPTARG ;;
o) output=$OPTARG ;;
h) usage ;;
:) die "-$OPTARG needs a value" ;;
*) die "unknown option -$OPTARG" ;;
esac
done
shift $(( OPTIND - 1 ))
(( $# == 1 )) || usage
log=$1
[[ -r $log ]] || die "cannot read $log"
rank() { awk -v f="$1" '{ print $f }' "$log" | sort | uniq -c | sort -k1,1nr -k2,2 | head -n "$top"; }
report() {
awk '{ n++; b += $10; if (!s[$1]++) v++; if ($9 >= 400) e++ }
END { printf "requests %d | visitors %d | %.1f KiB | errors %d (%.0f%%)\n", n, v, b/1024, e, 100*e/n }' "$log"
echo "top $top pages:"
rank 7 | awk '{ printf " %3d %s\n", $1, $2 }'
echo "top $top visitors:"
rank 1 | awk '{ printf " %3d %s\n", $1, $2 }'
}
if [[ -n $output ]]; then
report > "$output"
echo "report written to $output ($(wc -l < "$output") lines)"
else
report
fi
SCRIPT
chmod +x logreport.sh
./logreport.sh -n 2 access.log
echo "---"
./logreport.sh -o daily.txt access.log
echo "---"
./logreport.sh missing.log || echo "exit status $?"
./logreport.sh -n zero access.log || echo "exit status $?"
./logreport.sh || echo "exit status $?"requests 12 | visitors 4 | 42.9 KiB | errors 4 (33%)
top 2 pages:
3 /index.html
3 /products
top 2 visitors:
4 203.0.113.5
3 192.0.2.44
---
report written to daily.txt (9 lines)
---
logreport: cannot read missing.log
exit status 1
logreport: -n needs a positive number
exit status 1
Usage: logreport.sh [-n TOP] [-o OUTPUT] LOGFILE
exit status 2
Notice how the error cases end with clear messages on standard error and non-zero exit statuses: that's what makes the script safe to use from cron or other scripts.
Step 6: backup.sh with rotation
The second tool archives the logs/ directory into backups/logs-DATE.tar.gz and keeps only the newest KEEP archives. The date comes from the STAMP variable so you can test it; in production it defaults to today's date (date +%F). To test the rotation, the example first creates some older (empty) backups:
mkdir -p logs
cat > logs/access.log <<'LOG'
203.0.113.5 - - [14/Mar/2026:09:12:01 +0000] "GET /index.html HTTP/1.1" 200 5120
203.0.113.5 - - [14/Mar/2026:09:12:03 +0000] "GET /css/site.css HTTP/1.1" 200 1800
198.51.100.7 - - [14/Mar/2026:11:40:22 +0000] "GET /products HTTP/1.1" 200 8400
198.51.100.7 - - [14/Mar/2026:11:41:05 +0000] "POST /cart HTTP/1.1" 500 310
192.0.2.44 - - [14/Mar/2026:18:03:59 +0000] "GET /admin HTTP/1.1" 403 210
203.0.113.5 - - [15/Mar/2026:08:01:10 +0000] "GET /index.html HTTP/1.1" 200 5120
192.0.2.44 - - [15/Mar/2026:08:15:42 +0000] "GET /old-page HTTP/1.1" 404 190
198.51.100.7 - - [15/Mar/2026:10:22:31 +0000] "GET /products HTTP/1.1" 200 8400
198.51.100.9 - - [15/Mar/2026:10:22:35 +0000] "GET /index.html HTTP/1.1" 200 5120
198.51.100.9 - - [15/Mar/2026:10:23:01 +0000] "POST /cart HTTP/1.1" 200 640
203.0.113.5 - - [15/Mar/2026:12:00:00 +0000] "GET /products HTTP/1.1" 200 8400
192.0.2.44 - - [15/Mar/2026:12:30:12 +0000] "POST /login HTTP/1.1" 401 220
LOG
cat > backup.sh <<'SCRIPT'
#!/usr/bin/env bash
set -euo pipefail
src=${1:?usage: backup.sh SOURCE_DIR [DEST_DIR]}
dest=${2:-backups}
keep=${KEEP:-3}
stamp=${STAMP:-$(date +%F)}
[[ -d $src ]] || { echo "backup: no such directory: $src" >&2; exit 1; }
mkdir -p "$dest"
archive="$dest/$(basename "$src")-$stamp.tar.gz"
tar -czf "$archive" "$src"
echo "created $archive ($(tar -tzf "$archive" | wc -l) entries)"
# rotation: names contain ISO dates, so sorting by name sorts by age
mapfile -t old < <(ls -1 "$dest"/*.tar.gz | sort | head -n -"$keep")
for f in "${old[@]}"; do
rm -- "$f"
echo "removed old backup $f"
done
echo "kept: $(ls -1 "$dest" | sort | tr '\n' ' ')"
SCRIPT
mkdir -p backups
touch backups/logs-2026-03-10.tar.gz backups/logs-2026-03-12.tar.gz backups/logs-2026-03-13.tar.gz
STAMP=2026-03-15 bash backup.sh logs
echo "---"
tar -tzf backups/logs-2026-03-15.tar.gz
bash backup.sh nowhere || echo "exit status $?"created backups/logs-2026-03-15.tar.gz (2 entries)
removed old backup backups/logs-2026-03-10.tar.gz
kept: logs-2026-03-12.tar.gz logs-2026-03-13.tar.gz logs-2026-03-15.tar.gz
---
logs/
logs/access.log
backup: no such directory: nowhere
exit status 1
head -n -3 (GNU) prints all but the last 3 lines: everything except the three newest archives. ISO dates (YYYY-MM-DD) in file names are what make "sort by name" equal "sort by date".
Step 7: Schedule it
With both scripts tested, install them and let cron run them every night. A crontab entry (crontab -e) with absolute paths and logging:
PATH=/usr/local/bin:/usr/bin:/bin
# m h dom mon dow command
5 0 * * * $HOME/bin/logreport.sh -n 10 -o $HOME/reports/daily.txt /var/log/nginx/access.log >> $HOME/logs/cron.log 2>&1
30 0 * * * KEEP=14 $HOME/bin/backup.sh /var/log/nginx /srv/backups >> $HOME/logs/cron.log 2>&1Before trusting cron, simulate its minimal environment once, which catches missing PATH entries:
env -i HOME="$HOME" PATH=/usr/bin:/bin bash -c '$HOME/bin/logreport.sh /var/log/nginx/access.log'Ideas to extend the project
- Add
-d DATEtologreport.shto report only one day (filter$4withawk -v). - Print the busiest hour (
substr($4, 14, 2)). - Read compressed rotated logs too:
zcat -f access.log*. - Make
backup.shkeep backups by age instead of count:find "$dest" -name '*.tar.gz' -mtime +14 -delete. - Send an alert when the error rate exceeds a threshold (exit with status 1 and let cron mail you).
Test yourself
In the combined log format, which awk field holds the HTTP status code?
- $1
- $7
- $9
- $10
Why does backup.sh name archives with ISO dates like logs-2026-03-15.tar.gz?
- tar requires it
- Sorting the names alphabetically then sorts them by date, which makes rotation simple
- cron needs dates in names
- It compresses better
What does set -euo pipefail give logreport.sh?
- Faster execution
- It stops on errors, unset variables and failures inside pipelines
- Colourful output
- Automatic logging