From 1282575a222e9bbdb77a4c818cfdda25dd225fc3 Mon Sep 17 00:00:00 2001 From: Rasheed Olanrewaju Date: Thu, 2 Apr 2026 21:46:30 +0100 Subject: [PATCH] feat: complete bash scripts --- backups/backup_20260402_21:13:16.tar.gz | Bin 0 -> 2672 bytes logs/app.log | 30 +++ logs/backup.log | 23 ++ logs/file_manager.log | 10 + logs/process_monitor.log | 53 +++++ logs/system_report_2026-04-01.log | 280 ++++++++++++++++++++++++ logs/user_info.log | 6 + scripts/backup.sh | 48 ++++ scripts/file_manager.sh | 70 ++++++ scripts/process_monitor.sh | 31 +++ scripts/run_all.sh | 72 ++++++ scripts/system_check.sh | 42 ++++ scripts/user_info.sh | 63 ++++++ 13 files changed, 728 insertions(+) create mode 100755 backups/backup_20260402_21:13:16.tar.gz create mode 100755 logs/app.log create mode 100755 logs/backup.log create mode 100755 logs/file_manager.log create mode 100755 logs/process_monitor.log create mode 100755 logs/system_report_2026-04-01.log create mode 100755 logs/user_info.log create mode 100755 scripts/backup.sh create mode 100755 scripts/file_manager.sh create mode 100755 scripts/process_monitor.sh create mode 100755 scripts/run_all.sh create mode 100755 scripts/system_check.sh create mode 100755 scripts/user_info.sh diff --git a/backups/backup_20260402_21:13:16.tar.gz b/backups/backup_20260402_21:13:16.tar.gz new file mode 100755 index 0000000000000000000000000000000000000000..8c8ae7d4742ba4b1d986f900e2561f2fc7f54c6d GIT binary patch literal 2672 zcmV-$3Xk<4iwFP!000001MM2^Qrk$9zo)0@2z$ZFNwzUIdn8Wc07TJ;Z09`$th^z`(XWR_R%9V!T0TU&Ik zZq+NPn?rsWz%BP``iCR#svjSs}nmIAF}V1#j8&KPi~yGt0h+ zMRXkM~>2{%=$^8@T@)l}c>|D)%N)2L9svzxt?r=K5#^e4uHa!;|Bkz25Hr zakEg>UbS|9IXM#lbThFNu50fO-}GMZzuOa|UU1G66T*94(^g^6=W)ouaYJTDLFh6L z?32qQZFqrWV2nU7q{Manw~ph!HDq|3P}fU>*1O>R-1X0)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=c+bp-}(2MlVn?m0jcIV!6ClqSr#}>ma7W zbNIOerP4SRmW?R#pGMR@vOBiSNJa)x7`zAh4wWpBhK8?2UT_89 z5z@uVuR@m=LnK4SM@Ue<-AGhG%?au;^@iv_F^)`>{b`IpmN-4msqILk>CQkV6hRCQkV6hR literal 0 HcmV?d00001 diff --git a/logs/app.log b/logs/app.log new file mode 100755 index 0000000..78747a5 --- /dev/null +++ b/logs/app.log @@ -0,0 +1,30 @@ +[2026-04-02 20:58:30] unning backup.sh... +[2026-04-02 20:58:30] backup.sh completed successfully +[2026-04-02 20:58:52] Running all tasks... +[2026-04-02 20:58:52] Running file_manager.sh... +[2026-04-02 20:58:52] file_manager.sh completed successfully +[2026-04-02 20:58:52] Running system_check.sh... +[2026-04-02 20:58:52] system_check.sh completed successfully +[2026-04-02 20:58:52] Running backup.sh... +[2026-04-02 20:58:52] backup.sh completed successfully +[2026-04-02 20:58:52] Running process_monitor.sh... +[2026-04-02 20:58:52] process_monitor.sh completed successfully +[2026-04-02 20:58:52] Running user_info.sh... +[2026-04-02 20:59:28] user_info.sh completed successfully +[2026-04-02 20:59:28] All tasks completed +[2026-04-02 20:59:52] Exiting... +[2026-04-02 21:11:05] Exiting... +[2026-04-02 21:13:02] Running system_check.sh... +[2026-04-02 21:13:02] system_check.sh completed successfully +[2026-04-02 21:13:16] Running backup.sh... +[2026-04-02 21:13:16] backup.sh completed successfully +[2026-04-02 21:15:16] Exiting... +[2026-04-02 21:15:31] Running process_monitor.sh... +[2026-04-02 21:15:39] process_monitor.sh completed successfully +[2026-04-02 21:15:44] Running system_check.sh... +[2026-04-02 21:15:44] system_check.sh completed successfully +[2026-04-02 21:15:50] Running user_info.sh... +[2026-04-02 21:15:59] user_info.sh completed successfully +[2026-04-02 21:16:05] Running backup.sh... +[2026-04-02 21:16:05] backup.sh completed successfully +[2026-04-02 21:16:20] Exiting... diff --git a/logs/backup.log b/logs/backup.log new file mode 100755 index 0000000..e33ea58 --- /dev/null +++ b/logs/backup.log @@ -0,0 +1,23 @@ +Error: No source directory provided +Usage: backup.sh +Backup created: ../backups/backup_2026-04-02_20-24-07.tar.gz +Cleaning old backups... +Old backups cleaned +Backup created: ../backups/backup_2026-04-02_20-30-27.tar.gz +Cleaning old backups... +Old backups cleaned +Backup created: ../backups/backup_20260402_20:31:55.tar.gz +Cleaning old backups... +Old backups cleaned +Backup created: ../backups/backup_20260402_20:58:30.tar.gz +Cleaning old backups... +Old backups cleaned +Backup created: ../backups/backup_20260402_20:58:52.tar.gz +Cleaning old backups... +Old backups cleaned +Backup created: ../backups/backup_20260402_21:13:16.tar.gz +Cleaning old backups... +Old backups cleaned +Backup created: ../backups/backup_20260402_21:16:05.tar.gz +Cleaning old backups... +Old backups cleaned diff --git a/logs/file_manager.log b/logs/file_manager.log new file mode 100755 index 0000000..701216f --- /dev/null +++ b/logs/file_manager.log @@ -0,0 +1,10 @@ +File not found: -y +File deleted: test_doc.txt +File list: +total 24K +-rwxrwxrwx 1 rheed rheed 1007 Apr 2 20:31 backup.sh +-rwxrwxrwx 1 rheed rheed 1.4K Apr 1 23:07 file_manager.sh +-rwxrwxrwx 1 rheed rheed 708 Apr 2 20:43 process_monitor.sh +-rwxrwxrwx 1 rheed rheed 1.5K Apr 2 20:58 run_all.sh +-rwxrwxrwx 1 rheed rheed 1.2K Apr 1 22:49 system_check.sh +-rwxrwxrwx 1 rheed rheed 1.3K Apr 1 22:05 user_info.sh diff --git a/logs/process_monitor.log b/logs/process_monitor.log new file mode 100755 index 0000000..7af746d --- /dev/null +++ b/logs/process_monitor.log @@ -0,0 +1,53 @@ + +[2026-04-02 20:40:23] ===== Starting Process Monitor ===== +[2026-04-02 20:40:23] nginx is running +[2026-04-02 20:40:23] sshd is stopped. Attempting restart... +[2026-04-02 20:40:23] Failed to restart sshd +[2026-04-02 20:40:23] docker is stopped. Attempting restart... +[2026-04-02 20:40:23] Failed to restart docker +[2026-04-02 20:40:23] ===== Process Monitor Completed ===== +[2026-04-02 20:41:03] ===== Starting Process Monitor ===== +[2026-04-02 20:41:03] nginx is stopped. Attempting restart... +[2026-04-02 20:41:03] Failed to restart nginx +[2026-04-02 20:41:03] sshd is stopped. Attempting restart... +[2026-04-02 20:41:03] Failed to restart sshd +[2026-04-02 20:41:03] docker is stopped. Attempting restart... +[2026-04-02 20:41:03] Failed to restart docker +[2026-04-02 20:41:03] ===== Process Monitor Completed ===== +[2026-04-02 20:41:15] ===== Starting Process Monitor ===== +[2026-04-02 20:41:15] nginx is stopped. Attempting restart... +[2026-04-02 20:41:16] nginx restarted successfully +[2026-04-02 20:41:16] sshd is stopped. Attempting restart... +[2026-04-02 20:41:16] Failed to restart sshd +[2026-04-02 20:41:16] docker is stopped. Attempting restart... +[2026-04-02 20:41:16] Failed to restart docker +[2026-04-02 20:41:16] ===== Process Monitor Completed ===== +[2026-04-02 20:41:40] ===== Starting Process Monitor ===== +[2026-04-02 20:41:40] nginx is running +[2026-04-02 20:41:40] sshd is stopped. Attempting restart... +[2026-04-02 20:41:40] Failed to restart sshd +[2026-04-02 20:41:40] docker is stopped. Attempting restart... +[2026-04-02 20:41:40] Failed to restart docker +[2026-04-02 20:41:40] ===== Process Monitor Completed ===== +[2026-04-02 20:45:27] ===== Starting Process Monitor ===== +[2026-04-02 20:45:27] nginx is stopped. Attempting restart... +[2026-04-02 20:45:27] nginx restarted successfully +[2026-04-02 20:45:27] sshd is stopped. Attempting restart... +[2026-04-02 20:45:28] Failed to restart sshd +[2026-04-02 20:45:28] docker is stopped. Attempting restart... +[2026-04-02 20:45:28] Failed to restart docker +[2026-04-02 20:45:28] ===== Process Monitor Completed ===== +[2026-04-02 20:58:52] ===== Starting Process Monitor ===== +[2026-04-02 20:58:52] nginx is running +[2026-04-02 20:58:52] sshd is stopped. Attempting restart... +[2026-04-02 20:58:52] Failed to restart sshd +[2026-04-02 20:58:52] docker is stopped. Attempting restart... +[2026-04-02 20:58:52] Failed to restart docker +[2026-04-02 20:58:52] ===== Process Monitor Completed ===== +[2026-04-02 21:15:31] ===== Starting Process Monitor ===== +[2026-04-02 21:15:31] nginx is running +[2026-04-02 21:15:31] sshd is stopped. Attempting restart... +[2026-04-02 21:15:39] Failed to restart sshd +[2026-04-02 21:15:39] docker is stopped. Attempting restart... +[2026-04-02 21:15:39] Failed to restart docker +[2026-04-02 21:15:39] ===== Process Monitor Completed ===== diff --git a/logs/system_report_2026-04-01.log b/logs/system_report_2026-04-01.log new file mode 100755 index 0000000..21a90a9 --- /dev/null +++ b/logs/system_report_2026-04-01.log @@ -0,0 +1,280 @@ +===== SYSTEM RESOURCE MONITOR ===== +---------- Disk Usage ---------- +Filesystem Size Used Avail Use% Mounted on +none 3.9G 0 3.9G 0% /usr/lib/modules/5.15.167.4-microsoft-standard-WSL2 +none 3.9G 4.0K 3.9G 1% /mnt/wsl +drivers 476G 430G 47G 91% /usr/lib/wsl/drivers +/dev/sdd 1007G 2.6G 954G 1% / +none 3.9G 224K 3.9G 1% /mnt/wslg +none 3.9G 0 3.9G 0% /usr/lib/wsl/lib +rootfs 3.9G 2.4M 3.9G 1% /init +none 3.9G 488K 3.9G 1% /run +none 3.9G 0 3.9G 0% /run/lock +none 3.9G 0 3.9G 0% /run/shm +tmpfs 4.0M 0 4.0M 0% /sys/fs/cgroup +none 3.9G 76K 3.9G 1% /mnt/wslg/versions.txt +none 3.9G 76K 3.9G 1% /mnt/wslg/doc +C:\ 476G 430G 47G 91% /mnt/c +tmpfs 787M 20K 787M 1% /run/user/1000 +---------- Memory Usage ---------- + total used free shared buff/cache available +Mem: 7866 561 7347 3 177 7305 +Swap: 2048 0 2048 +---------- CPU Load ---------- + 22:36:36 up 22:56, 1 user, load average: 0.16, 0.08, 0.01 +----- Running processes: 43 +---------- Top 5 memory-consuming processes ---------- +USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND +root 213397 0.0 0.3 83360 24736 ? S" + exit 1 +fi + +if [[ ! -d "$SOURCE_DIR" ]]; then + log "Error: Directory does not exist: $SOURCE_DIR" + exit 1 +fi + +# Create timestamp +#TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S") +TIMESTAMP=$(date +"%Y%m%d_%H:%M:%S") +BACKUP_FILE="$BACKUP_DIR/backup_$TIMESTAMP.tar.gz" + +# Create backup safely +if tar -czf "$BACKUP_FILE" "$SOURCE_DIR"; then + log "Backup created: $BACKUP_FILE" +else + log "Backup failed!" + exit 1 +fi + +# Cleanup old backups (keep last 5 safely) +log "Cleaning old backups..." +find "$BACKUP_DIR" -name "backup_*.tar.gz" -type f -printf "%T@ %p\n" \ + | sort -nr \ + | tail -n +6 \ + | cut -d' ' -f2- \ + | xargs -r rm -- + +log "Old backups cleaned" diff --git a/scripts/file_manager.sh b/scripts/file_manager.sh new file mode 100755 index 0000000..78c5207 --- /dev/null +++ b/scripts/file_manager.sh @@ -0,0 +1,70 @@ +#!/bin/bash + +LOG_FILE="../logs/file_manager.log" +mkdir -p "$(dirname "$LOG_FILE")" + +ACTION=$1 +FILE=$2 +NEW_NAME=$3 + +# Logging function +log() { + echo -e "$1" | tee -a "$LOG_FILE" +} + +# Validate action +if [[ -z "$ACTION" ]]; then + log "Error: No action provided" + log "Usage: $0 {create|delete|list|rename} filename [new_name]" + exit 1 +fi + +case "$ACTION" in + create) + if [[ -z "$FILE" ]]; then + log "Please provide a filename" + elif [[ -f "$FILE" ]]; then + log "File already exists: $FILE" + else + touch "$FILE" + log "File created: $FILE" + fi + ;; + + delete) + if [[ -z "$FILE" ]]; then + log "Please provide a filename" + elif [[ -f "$FILE" ]]; then + read -p "Are you sure you want to delete '$FILE'? (y/n): " confirm + if [[ "$confirm" == "y" ]]; then + rm "$FILE" + log "File deleted: $FILE" + else + log "Deletion cancelled" + fi + else + log "File not found: $FILE" + fi + ;; + + list) + log "File list:" + ls -lh | tee -a "$LOG_FILE" + ;; + + rename) + if [[ -z "$FILE" || -z "$NEW_NAME" ]]; then + log "Usage: rename " + elif [[ -f "$FILE" ]]; then + mv "$FILE" "$NEW_NAME" + log "Renamed $FILE → $NEW_NAME" + else + log "File not found: $FILE" + fi + ;; + + *) + log "Invalid action" + log "Usage: $0 {create|delete|list|rename} filename [new_name]" + ;; +esac diff --git a/scripts/process_monitor.sh b/scripts/process_monitor.sh new file mode 100755 index 0000000..018333e --- /dev/null +++ b/scripts/process_monitor.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +LOG_FILE="../logs/process_monitor.log" +mkdir -p "$(dirname "$LOG_FILE")" + +# Services to monitor +services=("nginx" "sshd" "docker") + +# Logging function with timestamp +log() { + echo -e "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE" +} + +log "===== Starting Process Monitor =====" + +for service in "${services[@]}"; do + if systemctl is-active --quiet "$service"; then + log "$service is running" + else + log "$service is stopped. Attempting restart..." + + # Try restarting service + if sudo systemctl restart "$service"; then + log "$service restarted successfully" + else + log "Failed to restart $service" + fi + fi +done + +log "===== Process Monitor Completed =====" diff --git a/scripts/run_all.sh b/scripts/run_all.sh new file mode 100755 index 0000000..53fa439 --- /dev/null +++ b/scripts/run_all.sh @@ -0,0 +1,72 @@ +#!/bin/bash +set -u # Keep strict, but avoid breaking full runs + +LOG_FILE="../logs/app.log" +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +# Ensure log directory exists +mkdir -p "$(dirname "$LOG_FILE")" + +# Logging function +log() { + echo -e "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE" +} + +# Safe script runner +run_script() { + local script=$1 + shift + + if [[ -f "$SCRIPT_DIR/$script" ]]; then + log "Running $script..." + if bash "$SCRIPT_DIR/$script" "$@"; then + log "$script completed successfully" + else + log "$script failed" + fi + else + log "Script not found: $script" + fi +} + +run_all() { + log "Running all tasks..." + + run_script "file_manager.sh" list + run_script "system_check.sh" + run_script "backup.sh" . + run_script "process_monitor.sh" + run_script "user_info.sh" + + log "All tasks completed" +} + +menu() { + echo "=================================" + echo " MAIN MENU " + echo "=================================" + echo "1. Run all tasks" + echo "2. System check" + echo "3. Backup current directory" + echo "4. Monitor system processes" + echo "5. Get user information" + echo "6. Exit" + echo "=================================" +} + +while true; do + menu + read -p " Choose option: " choice + echo "" + case "$choice" in + 1) run_all ;; + 2) run_script "system_check.sh" ;; + 3) run_script "backup.sh" . ;; + 4)run_script "process_monitor.sh" ;; + 5)run_script "user_info.sh" ;; + 6) log "Exiting..."; exit 0 ;; + *) echo "Invalid option. Try again." ;; + esac + + echo +done diff --git a/scripts/system_check.sh b/scripts/system_check.sh new file mode 100755 index 0000000..16a61ce --- /dev/null +++ b/scripts/system_check.sh @@ -0,0 +1,42 @@ +#!/bin/bash + + +D#ATE=$(date +"%Y-%m-%d") +DATE=$(date +"%Y-%m-%d") +LOG_FILE="../logs/system_report_$DATE.log" +mkdir -p "$(dirname "$LOG_FILE")" + +echo "====================================================================" +echo " SYSTEM RESOURCE MONITOR " | tee -a "$LOG_FILE" +echo "====================================================================" +echo "" +df -h | tee -a "$LOG_FILE" + +echo "" + +echo "************************ Memory Usage ************************" | tee -a "$LOG_FILE" +free -m | tee -a "$LOG_FILE" + +echo "" + +echo "************************ CPU Load ************************" | tee -a "$LOG_FILE" +uptime | tee -a "$LOG_FILE" + +echo "" + +# Disk warning +USAGE=$(df / | awk 'NR==2 {print $5}' | sed 's/%//') +if (( USAGE > 80 )); then + echo "WARNING: Disk usage is above 80%" | tee -a "$LOG_FILE" +fi + +echo "" + +# Process count +echo "************************ Running processes: $(ps aux | wc -l)" | tee -a "$LOG_FILE" + +echo "" + +# Top 5 memory-consuming processes +echo "************************ Top 5 memory-consuming processes ************************" | tee -a "$LOG_FILE" + ps aux --sort=-%mem | head -n 6 | tee -a "$LOG_FILE" diff --git a/scripts/user_info.sh b/scripts/user_info.sh new file mode 100755 index 0000000..326cd3e --- /dev/null +++ b/scripts/user_info.sh @@ -0,0 +1,63 @@ +#!/bin/bash + +LOG_FILE="../logs/user_info.log" + +# This checks if the log directory exists and creates if not +mkdir -p "$(dirname "$LOG_FILE")" + +echo "===========================================" +echo " Please answer all the questions below " +echo "===========================================" + +# writes the responses to the log file +log_message() { + echo "$1" | tee -a "$LOG_FILE" +} + +# Gets the user name (loop until valid data is entered) +while true; do + read -p "Tell us your name: " name + if [[ -n "$name" ]]; then + break + else + echo "Name cannot be empty. Please try again." + fi +done + +# Get age (loop until valid) +while true; do + read -p "What is your age?: " age + if [[ "$age" =~ ^[0-9]+$ ]]; then + break + else + echo "Age must be a number. Please try again." + fi +done + +# Get country (loop until valid) +while true; do + read -p "What country are you from?: " country + if [[ -n "$country" ]]; then + break + else + echo "Country cannot be empty. Please try again." + fi +done + +# Determine age category +if (( age < 18 )); then + category="Minor" +elif (( age <= 65 )); then + category="Adult" +else + category="Senior" +fi + +# Final message +message="Hi $name from $country! +You are a: $category" + +echo +log_message "$message" + +echo "Your information has been saved!"