0
0
Raspberry Piprogramming~20 mins

Data rotation and cleanup in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Data Rotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of log file rotation script

What is the output of this Raspberry Pi shell script that rotates a log file by renaming and compressing it?

Raspberry Pi
#!/bin/bash
LOGFILE="/var/log/myapp.log"
if [ -f "$LOGFILE" ]; then
  mv "$LOGFILE" "$LOGFILE.$(date +%Y%m%d)"
  gzip "$LOGFILE.$(date +%Y%m%d)"
  echo "Log rotated"
else
  echo "No log to rotate"
fi
Amv: cannot stat '/var/log/myapp.log': No such file or directory
BLog rotated
CNo log to rotate
Dgzip: command not found
Attempts:
2 left
💡 Hint

Assume the log file exists before running the script.

🧠 Conceptual
intermediate
1:30remaining
Understanding log cleanup with find command

Which find command will delete all compressed log files older than 7 days in /var/log?

Afind /var/log -name '*.gz' -mtime +7 -delete
Bfind /var/log -name '*.gz' -mtime -7 -delete
Cfind /var/log -name '*.gz' -mtime 7 -delete
Dfind /var/log -name '*.gz' -mtime +7 -exec rm {} \;
Attempts:
2 left
💡 Hint

Remember -mtime +7 means files modified more than 7 days ago.

🔧 Debug
advanced
2:00remaining
Fix the Python script for rotating logs

What error does this Python script raise when trying to rotate logs?

import os
import datetime
logfile = '/var/log/myapp.log'
if os.path.exists(logfile):
    newname = logfile + datetime.datetime.now().strftime('%Y%m%d')
    os.rename(logfile, newname)
    os.system(f'gzip {newname}')
    print('Rotated')
else:
    print('No log')
APermissionError
BFileNotFoundError
CSyntaxError
DNo error, prints 'Rotated'
Attempts:
2 left
💡 Hint

Consider typical permissions for /var/log on Raspberry Pi.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this log cleanup script

Which option contains the syntax error in this Bash snippet?

for file in /var/log/*.gz
 do
  if [ $(stat -c %Y "$file") -lt $(date -d '7 days ago' +%s) ]; then
    rm "$file"
  fi
 done
AMissing semicolon after for loop declaration
BMissing closing bracket in if condition
CNo syntax error
DIncorrect use of single quotes in date command
Attempts:
2 left
💡 Hint

Check the syntax of the for loop header in Bash.

🚀 Application
expert
2:00remaining
Determine the number of rotated log files after cleanup

A Raspberry Pi rotates logs daily by renaming myapp.log to myapp.log.YYYYMMDD and compressing it. The cleanup script deletes compressed logs older than 10 days. If logs have been rotated daily for 15 days, how many compressed log files remain after running cleanup?

A0
B5
C15
D10
Attempts:
2 left
💡 Hint

Think about which files are deleted and which remain based on age.