diff --git a/prepare-email-storage.sh b/prepare-email-storage.sh new file mode 100644 index 0000000..5004554 --- /dev/null +++ b/prepare-email-storage.sh @@ -0,0 +1,125 @@ +#!/bin/bash +# filepath: prepare-email-storage.sh + +# Prepare mounted drive at /mnt/MainEmail for email storage +# Run this script before running the main setup script + +set -euo pipefail + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +MAIL_ROOT="/mnt/MainEmail" + +error() { + echo -e "${RED}ERROR: $1${NC}" >&2 + exit 1 +} + +info() { + echo -e "${BLUE}INFO: $1${NC}" +} + +success() { + echo -e "${GREEN}SUCCESS: $1${NC}" +} + +warning() { + echo -e "${YELLOW}WARNING: $1${NC}" +} + +# Check if running as root +if [[ $EUID -ne 0 ]]; then + error "This script must be run as root" +fi + +# Check if the mount point exists and is mounted +info "Checking mount point $MAIL_ROOT..." + +if [[ ! -d "$MAIL_ROOT" ]]; then + error "Directory $MAIL_ROOT does not exist. Please ensure your drive is mounted there." +fi + +# Check if it's actually a mount point +if ! mountpoint -q "$MAIL_ROOT"; then + warning "$MAIL_ROOT exists but is not a mount point. This might be just a regular directory." + echo "If this is intentional, press ENTER to continue. Otherwise, press Ctrl+C to abort." + read -r +fi + +# Display current mount information +info "Current mount information for $MAIL_ROOT:" +df -h "$MAIL_ROOT" +echo + +# Check available space +AVAILABLE_SPACE=$(df --output=avail "$MAIL_ROOT" | tail -n1) +AVAILABLE_GB=$((AVAILABLE_SPACE / 1024 / 1024)) + +info "Available space: ${AVAILABLE_GB}GB" + +if [[ $AVAILABLE_GB -lt 10 ]]; then + warning "Only ${AVAILABLE_GB}GB available. Consider having at least 10GB for email storage." +fi + +# Create vmail user and group if they don't exist +info "Creating vmail user and group..." +groupadd -g 5000 vmail 2>/dev/null || { + if getent group vmail >/dev/null; then + info "Group vmail already exists" + else + error "Failed to create vmail group" + fi +} + +useradd -g vmail -u 5000 vmail -d "$MAIL_ROOT" -s /bin/false 2>/dev/null || { + if getent passwd vmail >/dev/null; then + info "User vmail already exists" + # Update home directory if needed + usermod -d "$MAIL_ROOT" vmail + else + error "Failed to create vmail user" + fi +} + +# Set ownership and permissions on the mail root +info "Setting ownership and permissions on $MAIL_ROOT..." +chown vmail:vmail "$MAIL_ROOT" +chmod 755 "$MAIL_ROOT" + +# Create basic directory structure +info "Creating basic directory structure..." +mkdir -p "$MAIL_ROOT"/{domains,backup,logs} +chown -R vmail:vmail "$MAIL_ROOT" +chmod -R 755 "$MAIL_ROOT" + +# Test write permissions +info "Testing write permissions..." +if sudo -u vmail touch "$MAIL_ROOT/test_write" 2>/dev/null; then + rm -f "$MAIL_ROOT/test_write" + success "Write permissions test passed" +else + error "vmail user cannot write to $MAIL_ROOT" +fi + +# Display final status +success "Email storage preparation completed!" +echo +echo -e "${BLUE}=== Storage Summary ===${NC}" +echo "Mount point: $MAIL_ROOT" +echo "Owner: vmail:vmail (UID:5000, GID:5000)" +echo "Permissions: 755" +echo "Available space: ${AVAILABLE_GB}GB" +echo +echo -e "${BLUE}=== Directory Structure Created ===${NC}" +echo "$MAIL_ROOT/" +echo "├── domains/ (for domain-specific mailboxes)" +echo "├── backup/ (for backup storage)" +echo "└── logs/ (for mail-related logs)" +echo +echo -e "${GREEN}Ready for email server setup!${NC}" +echo "You can now run the main setup script: ./setup-email-server.sh" \ No newline at end of file