File: //bigscoots/wpo/redis_manager.sh
#!/bin/bash
# BigScoots Redis Manager v2.2 - Portal & Performance Optimized
COMMAND=$1
DOMAIN=$2
ARG=$3 # Used for --overwrite or memory values
# Source BigScoots common includes
source /bigscoots/includes/common.sh
# Pathing
SOCKET_PATH="/var/run/redis/redis-${DOMAIN}.sock"
CONFIG_FILE="/etc/redis/redis-${DOMAIN}.conf"
DATA_DIR="/var/lib/redis-${DOMAIN}"
WP_PATH="/home/nginx/domains/${DOMAIN}/public"
respond() {
echo "{\"status\": \"$1\", \"domain\": \"$DOMAIN\", \"message\": \"$2\"}"
[[ "$1" == "error" ]] && exit 1
}
if [[ -z "$DOMAIN" ]]; then
respond "error" "No domain provided."
fi
case $COMMAND in
deploy_instance)
# 1. Idempotency Check
if [[ -f "$CONFIG_FILE" ]]; then
respond "success" "Redis instance already exists for this domain. No changes made."
exit 0
fi
# 2. Systemd Template Check
if [[ ! -f "/etc/systemd/system/[email protected]" ]]; then
cat <<EOF > /etc/systemd/system/[email protected]
[Unit]
Description=Redis instance for %I
After=network.target
[Service]
Type=notify
User=redis
Group=redis
ExecStartPre=/usr/bin/mkdir -p /var/run/redis
ExecStartPre=/usr/bin/chown redis:redis /var/run/redis
ExecStart=/usr/bin/redis-server /etc/redis/redis-%i.conf
Restart=always
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
fi
# 3. Provision Directories & Group Membership
mkdir -p "$DATA_DIR"
chown redis:redis "$DATA_DIR"
usermod -a -G redis nginx > /dev/null 2>&1
# 4. Generate Config with BigScoots Performance Sauce
# Filter existing keys to prevent duplicates from the system default
grep -vE '^(port|bind|dir|pidfile|logfile|dbfilename|daemonize|supervised|maxmemory|maxmemory-policy|save|unixsocket|unixsocketperm|lazyfree|activedefrag)' /etc/redis/redis.conf > "$CONFIG_FILE"
cat <<EOF >> "$CONFIG_FILE"
# BigScoots Dedicated Instance Overrides
port 0
unixsocket $SOCKET_PATH
unixsocketperm 770
dir $DATA_DIR
pidfile /var/run/redis/redis-${DOMAIN}.pid
logfile /var/log/redis/redis-${DOMAIN}.log
dbfilename dump-${DOMAIN}.rdb
daemonize no
supervised systemd
save ""
# Memory Management
maxmemory 512mb
maxmemory-policy allkeys-lfu
# BigScoots Performance Suite (Lazy Freeing & Defrag)
lazyfree-lazy-eviction yes
lazyfree-lazy-expire yes
lazyfree-lazy-server-del yes
replica-lazy-flush yes
activedefrag yes
EOF
chown redis:redis "$CONFIG_FILE"
systemctl daemon-reload
systemctl enable --now "redis@${DOMAIN}" > /dev/null 2>&1
respond "success" "Redis instance deployed via Unix Socket with full performance suite."
;;
configure_wp)
OVERWRITE=$ARG
if [[ ! -d "$WP_PATH" ]]; then
respond "error" "WordPress directory not found at $WP_PATH"
fi
# 1. Check if ANY Redis config exists (Host OR Path)
HAS_HOST=$(wpcli config get WP_REDIS_HOST --path="$WP_PATH" > /dev/null 2>&1 && echo "yes" || echo "no")
HAS_PATH=$(wpcli config get WP_REDIS_PATH --path="$WP_PATH" > /dev/null 2>&1 && echo "yes" || echo "no")
if [[ "$HAS_HOST" == "yes" || "$HAS_PATH" == "yes" ]]; then
if [[ "$OVERWRITE" != "--overwrite" ]]; then
respond "success" "Redis configuration already exists. Use --overwrite to migrate to dedicated socket."
exit 0
fi
fi
# 2. Migration Cleanup: Delete old TCP defines
wpcli config delete WP_REDIS_HOST --path="$WP_PATH" --quiet || true
wpcli config delete WP_REDIS_PORT --path="$WP_PATH" --quiet || true
# 3. Set Essential Dedicated Constants
wpcli config set WP_REDIS_SCHEME "unix" --path="$WP_PATH" --quiet
wpcli config set WP_REDIS_PATH "$SOCKET_PATH" --path="$WP_PATH" --quiet
wpcli config set WP_REDIS_DATABASE 0 --raw --path="$WP_PATH" --quiet
wpcli config set WP_REDIS_PREFIX "${DOMAIN}" --path="$WP_PATH" --quiet
# Stability Timeouts
wpcli config set WP_REDIS_TIMEOUT 1 --raw --path="$WP_PATH" --quiet
wpcli config set WP_REDIS_READ_TIMEOUT 1 --raw --path="$WP_PATH" --quiet
# 4. Plugin Activation & Drop-in Linking
wpcli plugin install redis-cache --activate --allow-root --path="$WP_PATH" --quiet > /dev/null 2>&1
# The --force is key here to update existing object-cache.php files
wpcli redis enable --allow-root --path="$WP_PATH" --force --quiet > /dev/null 2>&1
chown -R nginx: "$WP_PATH/wp-content/"
respond "success" "wp-config.php updated to dedicated socket and object-cache.php drop-in linked."
;;
update_memory)
NEW_MEM=$ARG
if [[ ! "$NEW_MEM" =~ ^[0-9]+$ ]]; then
respond "error" "Memory must be a numeric value in MB (e.g., 1024)."
fi
if [[ ! -f "$CONFIG_FILE" ]]; then
respond "error" "Redis instance configuration not found for $DOMAIN."
fi
# Update running instance
redis-cli -s "$SOCKET_PATH" CONFIG SET maxmemory "${NEW_MEM}mb" > /dev/null 2>&1
# Persist change to the config file
sed -i "/^maxmemory /c\maxmemory ${NEW_MEM}mb" "$CONFIG_FILE"
respond "success" "Memory limit updated to ${NEW_MEM}MB for both running instance and config file."
;;
*)
echo "Usage: $0 {deploy_instance|configure_wp|update_memory} domain.com [arg]"
exit 1
;;
esac