Add comprehensive user permission validation and clear error messages

Improvements:

1. Enhanced root permission check (Lines 24-37)
   - Clear error message explaining why root is required
   - Lists all permission-required operations:
     - Read access to /var/lib/mysql
     - Create directories in /home
     - Change file ownership
     - Start mysqld daemon
     - Access system config files
   - Provides sudo command suggestion

2. MySQL data directory read permission check (Lines 189-231)
   - Validates read access to detected MySQL directory
   - Checks after each detection method (running MySQL, config, default)
   - Provides helpful error message if permission denied
   - Suggests running with sudo

3. Clear error messaging throughout
   - Users now understand WHY permission is denied
   - Actionable guidance (use sudo)
   - Consistent error format

Impact:
- Prevents confusing silent failures deep in workflow
- Users immediately know if they need to use sudo
- Better debugging experience
- Professional error handling

Before: User runs script, goes through 3 steps, then fails with:
        "Permission denied" with no context

After: User immediately sees:
       "PERMISSION DENIED: This script must be run as root"
       Lists exact reasons why
       Suggests: "sudo ./script.sh"

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
cschantz
2026-02-11 17:05:06 -05:00
parent 5f1f2a3c03
commit 5124af4e21
+31 -1
View File
@@ -23,7 +23,19 @@ source "$SCRIPT_DIR/lib/system-detect.sh"
# Root check
if [ "$EUID" -ne 0 ]; then
print_error "This script must be run as root"
echo ""
print_error "PERMISSION DENIED: This script must be run as root"
echo ""
echo "Why root is required:"
echo " - Read access to live MySQL data directory (/var/lib/mysql)"
echo " - Create directories in /home (for temporary restore location)"
echo " - Change file ownership to mysql:mysql"
echo " - Start MySQL daemon (mysqld) process"
echo " - Access system configuration files"
echo ""
echo "To run this script:"
echo " sudo $0 $*"
echo ""
exit 1
fi
@@ -182,6 +194,12 @@ detect_mysql_datadir() {
LIVE_DATADIR=$(mysql -NBe 'SELECT @@datadir;' 2>/dev/null)
if [ -n "$LIVE_DATADIR" ]; then
echo " Detected from running MySQL: $LIVE_DATADIR"
# Verify we can read this directory
if [ ! -r "$LIVE_DATADIR" ]; then
print_error "Cannot read MySQL data directory: Permission denied"
print_info "Try running this script with: sudo $0"
return 1
fi
return 0
fi
fi
@@ -191,6 +209,12 @@ detect_mysql_datadir() {
if [ -n "$config_dir" ]; then
LIVE_DATADIR="$config_dir"
echo " Detected from config: $LIVE_DATADIR"
# Verify we can read this directory
if [ ! -r "$LIVE_DATADIR" ]; then
print_error "Cannot read MySQL data directory: Permission denied"
print_info "Try running this script with: sudo $0"
return 1
fi
return 0
fi
@@ -198,6 +222,12 @@ detect_mysql_datadir() {
if [ -d "/var/lib/mysql" ]; then
LIVE_DATADIR="/var/lib/mysql"
echo " Using default: $LIVE_DATADIR"
# Verify we can read this directory
if [ ! -r "$LIVE_DATADIR" ]; then
print_error "Cannot read MySQL data directory: Permission denied"
print_info "Try running this script with: sudo $0"
return 1
fi
return 0
fi