Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

# nfiguration
BACKUP_DIR="backups"
LOG_FILE="logs/backup.log"
SOURCE_DIR=$1

# 1. Validation: Check if a directory was provided and if it exists
if [ -z "$SOURCE_DIR" ]; then
echo "Usage: $0 <directory_to_backup>"
exit 1
fi

if [ ! -d "$SOURCE_DIR" ]; then
echo "Error: Directory '$SOURCE_DIR' does not exist." | tee -a "$LOG_FILE"
exit 1
fi
# Create backup directory if it's missing
mkdir -p "$BACKUP_DIR"

# 2. Create the compressed backup
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_NAME="backup_${TIMESTAMP}.tar.gz"

echo "Creating backup of $SOURCE_DIR..."
tar -czf "${BACKUP_DIR}/${BACKUP_NAME}" "$SOURCE_DIR"

if [ $? -eq 0 ]; then
echo "$(date): Success - Backed up $SOURCE_DIR to $BACKUP_NAME" >> "$LOG_FILE"
echo "Backup saved: ${BACKUP_DIR}/${BACKUP_NAME}"
else
echo "$(date): Failure - Backup of $SOURCE_DIR failed" >> "$LOG_FILE"
exit 1
fi

# 3. Retention: Keep only the last 5 backups
# ls -t sorts by time (newest first): tail -n +6 selects everything after the 5th file
ls -t "$BACKUP_DIR"/backup_*.tar.gz | tail -n +6 | xargs -r rm

echo "Cleanup complete. Keeping only the 5 most recent backups."


60 changes: 60 additions & 0 deletions file_manager.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash

# Correctly define the log file path
LOG="logs/system_report_$(date +%Y%m%d_%H%M).log"
mkdir -p logs

# Assign argument to readable variables
action=$1
file1=$2
file2=$3

# Stops the scripts if the filename ($file1)is missing
if [ -z "$file1" ] && [ "$action" != "list" ]; then
echo "Error: Missing filename: ./file_manager.sh create [filename]"
exit 1
fi

case $action in
"create")
# check if file already exists to prevent overwriting
if [ -f "$file1" ]; then
echo "Error: $file1 already exist." | tee -a "$LOG"
else
touch "$file1"
echo "$(date): Created $file1" >> "$LOG"
echo "File '$file' created."
fi
;;

"delete")
if [ -f "$file1" ]; then
rm "$file1"
echo "$(date): Deleted $file1" >> "$LOG"
echo " File '$file1' deleted."
else
echo "Error: '$file1' does not exist." | tee -a "$LOG"
fi
;;

"list")
echo "$(date): Listed directory contents" >> "$LOG"
ls -lh
;;

"rename")
if [ -f "$file1" ]; then
mv "$file1" "$file2"
echo "$(date): Renamed $file1 to $file2" >> "$LOG"
echo "Renamed $file1 to $file2."
else
echo "Error: Source file '$file1' not found." | tee -a "$LOG"
fi
;;

*)
echo "Usage: $0 {create|delete|list|rename} [filename] [new_filename]"
;;
esac


22 changes: 22 additions & 0 deletions process_monitor.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

# Define the list of services to check
# Make sure to use the space between the names, not commas!
services=("nginx" "ssh" "docker")

echo "--- Starting Process Monitor ---"

# Loop through each service in the array
for services in "${services[@]}"; do
# pgrep looks for the process ID (PID)
if pgrep "$service" > /dev/null; then
echo "[RUNNING] $service is active."
else
echo "[STOPPED] $service is down! Attempting restart..."
# In a real environment, you'd use 'sudo systemtl restart $service'
# For the assignment, we simulate the restart
echo "[RESTARTED] $service has been brought back online."
fi
done


44 changes: 44 additions & 0 deletions run_all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash

# Interactive Menu for Bash Assignment

while true; do
echo "================================"
echo " DEVOPS AUTOMATION MENU "
echo "================================"
echo "1. Run System Check (Section B)"
echo "2. Run File Manger (Section C)"
echo "3. Run Directory Backup (Section D)"
echo "4. Run Monitor Services (Section E)"
echo "5. Exit"
echo "================================"
read -p "Enter your choice [1-5]: " choice

case $choice in
1)
./system_check.sh
;;
2)
read -p "Enter action(create/delete/list): " act
read -p "Enter filename: " fname
./file_manager.sh "$act" "$fname"
;;
3)
read -p "Enter directory to backup: " dir
./backup.sh "$dir"
;;
4)
./process_monitor.sh
;;
5)
echo "Exiting. Goodbye!"
break
;;
*)
echo "Invalid option. Please try again."
;;
esac
echo -e "\nPress Enter to return to menu..."
read
done

35 changes: 35 additions & 0 deletions system_check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

# Define the log file with a time stamp
LOG_FILE="system_report_$(date +%Y%m%d_%H%M).log"
echo "--- Generating system report---" | tee -a $LOG_FILE

# 1: Disk usauge (df -h)
echo -e "\nDisk Space Usage:" >> "$LOG_FILE"
df -h | grep '^/dev/' >> "$LOG_FILE"

# Alert for disk usage
usage=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g')
if [ "$usage" -gt 80 ]; then
echo " WARNING: Disk usage is at ${usage}%!" >> "$LOG_FILE"
fi

# 2: Memory usage (free -m)
echo -e "\nMMemory Usage:" >> "$LOG_FILE"
free -h >> "$LOG_FILE"

# 3: CPU load (uptime)
echo "CPU Uptime:" >> "$LOG_FILE"
uptime >> "$LOG_FILE"

# 4: Count total running processes
echo -e "\nTotal Running Processes:" >> "$LOG_FILE"
ps aux | wc -l >> "$LOG_FILE"

# 5: Display top 5 memory consuming processe
echo -e "\nTop 5 Memory-Consuming Processes:" >> "$LOG_FILE"
# ps aux : list proceses, --sort=-%mem: highest memory firt, head -n 6: top 5 + header
ps aux --sort=-%mem | head -n 6 >> "$LOG_FILE"

echo " Report saved to $LOG_FILE"

27 changes: 27 additions & 0 deletions user_info.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

# Task 1: Collecting dats
read -p "Emter your full name" name
read -p "Enter your age" age
read -p "Enter your country" coumtry

# Task 2: Validation and classification of age
if [[ ! "$age" =~ [0-9]+$ ]]; then
echo "Error: Age must be a number"
exit 1
fi

if [[ "$age" -lt 18 ]]; then
category="Minor"
elif [[ "$age" -le 65 ]]; then
category="Adult"
else category="Senior"

fi

# Task 4: Output
echo "Hello $name from $country. you are classified as: $category."

# Task 5:
# save this to a log file automatically
echo "($date): $name ($category) from $country" >> logs/user_info.log"