#!/usr/bin/env bash # Author : Allan Christensen # First Created : 14-02-2026 (DD-MM-YYYY) # Description : Updates DokuWiki installations safely (does not modify the data directory) # License : MIT License # # Are we root # if [[ $(id -u) -ne 0 ]]; then echo "" && echo "Must be root or use sudo" && echo "" ; exit ; fi # # Validate input # if [[ $# -ne 1 ]]; then echo "" echo "Usage:" echo " dokuwikiupdate /path/to/wiki" echo "" exit 1 fi wikidir="$1" if [[ ! -d "$wikidir" ]]; then echo "" echo "Directory not found: $wikidir" echo "" exit 1 fi if [[ ! -f "$wikidir/VERSION" ]]; then echo "" echo "VERSION file not found in $wikidir — not a valid DokuWiki installation." echo "" exit 1 fi # # Variables # logdir="/var/log/dokuwiki" tmpdir=$(mktemp -d /tmp/dokuwiki-update.XXXXXX) wikiid=$(echo "$wikidir" | sed 's#^/##; s#/#_#g') logfile="$logdir/${wikiid}.log" backupfile="$logdir/${wikiid}-core-backup.tar.gz" tarball="$tmpdir/dokuwiki-stable.tgz" extractdir="$tmpdir/extracted" # # Prepare logging # mkdir -p "$logdir" touch "$logfile" chmod 640 "$logfile" # # Detect interactive mode # if [[ -t 1 ]]; then interactive=true else interactive=false fi # # Redirect output to logfile # exec >>"$logfile" 2>&1 echo "============================================================" echo "DokuWiki update run : $(date)" echo "Wiki path : $wikidir" echo "============================================================" # # Logging helper # log() { echo "$1" if [[ "$interactive" == true ]]; then echo "$1" > /dev/tty fi } # # Cleanup tmpdir automatically # trap 'rm -rf "$tmpdir" 2>/dev/null' EXIT # # Read installed version # installed_version=$(tr -d '\r\n' < "$wikidir/VERSION") if [[ -z "$installed_version" ]]; then log "" log "Could not determine installed version — aborting." log "" exit 1 fi # # Download latest DokuWiki # log "" log "Downloading latest DokuWiki..." curl -fL https://download.dokuwiki.org/src/dokuwiki/dokuwiki-stable.tgz -o "$tarball" \ || { log "Download failed — aborting."; exit 1; } # # Extract archive # log "Extracting archive..." mkdir -p "$extractdir" tar -xzf "$tarball" -C "$extractdir" --strip-components=1 \ || { log "Extraction failed — aborting."; exit 1; } # # Read latest version # if [[ ! -f "$extractdir/VERSION" ]]; then log "VERSION file missing in downloaded archive — aborting." exit 1 fi latest_version=$(tr -d '\r\n' < "$extractdir/VERSION") log "" log "Installed version : $installed_version" log "Latest version : $latest_version" # # Compare versions # if [[ "$installed_version" == "$latest_version" ]]; then log "" log "Already up to date. Nothing to do." log "" exit 0 fi log "" log "Updating DokuWiki to version: $latest_version" # # Clear cache before backup # log "" log "Clearing DokuWiki cache..." rm -rf "$wikidir/data/cache/"* 2>/dev/null rm -rf "$wikidir/data/tmp/"* 2>/dev/null # # Estimate backup size # log "" log "Checking available disk space..." estimated_size=$(du -sb \ --exclude=data \ --exclude=lib/plugins \ "$wikidir" | awk '{print $1}') available_space=$(df --output=avail -B1 "$logdir" | tail -1) log "Estimated backup size : $estimated_size bytes" log "Available space : $available_space bytes" if (( available_space < estimated_size )); then log "" log "Not enough free space in $logdir to create backup." log "Aborting update." log "" exit 1 fi # # Backup current installation (core only) # log "" log "Creating backup..." rm -f "$backupfile" tar \ --exclude='data' \ --exclude='lib/plugins' \ -czf "$backupfile" \ -C "$wikidir" . \ || { log "Backup failed — aborting."; exit 1; } log "Backup saved to: $backupfile" # # Inform user what will happen # log "" log "Core files will be overwritten with the latest DokuWiki release." log "User data and plugins will NOT be modified." # # Prepare extracted files # log "" log "Preparing files (setting ownership to www-data)..." chown -R www-data:www-data "$extractdir" # # Copy files over existing installation # log "Updating core files..." cp -a "$extractdir/." "$wikidir/" \ || { log "Copy failed — aborting."; exit 1; } # # Completed # log "" log "DokuWiki successfully updated to: $latest_version" log "" # # All done # printf "\nAll Done...\n" # # End of script #