L2 Support Engineer · Fintech · Week 3
Week 3
Day 7
Day 8
Week 3 · Day 7 & Day 8
Backup Scripts &
Mini Project
Day 7 teaches you to back up and restore log files using tar and gzip. Day 8 brings everything together into a fully automated L2 mini project — the script that detects and fixes problems on its own.
Day 7 — Backup Script
Day 8 — Mini Project
targzip
Service MonitorFull Automation
Day 7
Backup Script — tar & gzip
01 The Simple Idea
Real-life Analogy
Think of log files like important paper documents in an office. Every day new documents are created. If the office catches fire, everything is gone.
A backup is like photocopying all documents, putting them in a sealed box, and storing them in a different building. If something goes wrong — the box is safe. You unpack it and continue.
tar creates the box. gzip compresses it so it takes up less storage space.
02 tar & gzip — How They Work
tar — bundles files into one archive
tar takes a whole folder of files and puts them into a single file — like zipping many documents into one envelope. On its own it doesn't compress — it just bundles.
gzip compresses the tar file to make it smaller — like vacuum-sealing the envelope. Combined as .tar.gz, you get a compressed archive.
⌨️ tar Flags — What Each One Does
| Flag | Meaning | Used for |
| -c | Create a new archive | Making a backup |
| -z | Compress with gzip | Making it smaller |
| -f | Filename follows next | Always needed — name your file |
| -t | List contents of archive | Peeking inside without extracting |
| -x | Extract the archive | Restoring a backup |
| -v | Verbose — show each file | See what is happening |
| -C | Change to this directory first | Controlling where files go |
tar — quick reference commands
# Create a compressed backup
tar -czf backup.tar.gz /folder/to/backup/
# List what's inside a backup (don't extract)
tar -tzvf backup.tar.gz
# Extract a backup into a specific folder
tar -xzf backup.tar.gz -C /where/to/restore/
# Backup with a timestamp in the filename
TS=$(date +"%Y-%m-%d_%H-%M-%S")
tar -czf "backup-$TS.tar.gz" /folder/
03 Day 7 Scripts
01-setup-lab.sh
Script 1 — Setup Lab Folders & Sample Logs
01
Run First
01-setup-lab.sh
#!/bin/bash
# Creates ~/logs, ~/backups, ~/scripts folders
# and 3 sample log files for testing
mkdir -p "$HOME/logs" "$HOME/backups" "$HOME/scripts"
cat > "$HOME/logs/payment-service.log" << 'EOF'
[2024-03-15 14:02:05] [ERROR] DB_CONNECTION_TIMEOUT failed to acquire connection
[2024-03-15 14:02:05] [ERROR] TXN-9821 FAILED unable to write to database
[2024-03-15 14:02:16] [INFO ] TXN-9824 processed successfully Status: SUCCESS
EOF
cat > "$HOME/logs/api-service.log" << 'EOF'
[2024-03-15 13:45:01] [ERROR] NullPointerException at PaymentProcessor.java:142
[2024-03-15 14:30:00] [INFO ] Scheduled job completed successfully
EOF
cat > "$HOME/logs/system.log" << 'EOF'
[2024-03-15 16:00:00] [ERROR] Disk usage: 92% - critical level reached
[2024-03-15 08:01:00] [INFO ] All services started
EOF
echo "Setup complete. Folders and logs created."
✅ How to run: chmod +x 01-setup-lab.sh && ./01-setup-lab.sh
02-backup-logs.sh
Script 2 — Backup the Logs Folder
02
Day 7 Lab
02-backup-logs.sh
#!/bin/bash
# Backs up ~/logs to ~/backups as a timestamped .tar.gz
# Deletes backups older than 7 days automatically
LOGS_DIR="$HOME/logs"
BACKUP_DIR="$HOME/backups"
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
BACKUP_FILE="$BACKUP_DIR/logs-backup-$TIMESTAMP.tar.gz"
KEEP_DAYS=7
mkdir -p "$BACKUP_DIR"
echo "Creating backup: $BACKUP_FILE"
tar -czf "$BACKUP_FILE" -C "$HOME" logs/
if [ $? -eq 0 ]; then
SIZE=$(du -sh "$BACKUP_FILE" | awk '{print $1}')
echo "SUCCESS: Backup created ($SIZE)"
else
echo "ERROR: Backup failed!"; exit 1
fi
# Remove backups older than KEEP_DAYS
find "$BACKUP_DIR" -name "logs-backup-*.tar.gz" -mtime +$KEEP_DAYS -delete
echo "Old backups cleaned. Current backups:"
ls -lh "$BACKUP_DIR"
✅ How to run: chmod +x 02-backup-logs.sh && ./02-backup-logs.sh
03-restore-backup.sh
Script 3 — Restore a Backup
03
Restore
03-restore-backup.sh
#!/bin/bash
# Lists available backups and restores chosen one
BACKUP_DIR="$HOME/backups"
RESTORE_DIR="$HOME/logs-restored"
echo "Available backups:"
ls -lh "$BACKUP_DIR"/*.tar.gz
echo ""
echo "Enter the full path of backup to restore:"
read BACKUP_FILE
if [ ! -f "$BACKUP_FILE" ]; then
echo "ERROR: File not found."; exit 1
fi
echo "Contents of backup:"
tar -tzvf "$BACKUP_FILE"
echo ""
mkdir -p "$RESTORE_DIR"
tar -xzf "$BACKUP_FILE" -C "$RESTORE_DIR"
echo "SUCCESS: Restored to $RESTORE_DIR"
✅ How to run: chmod +x 03-restore-backup.sh && ./03-restore-backup.sh
04 Day 7 Lab — Backup Logs Folder
🔬 Lab: Create, Inspect & Restore a Backup
Kali Linux
Setup and run the backup
Run setup first, then backup.
terminal
chmod +x ~/scripts/*.sh
./01-setup-lab.sh
./02-backup-logs.sh
→ Backup file created in ~/backups/ ✅
Inspect the backup without extracting
Use tar -tzvf to see what's inside.
terminal
tar -tzvf ~/backups/logs-backup-*.tar.gz
→ Shows all files inside the archive with sizes ✅
Delete the logs folder and restore
Simulate a disaster — delete logs, then restore from backup.
terminal
rm -rf ~/logs/
./03-restore-backup.sh
→ Logs restored to ~/logs-restored/ from backup ✅
Day 8
Mini Project — Full Bash Automation
05 The Simple Idea
Real-life Analogy
Think of a self-driving car's safety system. It constantly checks everything — tire pressure, engine, fuel, road conditions. The moment something is wrong it takes action — sounds an alarm, slows down, pulls over. It doesn't wait for you to notice.
Your mini project script is that safety system for your server. It runs on a loop, checks disk + memory + services + logs, and automatically restarts anything that has stopped — all without you having to do anything.
06 Day 8 Scripts
05-service-monitor.sh
Script 5 — Auto Detect & Restart Failed Service
05
Mini Project
05-service-monitor.sh — runs in a loop
#!/bin/bash
# Monitors services in a loop.
# Auto-restarts if a service is down.
# Press Ctrl+C to stop.
SERVICES=("ssh" "cron")
CHECK_INTERVAL=30
LOG_FILE="$HOME/logs/monitor.log"
MAX_RESTARTS=3
log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"; }
check_disk() {
D=$(df / | awk 'NR==2{print $5}' | tr -d '%')
[ "$D" -ge 85 ] && log "[DISK] WARNING: $D%" || log "[DISK] OK: $D%"
}
check_memory() {
SW=$(free -m | awk 'NR==3{print $3}')
[ "$SW" -gt 100 ] && log "[MEM ] WARNING: Swap ${SW}MB" || log "[MEM ] OK: Swap ${SW}MB"
}
check_service() {
SVC="$1"; RC=0
if systemctl is-active --quiet "$SVC" 2>/dev/null; then
log "[SVC ] OK: $SVC is RUNNING"
else
log "[SVC ] DOWN: $SVC — attempting restart..."
while [ $RC -lt $MAX_RESTARTS ]; do
RC=$((RC+1))
sudo systemctl restart "$SVC" 2>/dev/null; sleep 3
if systemctl is-active --quiet "$SVC" 2>/dev/null; then
log "[SVC ] SUCCESS: $SVC restarted (attempt $RC)"; return
fi
done
log "[SVC ] CRITICAL: $SVC failed after $MAX_RESTARTS attempts!"
fi
}
mkdir -p "$HOME/logs"
log "Monitor started. Services: ${SERVICES[*]}"
echo "Running... Press Ctrl+C to stop. Log: $LOG_FILE"
LOOP=1
while true; do
log "--- Check #$LOOP ---"
check_disk
check_memory
for S in "${SERVICES[@]}"; do check_service "$S"; done
LOOP=$((LOOP+1))
sleep "$CHECK_INTERVAL"
done
✅ How to run: chmod +x 05-service-monitor.sh && ./05-service-monitor.sh — Press Ctrl+C to stop
06-l2-daily-runbook.sh
Script 6 — Full L2 Daily Runbook (The Main Script)
06
⭐ Main Script
⭐ This is the key script of the whole mini project. One command — runs every check, logs every result, creates a backup, and saves a full daily report. This is what runs at 8 AM every morning via crontab.
06-l2-daily-runbook.sh — abbreviated for reference
#!/bin/bash
# Full daily runbook — disk + RAM + services + logs + backup
# Run every morning. Saves a dated report file.
REPORT_FILE="$HOME/daily-runbook-$(date +%Y-%m-%d).txt"
SERVICES=("ssh" "cron")
ISSUES=0
plog() { echo "$1"; echo "$1" >> "$REPORT_FILE"; }
plog "=== L2 DAILY RUNBOOK — $(date) ==="
# 1. Disk check
D=$(df / | awk 'NR==2{print $5}' | tr -d '%')
[ "$D" -ge 90 ] && { plog "DISK CRITICAL: $D%"; ISSUES+=1; } || plog "DISK OK: $D%"
# 2. Memory check
SW=$(free -m | awk 'NR==3{print $3}')
[ "$SW" -gt 200 ] && { plog "MEM WARNING: Swap ${SW}MB"; ISSUES+=1; } || plog "MEM OK"
# 3. Service checks
for S in "${SERVICES[@]}"; do
systemctl is-active --quiet "$S" && plog "SVC OK: $S" || { plog "SVC DOWN: $S"; ISSUES+=1; }
done
# 4. Log errors
E=$(grep -c "ERROR" "$HOME/logs/payment-service.log" 2>/dev/null || echo 0)
[ "$E" -gt 0 ] && { plog "LOG WARNING: $E errors found"; ISSUES+=1; } || plog "LOG OK"
# 5. Backup
TS=$(date +"%Y-%m-%d_%H-%M-%S")
tar -czf "$HOME/backups/logs-backup-$TS.tar.gz" -C "$HOME" logs/ && plog "BACKUP OK" || plog "BACKUP FAILED"
plog "=== DONE — $ISSUES issue(s) found. Report: $REPORT_FILE ==="
✅ How to run: chmod +x 06-l2-daily-runbook.sh && ./06-l2-daily-runbook.sh
07-schedule-all.sh
Script 7 — Schedule All Scripts with Crontab
07
Run Last
07-schedule-all.sh
#!/bin/bash
# Gives all scripts permission and schedules cron jobs
chmod +x "$HOME/scripts"/*.sh
echo "Permissions set."
# Daily runbook at 8 AM every weekday
(crontab -l 2>/dev/null; echo "0 8 * * 1-5 $HOME/scripts/06-l2-daily-runbook.sh >> $HOME/logs/runbook.log 2>&1") | crontab -
# Midnight backup daily
(crontab -l 2>/dev/null; echo "0 0 * * * $HOME/scripts/02-backup-logs.sh >> $HOME/backups/backup.log 2>&1") | crontab -
echo "Cron jobs scheduled:"
crontab -l
✅ How to run: chmod +x 07-schedule-all.sh && ./07-schedule-all.sh — run this last
07 Day 8 Lab — Auto Detect + Restart Failed Service
🔬 Lab: Run the Full Mini Project
Kali Linux · Full Automation
Run the service monitor
Starts the loop — watches services every 30 seconds.
terminal
./05-service-monitor.sh
→ Watch the output. It logs disk, memory, service status every 30s. Press Ctrl+C to stop.
Run the full daily runbook
One command — does everything and saves a report.
terminal
./06-l2-daily-runbook.sh
→ Full report saved to ~/daily-runbook-DATE.txt ✅
Read the report
See what the runbook found.
terminal
cat ~/daily-runbook-$(date +%Y-%m-%d).txt
→ Shows all check results and issue count for today ✅
Schedule everything with crontab
Run the final script to automate everything permanently.
terminal
./07-schedule-all.sh
crontab -l
→ All cron jobs confirmed active ✅
08 All Scripts — Summary
📋 Complete Script Reference
| Script | Day | What it does | Run order |
| 01-setup-lab.sh | Day 7 | Creates ~/logs, ~/backups, ~/scripts + 3 sample log files | First |
| 02-backup-logs.sh | Day 7 | Backs up ~/logs as timestamped .tar.gz, cleans old backups | Second |
| 03-restore-backup.sh | Day 7 | Restores a chosen backup to ~/logs-restored/ | After backup |
| 04-scheduled-backup.sh | Day 7 | Schedules backup with crontab at midnight | Optional |
| 05-service-monitor.sh | Day 8 | Monitors services in a loop, auto-restarts if down | Interactive |
| 06-l2-daily-runbook.sh | Day 8 | Full daily runbook — disk + RAM + services + logs + backup | Every morning |
| 07-schedule-all.sh | Day 8 | Sets permissions + adds all cron jobs at once | Last |
09 Real L2 Scenarios
01
Disk fills up at 3 AM and the payment service stops writing logs. The backup that ran at midnight has all the logs safely stored. You restore from backup, identify the issue from the archived logs, and fix it before 8 AM. No data lost.
02
A service crashes at 2 AM. The service monitor running in the background detects it within 30 seconds, tries restarting it automatically up to 3 times, logs each attempt, and alerts you if all 3 fail. You wake up to a log showing the service restarted successfully at 2:01 AM.
03
Manager asks for this morning's server health report. You open ~/daily-runbook-2024-03-15.txt — the full disk, memory, service status and error count from 8 AM is already there. You paste it in the reply. Done in 10 seconds.
04
Client accidentally deletes their transaction log and asks if you can recover it. The backup from last midnight has it. You run the restore script, locate the file inside the archive, and send it to the client within 5 minutes.
✅ Week 3 · Day 7 & 8 Outcomes
- Understand what tar and gzip do and why .tar.gz is the standard backup format
- Use tar -czf to create a compressed backup and tar -xzf to restore it
- Use tar -tzvf to inspect a backup without extracting it
- Build a backup script with timestamps, automatic cleanup of old backups, and size reporting
- Build a restore script that lists available backups and extracts the chosen one safely
- Build a service monitor script that runs in a loop and auto-restarts failed services
- Build the full L2 daily runbook — the one script that does all checks and saves a report
- Schedule all scripts with crontab — runbook at 8 AM, backup at midnight
- Understand how full bash automation makes you proactive as an L2 engineer