Skip to content

[Wiki.js] Auto Upgrade Script

General
  • Hi 🙂

    Sorry if I’m not in the right category.

    It’s nothing compared to @phenomlab and for all that he gives us but here’s a small contribution.

    I use the excellent software Wiki.js for my Wiki and I wrote this script and I share it with you


    Informations


    – The script allows you to automatically upgrade Wiki.js.

    – It pre-checks the installed version and compares it with the latest available on GitHub.

    – A backup of Wiki.js is made before the version upgrade in a defined directory.

    – The script has a –rollback option which allows you to return to the version installed before the execution of the script thanks to the backup if a problem occurs. (This does not prevent having another backup on hand 🙂 )


    Use


    – Copy and adapt the code below and paste it into a file named upgrade-wikijs.sh :

    nano upgrade-wikijs.sh
    

    – Make the script executable :

    chmod +x upgrade-wikijs.sh
    

    – Use it :

    ./upgrade-wikijs.sh
    
    ./upgrade-wikijs.sh --rollback
    

    CODE


    – I consider postgreSQL database engine is installed for Wiki.js
    This is the best possible choice for the future.

    – Adapt the code below :

    #!/bin/bash
    #####################################################
    #### Auteur: DownPW
    #### Date: 06.12.2021
    #####################################################
    ################ Upgrade wiki.js ####################
    #####################################################
    ###
    
    ### Definition of variables
    WIKIDIR=/pathtoyourwiki
    PACKAGE_VERSION_OLD=$(sed -nE 's/^\s*"version": "(.*?)",$/\1/p' $WIKIDIR/package.json)
    CURRENT_WIKI=$(curl -s https://api.github.com/repos/Requarks/wiki/releases/latest | grep tag_name | head -n 1 | cut -d '"' -f 4)
    CURRENT_WIKI_DOWNLOAD="https://github.com/Requarks/wiki/releases/download/$CURRENT_WIKI/wiki-js.tar.gz"
    
    ### Script option: Rollback to old version
    if [[ $1 == "--rollback" ]]
    then
     if [[ -d /home/XXXX/wiki.bak ]]
     then
      PACKAGE_VERSION_BACKUP=$(sed -nE 's/^\s*"version": "(.*?)",$/\1/p' /home/XXXX/wiki.bak/wikipw/package.json)
      echo "Stop wiki.js ..."
      systemctl stop wiki
      echo "Stop wiki.js OK..."
      
      echo "Stop postgresql ..."
      systemctl stop postgresql
      echo "Stop postgresql OK..."
      
      echo "Rollback to $PACKAGE_VERSION_BACKUP ..."
      rm -rf $WIKIDIR/* && cp -r /home/XXXX/wiki.bak/wikipw/* $WIKIDIR
      echo "Rollback to $PACKAGE_VERSION_BACKUP OK"
      
      echo "Reload and restart necessary services ..."
      systemctl daemon-reload
      systemctl enable wiki
      systemctl reload nginx.service
      nginx -t
      systemctl restart nginx.service
      systemctl start postgresql
      echo "Reload and restart necessary services OK"
      
      echo "Starting Wiki.js. It may take a few seconds ..."
      systemctl start wiki
      echo "Starting Wiki.js OK"
      
      exit 0
     else
      echo "No backups found :("
      exit 1
     fi
    fi
    
    ### Backup of the old version and Upgrade to the new one
    if [[ $CURRENT_WIKI != $PACKAGE_VERSION_OLD ]]
    then
     read -r -p "Do you want to update Wiki.js $PACKAGE_VERSION_OLD to the version $CURRENT_WIKI? [Y/n] " response
     if [[ $response =~ ^([yY]|"")$ ]]
     then
      echo "Stop wiki.js ..."
      systemctl stop wiki
      echo "Stop wiki.js OK"
      
      echo "Stop postgresql ..."
      systemctl stop postgresql
      echo "Stop postgresql OK"
      
      echo "Wiki.js update ..."
      
      echo "version download $CURRENT_WIKI ..."
      (cd /home/XXXX/ && curl --progress-bar -LO $CURRENT_WIKI_DOWNLOAD)
      echo "version download $CURRENT_WIKI OK"
      
      echo "Removed old residue from this script ..."
      rm -rf /home/XXXX/wiki.bak
      echo "Removed old residue from this script OK"
      
      echo "Creating the working directory ..."
      mkdir /home/XXXX/wiki.bak/
      echo "Creating the working directory OK"
      
      echo "Configuration backup $PACKAGE_VERSION_OLD ..."
      cp $WIKIDIR/config.yml /home/XXXX/config.yml.bak
      cp -r $WIKIDIR /home/XXXX/wiki.bak
      echo "Configuration backup $PACKAGE_VERSION_OLD OK"
      
      echo "Version deletion $PACKAGE_VERSION_OLD ..."
      rm -rf $WIKIDIR/*
      echo "Version deletion $PACKAGE_VERSION_OLD OK"
      
      echo "Extraction and installation of the version $CURRENT_WIKI ..."
      tar xzf /home/XXXX/wiki-js.tar.gz -C $WIKIDIR && rm /home/XXXX/wiki-js.tar.gz
      cp /home/XXXX/config.yml.bak $WIKIDIR/config.yml && rm /home/XXXX/config.yml.bak
      echo "Extraction and installation of the version $CURRENT_WIKI OK"
    
      echo "Reload and restart necessary services :"
      systemctl daemon-reload
      systemctl enable wiki
      systemctl reload nginx.service
      nginx -t
      systemctl restart nginx.service
      systemctl start postgresql
      echo "Reload and restart necessary services OK"
      
      echo "################"
      PACKAGE_VERSION=$(sed -nE 's/^\s*"version": "(.*?)",$/\1/p' $WIKIDIR/package.json)
      echo "Wiki.js $PACKAGE_VERSION_OLD has been updated to version $PACKAGE_VERSION"
      echo "################"
      
      echo "Start Wiki.js. It may take a few seconds ..."
      systemctl start wiki
      echo "Start Wiki.js OK"
      
      echo "----------------"
      echo "Status of necessary services ..."
      systemctl status nginx
      echo "----------------"
      systemctl status postgresql
      echo "----------------"
      systemctl status wiki
      echo "----------------"
      echo "If something is wrong, please check the logs: 'journalctl -u wiki.service --since today'"
      echo "To undo the update to the latest version, use the option '--rollback'"
      echo "----------------"
     fi
    else
    ### Wiki.js UP TO DATE
     echo "################"
     echo "Wiki.js $PACKAGE_VERSION_OLD is already up to date. No update needed"
     echo "################"
    fi
    

    Sorry if the traduction is not excellent 🙂