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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<<<<<<< HEAD
# 🚀 DevOps Bash Toolkit Assessment

![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/your-username/devops-bash-toolkit/grade.yml)
Expand Down Expand Up @@ -228,3 +229,13 @@ logs/app.log
* Handle script failures gracefully

**Submission link:** [CLICK HERE](https://forms.gle/jrhpKjXsQXZxLopN6)
=======
This assignment tests my DevOps fundamentals:

Bash scripting
Git workflow (branching, commits, pull requests)
Automation mindset
System monitoring and logging

I will build a real-world automation toolkit and submit it via a Pull Request (PR).
>>>>>>> 3e1f32f (feat: complete bash scripts)
1 change: 1 addition & 0 deletions bash-assignment
Submodule bash-assignment added at bd24f9
224 changes: 224 additions & 0 deletions run_all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
#!/bin/bash
set -euo pipefail

# Create necessary directories
mkdir -p scripts logs

# Global log file
logfile="logs/app.log"

# Function to log actions
log_action() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$logfile"
}

# Function to display banner
show_banner() {
cat << "EOF"
========================================
SYSTEM MANAGEMENT DASHBOARD
========================================
EOF
}

# Function to check if scripts exist
check_scripts() {
local missing=0

if [[ ! -f "scripts/user_info.sh" ]]; then
echo "Warning: scripts/user_info.sh not found" >&2
missing=1
fi
if [[ ! -f "scripts/system_report.sh" ]]; then
echo "Warning: scripts/system_check.sh not found" >&2
missing=1
fi
if [[ ! -f "scripts/file_manager.sh" ]]; then
echo "Warning: scripts/file_manager.sh not found" >&2
missing=1
fi
if [[ ! -f "scripts/backup.sh" ]]; then
echo "Warning: scripts/backup.sh not found" >&2
missing=1
fi
if [[ ! -f "scripts/process_monitor.sh" ]]; then
echo "Warning: scripts/process_monitor.sh not found" >&2
missing=1
fi

return $missing
}

# Function to run all scripts
run_all() {
echo ""
echo "========================================="
echo "RUNNING ALL SCRIPTS"
echo "========================================="
log_action "Starting 'Run All' operation"

# Run user_info.sh
echo -e "\n[1/5] Running user_info.sh..."
if [[ -f "scripts/user_info.sh" ]]; then
bash scripts/user_info.sh
log_action "Executed: user_info.sh"
else
echo "Skipped: user_info.sh not found"
log_action "FAILED: user_info.sh not found"
fi

# Run system_check.sh
echo -e "\n[2/5] Running system_check.sh..."
if [[ -f "scripts/system_check.sh" ]]; then
bash scripts/system_check.sh
log_action "Executed: system_check.sh"
else
echo "Skipped: system_check.sh not found"
log_action "FAILED: system_check.sh not found"
fi

# Run file_manager.sh (example: create a test file)
echo -e "\n[3/5] Running file_manager.sh..."
if [[ -f "scripts/file_manager.sh" ]]; then
echo " Creating test file..."
bash scripts/file_manager.sh create test_$(date +%s).txt 2>/dev/null || true
bash scripts/file_manager.sh list 2>/dev/null || true
log_action "Executed: file_manager.sh"
else
echo "Skipped: file_manager.sh not found"
log_action "FAILED: file_manager.sh not found"
fi

# Run backup.sh
echo -e "\n[4/5] Running backup.sh..."
if [[ -f "scripts/backup.sh" ]]; then
# Backup current directory (or specify a test directory)
mkdir -p test_backup_dir
echo "Test content" > test_backup_dir/test.txt
bash scripts/backup.sh test_backup_dir 2>/dev/null || true
log_action "Executed: backup.sh"
else
echo "Skipped: backup.sh not found"
log_action "FAILED: backup.sh not found"
fi

# Run process_monitor.sh
echo -e "\n[5/5] Running process_monitor.sh..."
if [[ -f "scripts/process_monitor.sh" ]]; then
bash scripts/process_monitor.sh ssh 2>/dev/null || true
log_action "Executed: process_monitor.sh"
else
echo "Skipped: process_monitor.sh not found"
log_action "FAILED: process_monitor.sh not found"
fi

echo -e "\n✓ All scripts completed!"
log_action "Completed 'Run All' operation"
}

# Function to run system check
system_check() {
echo ""
echo "========================================="
echo "SYSTEM CHECK"
echo "========================================="
log_action "Starting 'System Check' operation"

if [[ -f "scripts/system_check.sh" ]]; then
bash scripts/system_check.sh
log_action "Executed: system_check.sh"
else
echo "Error: scripts/system_check.sh not found"
log_action "FAILED: system_check.sh not found"
return 1
fi

log_action "Completed 'System Check' operation"
}

# Function to run backup
run_backup() {
echo ""
echo "========================================="
echo "BACKUP OPERATION"
echo "========================================="
log_action "Starting 'Backup' operation"

# Prompt for directory to backup
read -p "Enter directory path to backup: " backup_dir

if [[ -z "$backup_dir" ]]; then
echo "Error: No directory provided. Using current directory."
backup_dir="."
fi

if [[ -f "scripts/backup.sh" ]]; then
bash scripts/backup.sh "$backup_dir"
log_action "Executed: backup.sh for directory '$backup_dir'"
else
echo "Error: scripts/backup.sh not found"
log_action "FAILED: backup.sh not found"
return 1
fi

log_action "Completed 'Backup' operation"
}

# Function to display menu
show_menu() {
echo ""
echo "========================================="
echo " MAIN MENU"
echo "========================================="
echo "1. Run All Scripts"
echo "2. System Check (Disk/Memory/CPU)"
echo "3. Backup Directory"
echo "4. Exit"
echo "========================================="
echo -n "Enter your choice [1-4]: "
}

# Main program loop
main() {
show_banner

# Check if scripts directory has required scripts
check_scripts

while true; do
show_menu
read choice

case "$choice" in
1)
run_all
;;
2)
system_check
;;
3)
run_backup
;;
4)
echo ""
echo "Exiting... Goodbye!"
log_action "User exited the application"
exit 0
;;
*)
echo "Error: Invalid choice. Please enter 1, 2, 3, or 4" >&2
log_action "ERROR: Invalid menu choice '$choice'"
;;
esac

# Pause before showing menu again
echo ""
read -p "Press Enter to continue..."
done
}

# Trap Ctrl+C for clean exit
trap 'echo ""; echo "Interrupted by user"; log_action "Application interrupted (SIGINT)"; exit 0' INT

# Run main function
main
77 changes: 77 additions & 0 deletions scripts/backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/bash
set -euo pipefail

# Create necessary directories
mkdir -p backups logs

logfile="logs/backup.log"

# Function to log actions
log_action() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$logfile"
}

# Function to display usage
usage() {
echo "Usage: $0 <directory>"
echo "Example: $0 /home/user/documents"
exit 1
}

# Check if directory argument is provided
if [[ $# -ne 1 ]]; then
echo "Error: Please provide exactly one directory path" >&2
usage
fi

source_dir="$1"

# Validate that directory exists
if [[ ! -d "$source_dir" ]]; then
echo "Error: Directory '$source_dir' does not exist" >&2
log_action "FAILED: Backup - Directory '$source_dir' not found"
exit 1
fi

# Create timestamp and backup filename
timestamp=$(date '+%Y%m%d_%H%M%S')
backup_name="backup_${timestamp}.tar.gz"
backup_path="backups/${backup_name}"

# Create compressed backup
echo "Creating backup of: $source_dir"
log_action "Starting backup of '$source_dir'"

if tar -czf "$backup_path" -C "$(dirname "$source_dir")" "$(basename "$source_dir")" 2>/dev/null; then
backup_size=$(du -h "$backup_path" | cut -f1)
echo "✓ Backup created: $backup_path ($backup_size)"
log_action "SUCCESS: Created $backup_name (Size: $backup_size) from '$source_dir'"
else
echo "Error: Failed to create backup" >&2
log_action "FAILED: Could not create backup of '$source_dir'"
exit 1
fi

# Keep only the last 5 backups (delete older ones)
echo "Checking for old backups to remove..."
log_action "Checking backups in 'backups/' directory"

# List backups sorted by name (timestamp descending), skip first 5, delete rest
old_backups=$(ls -1 backups/backup_*.tar.gz 2>/dev/null | sort -r | tail -n +6)

if [[ -n "$old_backups" ]]; then
while IFS= read -r old_backup; do
rm "$old_backup"
echo " Removed old backup: $(basename "$old_backup")"
log_action "REMOVED: $(basename "$old_backup") - Keeping only last 5 backups"
done <<< "$old_backups"
else
echo " No old backups to remove (less than 5 total)"
fi

# Show current backups count
backup_count=$(ls -1 backups/backup_*.tar.gz 2>/dev/null | wc -l)
echo "✓ Current backups in 'backups/': $backup_count (max: 5)"
log_action "Backup cleanup complete. Total backups kept: $backup_count"

echo "Backup process finished successfully!"
Empty file added scripts/blue.txt
Empty file.
Loading