nas08-scripts/nextcloud/nextcloud_update_check.sh

398 lines
17 KiB
Bash

#!/bin/bash
# =============================================================
# nextcloud_update_check.sh
# Checks for Nextcloud CORE and app updates daily.
# Sends an HTML email report with update commands ready to
# copy-paste into terminal.
#
# Schedule: Daily (recommended 7:00 AM)
# Install: /usr/scripts/omv/nextcloud_update_check.sh
# Log: /var/log/nextcloud-updates/
# =============================================================
# --- Config --------------------------------------------------
CONTAINER="nextcloud"
OCC="sudo docker exec -u www-data ${CONTAINER} php occ"
NEXTCLOUD_COMPOSE_DIR="/kingdezignsnas/Docker/Compose/nextcloud"
NEXTCLOUD_COMPOSE_FILES="-f nextcloud.yml -f compose.override.yml"
NEXTCLOUD_FFMPEG_BUILD_DIR="/kingdezignsnas/Docker/Compose/nextcloud-ffmpeg-build"
NEXTCLOUD_FFMPEG_BUILD_FILES="-f nextcloud-ffmpeg-build.yml --env-file nextcloud-ffmpeg-build.env"
NEXTCLOUD_PULL_SERVICES="db onlyoffice autoheal"
SMTP_SERVER="smtppro.zoho.com"
SMTP_PORT="465"
SMTP_USER="rufus.king@kingdezigns.com"
SMTP_PASS_FILE="/etc/nextcloud-smtp-pass"
FROM_NAME="NAS08"
FROM_EMAIL="rufus.king@kingdezigns.com"
TO_EMAIL="rufus.king@kingdezigns.com"
HOSTNAME_LABEL="NAS08"
LOG_DIR="/var/log/nextcloud-updates"
LOG_RETENTION_DAYS=90
# -------------------------------------------------------------
mkdir -p "$LOG_DIR"
DATESTAMP=$(date +"%Y%m%d")
TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")
LOGFILE="$LOG_DIR/update-check-${DATESTAMP}.log"
log() { echo "[$(date +"%H:%M:%S")] $*" | tee -a "$LOGFILE"; }
# Returns 0 when $1 is strictly newer than $2 (e.g. 34.0.3 > 34.0.2).
version_gt() {
local ver1=$1 ver2=$2
[[ -n "$ver1" && -n "$ver2" ]] || return 1
local IFS=.
local -a v1=($ver1) v2=($ver2)
local i n1 n2
for ((i=0; i<${#v1[@]} || i<${#v2[@]}; i++)); do
n1=$((10#${v1[i]:-0}))
n2=$((10#${v2[i]:-0}))
if (( n1 > n2 )); then return 0; fi
if (( n1 < n2 )); then return 1; fi
done
return 1
}
# Adjusted to strip any "v" prefix or extra spaces cleanly
fetch_latest_github_release() {
curl -sf --max-time 20 "https://api.github.com/repos/nextcloud/server/releases/latest" \
| grep -oE '"tag_name"[[:space:]]*:[[:space:]]*"v[0-9]+\.[0-9]+\.[0-9]+"' \
| head -1 \
| grep -oE '[0-9]+\.[0-9]+\.[0-9]+'
}
# --- Load SMTP password --------------------------------------
if [[ ! -f "$SMTP_PASS_FILE" ]]; then
log "ERROR: SMTP password file not found at $SMTP_PASS_FILE"
exit 1
fi
SMTP_PASS=$(cat "$SMTP_PASS_FILE")
# --- Check container is running ------------------------------
if ! docker ps --format '{{.Names}}' | grep -q "^${CONTAINER}$"; then
log "ERROR: Container '${CONTAINER}' is not running."
STATUS_COLOR="#b91c1c"
STATUS_LABEL="CRITICAL"
STATUS_MSG="⛔ &nbsp;Nextcloud container is not running"
BODY_HTML="<p style='color:#b91c1c;font-weight:600;'>The Nextcloud Docker container was not found running on ${HOSTNAME_LABEL}. No update check could be performed.</p>"
NEEDS_ACTION=true
APP_ROWS=""
CMD_BLOCK=""
else
# --- Get Nextcloud status ----------------------------------
log "Checking Nextcloud status..."
NC_STATUS=$($OCC status 2>/dev/null | grep -v '^{')
MAINTENANCE=$(echo "$NC_STATUS" | grep "maintenance:" | awk '{print $3}')
NEEDS_UPGRADE=$(echo "$NC_STATUS" | grep "needsDbUpgrade:" | awk '{print $3}')
NC_VERSION=$(echo "$NC_STATUS" | grep "versionstring:" | awk '{print $3}')
log "Nextcloud version: $NC_VERSION | maintenance: $MAINTENANCE | needsDbUpgrade: $NEEDS_UPGRADE"
# --- Check for CORE (server) update -------------------------
log "Checking core (server) update via occ update:check..."
CORE_CHECK_RAW=$($OCC update:check 2>/dev/null | grep -v '^{')
log "occ update:check raw output:"
while IFS= read -r line; do
[[ -n "$line" ]] && log " $line"
done <<< "$CORE_CHECK_RAW"
CORE_UPDATE_AVAILABLE=false
CORE_NEW_VERSION=""
CORE_UPDATE_SOURCE=""
if echo "$CORE_CHECK_RAW" | grep -qiE 'Nextcloud [0-9]+\.[0-9]+\.[0-9]+ is available'; then
CORE_UPDATE_AVAILABLE=true
CORE_NEW_VERSION=$(echo "$CORE_CHECK_RAW" | grep -oiE 'Nextcloud [0-9]+\.[0-9]+\.[0-9]+ is available' | head -1 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+')
CORE_UPDATE_SOURCE="occ update:check"
log "Core update available (occ): Nextcloud ${CORE_NEW_VERSION}"
fi
log "Checking core update via GitHub latest release fallback..."
GITHUB_LATEST=$(fetch_latest_github_release)
if [[ -n "$GITHUB_LATEST" ]]; then
log "GitHub latest stable release: ${GITHUB_LATEST} (installed: ${NC_VERSION})"
if version_gt "$GITHUB_LATEST" "$NC_VERSION"; then
if [[ "$CORE_UPDATE_AVAILABLE" != "true" ]] || version_gt "$GITHUB_LATEST" "$CORE_NEW_VERSION"; then
CORE_UPDATE_AVAILABLE=true
CORE_NEW_VERSION="$GITHUB_LATEST"
if [[ -z "$CORE_UPDATE_SOURCE" ]]; then
CORE_UPDATE_SOURCE="GitHub latest release (occ update:check lagged)"
else
CORE_UPDATE_SOURCE="${CORE_UPDATE_SOURCE} + GitHub latest release"
fi
log "Core update available (GitHub fallback): ${NC_VERSION} → ${CORE_NEW_VERSION}"
fi
fi
else
log "WARN: Could not fetch GitHub latest release for fallback compare."
fi
# --- Check for app updates --------------------------------
log "Checking app updates..."
APP_UPDATE_RAW=$($OCC app:update --all --showonly 2>/dev/null | grep -v '^{' )
UPDATABLE_APPS=()
while IFS= read -r line; do
if echo "$line" | grep -q "new version available"; then
APP_NAME=$(echo "$line" | awk '{print $1}')
NEW_VER=$(echo "$line" | awk '{print $NF}')
UPDATABLE_APPS+=("${APP_NAME}|${NEW_VER}")
log "Update available: $APP_NAME → $NEW_VER"
fi
done <<< "$APP_UPDATE_RAW"
APP_COUNT=${#UPDATABLE_APPS[@]}
# --- Build status -----------------------------------------
NEEDS_ACTION=false
WARNINGS=()
if [[ "$MAINTENANCE" == "true" ]]; then
WARNINGS+=("Nextcloud is currently in maintenance mode")
NEEDS_ACTION=true
fi
if [[ "$NEEDS_UPGRADE" == "true" ]]; then
WARNINGS+=("Database upgrade required (needsDbUpgrade: true)")
NEEDS_ACTION=true
fi
if [[ $APP_COUNT -gt 0 ]]; then
NEEDS_ACTION=true
fi
if [[ "$CORE_UPDATE_AVAILABLE" == "true" ]]; then
NEEDS_ACTION=true
fi
# --- Determine overall status color -----------------------
if [[ "$MAINTENANCE" == "true" ]] || [[ "$NEEDS_UPGRADE" == "true" ]]; then
STATUS_COLOR="#b91c1c"
STATUS_LABEL="CRITICAL"
STATUS_MSG="⛔ &nbsp;Nextcloud requires immediate attention"
elif [[ "$CORE_UPDATE_AVAILABLE" == "true" ]] && [[ $APP_COUNT -gt 0 ]]; then
STATUS_COLOR="#b45309"
STATUS_LABEL="UPDATES AVAILABLE"
STATUS_MSG="🔄 &nbsp;Core update to ${CORE_NEW_VERSION} + ${APP_COUNT} app update(s) available"
elif [[ "$CORE_UPDATE_AVAILABLE" == "true" ]]; then
STATUS_COLOR="#b45309"
STATUS_LABEL="UPDATES AVAILABLE"
STATUS_MSG="🔄 &nbsp;Nextcloud core update available (${NC_VERSION} → ${CORE_NEW_VERSION})"
elif [[ $APP_COUNT -gt 0 ]]; then
STATUS_COLOR="#b45309"
STATUS_LABEL="UPDATES AVAILABLE"
STATUS_MSG="🔄 &nbsp;${APP_COUNT} app update(s) available"
else
STATUS_COLOR="#1a7f4b"
STATUS_LABEL="UP TO DATE"
STATUS_MSG="✅ &nbsp;Nextcloud is fully up to date"
fi
if [[ "$MAINTENANCE" == "true" ]]; then
MAINTENANCE_COLOR="#b91c1c"
else
MAINTENANCE_COLOR="#1a7f4b"
fi
# --- Build app update rows --------------------------------
APP_ROWS=""
if [[ $APP_COUNT -gt 0 ]]; then
for entry in "${UPDATABLE_APPS[@]}"; do
APP_NAME=$(echo "$entry" | cut -d'|' -f1)
NEW_VER=$(echo "$entry" | cut -d'|' -f2)
APP_ROWS+="<tr>
<td style='padding:9px 14px;border-bottom:1px solid #e5e7eb;font-size:13px;color:#111827;font-family:monospace;'>${APP_NAME}</td>
<td style='padding:9px 14px;border-bottom:1px solid #e5e7eb;font-size:13px;color:#1a7f4b;font-weight:600;'>${NEW_VER}</td>
</tr>"
done
fi
# --- Build warning rows -----------------------------------
WARN_HTML=""
for w in "${WARNINGS[@]}"; do
WARN_HTML+="<div style='margin-top:10px;padding:10px 14px;background:#fff1f2;border-left:4px solid #b91c1c;border-radius:4px;'>
<span style='font-size:13px;color:#7f1d1d;font-weight:600;'>⚠ ${w}</span>
</div>"
done
# --- Build copy-paste command block -----------------------
CMD_LINES=""
if [[ "$MAINTENANCE" == "true" ]]; then
CMD_LINES+=$'# Disable maintenance mode\nsudo docker exec -u www-data nextcloud php occ maintenance:mode --off\n\n'
fi
if [[ "$CORE_UPDATE_AVAILABLE" == "true" ]]; then
# Corrected step to target the actual service container for your custom image
CMD_LINES+="# Update Nextcloud CORE to ${CORE_NEW_VERSION} (do this before app updates)"$'\n'
CMD_LINES+="# Step 1: Rebuild nextcloud-ffmpeg from a fresh nextcloud:latest base"$'\n'
CMD_LINES+="sudo bash -c \"cd ${NEXTCLOUD_FFMPEG_BUILD_DIR} && docker compose ${NEXTCLOUD_FFMPEG_BUILD_FILES} build --no-cache --pull\""$'\n\n'
CMD_LINES+="# Step 2: Pull supporting services only (skip nextcloud/cron — local image)"$'\n'
CMD_LINES+="sudo bash -c \"cd ${NEXTCLOUD_COMPOSE_DIR} && docker compose ${NEXTCLOUD_COMPOSE_FILES} pull ${NEXTCLOUD_PULL_SERVICES}\""$'\n\n'
CMD_LINES+="# Step 3: Recreate containers to pick up the rebuilt image"$'\n'
CMD_LINES+="sudo bash -c \"cd ${NEXTCLOUD_COMPOSE_DIR} && docker compose ${NEXTCLOUD_COMPOSE_FILES} up -d\""$'\n\n'
CMD_LINES+="# Give the container's built-in upgrade entrypoint time to finish,"$'\n'
CMD_LINES+="# then confirm the new version is active (watch for versionstring: ${CORE_NEW_VERSION})"$'\n'
CMD_LINES+="sleep 30"$'\n'
CMD_LINES+="sudo docker exec -u www-data nextcloud php occ status"$'\n\n'
fi
if [[ "$NEEDS_UPGRADE" == "true" ]]; then
CMD_LINES+=$'# Run database upgrade\nsudo docker exec -u www-data nextcloud php occ upgrade --no-interaction\n\n'
fi
if [[ $APP_COUNT -gt 0 ]]; then
CMD_LINES+=$'# Update all apps\nsudo docker exec -u www-data nextcloud php occ app:update --all\n\n'
fi
if [[ -z "$CMD_LINES" ]]; then
CMD_LINES="# No action required — everything is up to date"$'\n'
fi
CMD_LINES+=$'# Verify status after updates\nsudo docker exec -u www-data nextcloud php occ status'
CMD_BLOCK="<div style='margin-top:16px;'>
<div style='font-size:11px;font-weight:700;color:#374151;text-transform:uppercase;letter-spacing:.5px;margin-bottom:8px;'>📋 Commands to run on NAS08</div>
<pre style='margin:0;background:#1e293b;color:#e2e8f0;padding:14px;border-radius:6px;font-size:12px;line-height:1.6;overflow-x:auto;white-space:pre;'>${CMD_LINES}</pre>
</div>"
MAINT_BLOCK=""
if [[ -n "$WARN_HTML" ]]; then
MAINT_BLOCK="<div style='margin-top:20px;'>
<div style='font-size:11px;font-weight:700;color:#b91c1c;text-transform:uppercase;letter-spacing:.5px;margin-bottom:6px;'>⚠ Issues Detected</div>
${WARN_HTML}
</div>"
fi
CORE_BLOCK=""
if [[ "$CORE_UPDATE_AVAILABLE" == "true" ]]; then
CORE_SOURCE_NOTE=""
if [[ -n "$CORE_UPDATE_SOURCE" ]]; then
CORE_SOURCE_NOTE="<tr>
<td style='font-size:13px;color:#6b7280;padding-top:6px;'>Detected Via</td>
<td style='font-size:13px;color:#374151;padding-top:6px;'>${CORE_UPDATE_SOURCE}</td>
</tr>"
fi
CORE_BLOCK="<div style='margin-top:20px;background:white;border-radius:10px;border:1px solid #e5e7eb;padding:18px 22px;'>
<div style='font-size:13px;font-weight:700;color:#374151;margin-bottom:12px;text-transform:uppercase;letter-spacing:.5px;'>🆙 Core Update Available</div>
<table width='100%' cellspacing='0' cellpadding='0'>
<tr>
<td style='font-size:13px;color:#6b7280;padding-bottom:6px;'>Current Version</td>
<td style='font-size:13px;color:#111827;font-weight:600;padding-bottom:6px;font-family:monospace;'>${NC_VERSION}</td>
</tr>
<tr>
<td style='font-size:13px;color:#6b7280;'>New Version</td>
<td style='font-size:13px;color:#b45309;font-weight:700;font-family:monospace;'>${CORE_NEW_VERSION}</td>
</tr>
${CORE_SOURCE_NOTE}
</table>
<div style='margin-top:10px;font-size:12px;color:#6b7280;'>This install uses a custom <code style='font-family:monospace;'>nextcloud-ffmpeg:latest</code> image. The emailed commands rebuild that image first — do not run a plain <code style='font-family:monospace;'>docker compose pull</code> in the main project.</div>
</div>"
fi
APP_TABLE=""
if [[ $APP_COUNT -gt 0 ]]; then
APP_TABLE="<div style='margin-top:20px;background:white;border-radius:10px;border:1px solid #e5e7eb;padding:18px 22px;'>
<div style='font-size:13px;font-weight:700;color:#374151;margin-bottom:12px;text-transform:uppercase;letter-spacing:.5px;'>🔄 App Updates Available (${APP_COUNT})</div>
<table width='100%' cellspacing='0' cellpadding='0' style='border-collapse:collapse;font-size:13px;'>
<thead>
<tr>
<th style='text-align:left;padding:8px 14px;font-size:10px;text-transform:uppercase;letter-spacing:.06em;color:#9ca3af;background:#f9fafb;border-bottom:1px solid #e5e7eb;font-weight:500;'>App</th>
<th style='text-align:left;padding:8px 14px;font-size:10px;text-transform:uppercase;letter-spacing:.06em;color:#9ca3af;background:#f9fafb;border-bottom:1px solid #e5e7eb;font-weight:500;'>New Version</th>
</tr>
</thead>
<tbody>${APP_ROWS}</tbody>
</table>
</div>"
else
APP_TABLE="<div style='margin-top:20px;padding:12px 14px;background:#f0fdf4;border-radius:6px;border:1px solid #bbf7d0;'>
<span style='font-size:13px;color:#166534;font-weight:600;'>✓ All apps are up to date</span>
</div>"
fi
BODY_HTML="${MAINT_BLOCK}
<div style='margin-top:20px;padding:12px 14px;background:#f8fafc;border-radius:6px;border:1px solid #e5e7eb;'>
<span style='font-size:12px;font-weight:600;color:#374151;text-transform:uppercase;letter-spacing:.5px;'>Nextcloud Version</span>
<span style='margin-left:10px;font-size:13px;color:#374151;font-family:monospace;'>${NC_VERSION}</span>
<span style='margin-left:20px;font-size:12px;font-weight:600;color:#374151;text-transform:uppercase;letter-spacing:.5px;'>Maintenance Mode</span>
<span style='margin-left:10px;font-size:13px;color:${MAINTENANCE_COLOR};font-family:monospace;'>${MAINTENANCE:-false}</span>
</div>
${CORE_BLOCK}
${APP_TABLE}
${CMD_BLOCK}"
fi
if [[ "$NEEDS_ACTION" == "true" ]]; then
SUBJECT="[NAS08] Nextcloud — ${STATUS_LABEL} — Action Required"
else
SUBJECT="[NAS08] Nextcloud — All Up To Date"
fi
log "Composing email: $SUBJECT"
HTML_EMAIL=$(cat <<HTML
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1.0"></head>
<body style="margin:0;padding:0;background:#f1f5f9;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif;">
<table width="100%" cellspacing="0" cellpadding="0" style="background:#f1f5f9;padding:24px 16px;">
<tr><td align="center">
<table width="100%" cellspacing="0" cellpadding="0" style="max-width:720px;">
<!-- HEADER -->
<tr><td style="background:#0f172a;border-radius:10px 10px 0 0;padding:28px 32px;">
<table width="100%" cellspacing="0" cellpadding="0"><tr>
<td>
<div style="font-size:11px;color:#94a3b8;letter-spacing:1px;text-transform:uppercase;">Nextcloud · Daily Update Report</div>
<div style="font-size:26px;font-weight:800;color:white;margin-top:4px;">☁ ${HOSTNAME_LABEL}</div>
<div style="font-size:13px;color:#94a3b8;margin-top:4px;">${TIMESTAMP} | cloud.kingdezigns.com</div>
</td>
<td align="right">
<div style="background:${STATUS_COLOR};color:white;padding:10px 20px;border-radius:8px;font-size:14px;font-weight:700;">${STATUS_LABEL}</div>
</td>
</tr></table>
</td></tr>
<!-- STATUS BANNER -->
<tr><td style="background:${STATUS_COLOR};padding:12px 32px;">
<span style="color:white;font-size:14px;font-weight:600;">${STATUS_MSG}</span>
</td></tr>
<!-- BODY -->
<tr><td style="background:white;padding:28px 32px;">
${BODY_HTML}
</td></tr>
<!-- FOOTER -->
<tr><td style="background:#0f172a;border-radius:0 0 10px 10px;padding:16px 32px;text-align:center;">
<span style="font-size:11px;color:#64748b;">Nextcloud Update Check &nbsp;|&nbsp; ${HOSTNAME_LABEL} &nbsp;|&nbsp; KingDezigns Infrastructure</span>
</td></tr>
</table>
</td></tr>
</table>
</body>
</html>
HTML
)
log "Sending email to ${TO_EMAIL}..."
SEND_RESULT=$(curl --silent --show-error \
--url "smtps://${SMTP_SERVER}:${SMTP_PORT}" \
--ssl-reqd \
--mail-from "${FROM_EMAIL}" \
--mail-rcpt "${TO_EMAIL}" \
--user "${SMTP_USER}:${SMTP_PASS}" \
--upload-file - <<EOF 2>&1
From: ${FROM_NAME} <${FROM_EMAIL}>
To: ${TO_EMAIL}
Subject: ${SUBJECT}
MIME-Version: 1.0
Content-Type: text/html; charset=UTF-8
${HTML_EMAIL}
EOF
)
if [[ $? -eq 0 ]]; then
log "Email sent successfully."
else
log "ERROR sending email: ${SEND_RESULT}"
fi
find "$LOG_DIR" -name "update-check-*.log" -mtime +${LOG_RETENTION_DAYS} -delete
log "Done."