Automatically update Matomo via Cronjob?

Hey there!

Whenever a new Matomo version is released, I’ll get an email and need to click a few buttons in the interface to “auto-update” Matomo.

Is there a way to do this fully automatic using a cron or similar?

My idea was to run the following code in a cronjob, but would that be everything necessary?

php console core:update --yes

Thanks for help.
Aaron

1 Like

Isn’t console core:update only for db schema upgrades?

Yes, it is. you also need to download/copy the new files first. also, you might want to backup some stuff before updating. and it is most likely not a good idea to just update everything instantly without checking if there is a compatibility problem.

however, i asked ChatGPT for you:
Careful, this is untested GPT content that might do you more harm than good!

To upgrade Matomo automatically using a cron job, you need to write a script that performs the upgrade steps and schedule it to run periodically. Below is a step-by-step guide to achieve this.

Prerequisites

  1. Backup your data: Always ensure you have backups of your Matomo database and installation files before running any upgrade process.
  2. Ensure permissions: The user running the cron job should have the appropriate permissions to perform file operations on the Matomo installation directory.

Steps to Upgrade Matomo Automatically

  1. Create an Upgrade Script

    Create a shell script that automates the upgrade process. For example, create a file named matomo_upgrade.sh with the following content:

    #!/bin/bash
    
    # Set variables
    MATOMO_DIR="/path/to/matomo"  # Change to your Matomo installation path
    BACKUP_DIR="/path/to/backup"  # Directory where you want to keep backups
    MATOMO_URL="https://builds.matomo.org/matomo-latest.zip"
    
    # Navigate to Matomo directory
    cd $MATOMO_DIR || exit
    
    # Backup current Matomo directory
    tar -czf $BACKUP_DIR/matomo-backup-$(date +%F).tar.gz .
    
    # Download latest Matomo version
    wget $MATOMO_URL -O matomo-latest.zip
    
    # Extract downloaded zip and overwrite the existing files
    unzip -o matomo-latest.zip
    
    # Cleanup
    rm matomo-latest.zip
    
    # Set proper permissions (Optional: Set these as per your server configuration)
    chown -R www-data:www-data $MATOMO_DIR
    chmod -R 755 $MATOMO_DIR
    
    # Run Matomo database update (if necessary)
    php $MATOMO_DIR/console core:update --yes
    
    echo "Matomo upgrade completed successfully on $(date)"
    
    • MATOMO_DIR: The path to your Matomo installation.
    • BACKUP_DIR: The directory where backups will be stored.
    • MATOMO_URL: The URL to download the latest Matomo version.
  2. Make the Script Executable

    Give execute permissions to the script:

    chmod +x /path/to/matomo_upgrade.sh
    
  3. Set Up the Cron Job

    Open your crontab file:

    crontab -e
    

    Add the following line to schedule the script to run periodically, e.g., every Sunday at 2 AM:

    0 2 * * 0 /path/to/matomo_upgrade.sh >> /path/to/matomo_upgrade.log 2>&1
    

    This line specifies that the script should run:

    • 0 2: At 2:00 AM.
    • * * 0: Every Sunday.
    • >> /path/to/matomo_upgrade.log 2>&1: Redirects both standard output and error to the log file.
  4. Test the Script

    Run the script manually to ensure it works as expected:

    /path/to/matomo_upgrade.sh
    

    Check the log file (/path/to/matomo_upgrade.log) for any errors and verify that the Matomo installation was upgraded correctly.

Additional Considerations

  • Check Matomo Requirements: Ensure that your server meets the requirements of the new Matomo version before upgrading.
  • Monitor the Cron Job: Regularly check the log file to monitor the outcome of the cron job.
  • Handle Errors: You can enhance the script to send notifications (e.g., via email) if an error occurs during the upgrade process.

This setup will automatically upgrade your Matomo installation at the scheduled interval, keeping it up to date with the latest releases.