Automated Moodle update script with comprehensive backup, security checks, and maintenance mode handling.
- Automated Updates: Download and install any Moodle version from GitHub releases
- Stable/Dev Resolution: Stable versions use
vX.Y.Ztags; unreleased/dev targets require--allow-unstable - Smart Backup System: Automatic backup of code, database, and critical data
- PHP Compatibility: Automatic PHP version checking and upgrade support
- Non-interactive Runs:
--yessupports unattended runs after pre-flight validation - Public Webroot Handling: Updates Apache/Nginx roots to Moodle
public/when needed - Version Validation: Prevents downgrades and validates target versions
- Maintenance Mode: Automatic maintenance mode during updates
- Database Support: MySQL, MariaDB, and PostgreSQL backup support
- Security Focused: Proper permissions and security hardening
- Progress Tracking: Real-time progress with colored output
- Web Server Integration: Automatic web server restart after updates
- Operating System: Ubuntu/Debian (preferred), CentOS/RHEL compatible
- Privileges: Root access (sudo) required
- PHP: Version compatible with the target Moodle release. Moodle 5.2+ requires PHP 8.3+
- Database: MySQL/MariaDB or PostgreSQL
- Web Server: Apache2 or Nginx
- Tools: curl, tar, sed, grep, systemctl
# Download directly from GitHub
curl -o updater.sh https://raw.githubusercontent.com/EverHype-Systems/moodle-update/main/updater.sh
chmod +x updater.sh# Clone the repository
git clone https://github.com/EverHype-Systems/moodle-update.git
cd moodle-update
chmod +x updater.sh# Check script integrity
sha256sum updater.sh
# Compare with hash from GitHub releases# Interactive mode - will prompt for version
sudo ./updater.sh /var/www/html/moodle /var/www/moodledata
# Specify target version
sudo ./updater.sh /var/www/html/moodle /var/www/moodledata 5.2.0
# Update to latest available version
sudo ./updater.sh /var/www/html/moodle /var/www/moodledata latest
# Non-interactive run, only when all requirements already pass
sudo ./updater.sh --yes /var/www/html/moodle /var/www/moodledata latest
# Permit unattended PHP/database package changes as well
sudo ./updater.sh --yes --allow-system-changes /var/www/html/moodle /var/www/moodledata 5.2.0
# Install an unreleased/dev target such as 5.3dev from main (staging only)
sudo ./updater.sh --allow-unstable /var/www/html/moodle /var/www/moodledata 5.3-y, --yes Run non-interactively and answer yes to updater prompts
--allow-unstable Allow beta, RC, or dev targets such as 5.3dev
--allow-system-changes
Permit unattended PHP/database package upgrades with --yes
--skip-webroot-update Do not update Apache/Nginx DocumentRoot to /public
--no-restart Do not restart Apache/Nginx at the endStandard Moodle Installation:
sudo ./updater.sh /var/www/html/moodle /var/www/moodledataCustom Paths:
sudo ./updater.sh /opt/moodle /opt/moodledata 4.4.2Development/Testing Environment:
sudo ./updater.sh --allow-unstable /home/user/moodle /home/user/moodledata 5.3The script expects the following structure:
/var/www/html/moodle/ # Moodle code directory
├── admin/ # Moodle admin directory
├── config.php # Moodle configuration
├── version.php # Version information
└── ... # Other Moodle files
/var/www/moodledata/ # Moodle data directory
├── cache/ # Cache files
├── sessions/ # Session data
├── temp/ # Temporary files
└── ... # Other data files
- Automatic Backups: Created before any changes
- Multiple Backup Types: Code, database, and configuration
- Timestamp Naming: Easy identification of backup versions
- Safe Location: Stored in
/tmp/moodle_backup_YYYYMMDD_HHMMSS/
- Secure Defaults: Proper file and directory permissions
- Web Server User: Automatic detection of web server user (www-data, apache, nginx)
- Config Protection: Secured config.php with restricted access
- Maintenance Mode: Automatic activation during updates
- Downgrade Prevention: Blocks dangerous downgrades
- PHP Compatibility: Validates PHP version requirements
- Version Comparison: Intelligent semantic version comparison
Permission Denied:
# Ensure script has execute permissions
chmod +x updater.sh
# Run with sudo
sudo ./updater.sh [paths]PHP Version Too Old:
# Script will prompt to auto-upgrade PHP
# Or manually upgrade PHP first:
sudo apt-get update
sudo apt-get install php8.3Download Failures:
# Check internet connection
curl -I https://github.com
# Verify GitHub access
curl -s https://api.github.com/repos/moodle/moodle/tags | headMoodle 5.3 Is Not Listed As Stable:
# Moodle 5.3 is currently resolved from the main branch as 5.3dev.
# Use this only in staging/testing:
sudo ./updater.sh --allow-unstable /var/www/html/moodle /var/www/moodledata 5.3Moodle 5.1+ Shows The Wrong Web Directory:
# Moodle now ships a public/ webroot. The updater attempts to update Apache/Nginx.
# If skipped or not detected, set your site root manually to:
/var/www/html/moodle/publicDatabase Backup Issues:
# Ensure database tools are installed
sudo apt-get install mysql-client # For MySQL/MariaDB
sudo apt-get install postgresql-client # For PostgreSQL
# Verify database credentials in config.phpThe script automatically enables debug mode during updates. To manually enable:
// In config.php
$CFG->debug = 32767;
$CFG->debugdisplay = 1;Check these locations for detailed error information:
- Web server logs:
/var/log/apache2/error.logor/var/log/nginx/error.log - PHP logs:
/var/log/php/error.log - Moodle logs: Via Admin → Reports → Logs in Moodle interface
-
Pre-flight Checks
- Validate paths and permissions
- Check current Moodle version
- Verify PHP compatibility
- Fetch available versions
-
Backup Phase
- Create timestamped backup directory
- Backup Moodle code
- Backup database
- Backup configuration files
-
Maintenance Mode
- Enable maintenance mode
- Display maintenance message to users
-
Update Phase
- Download target Moodle version
- Extract and install new files
- Preserve existing configuration
- Set proper permissions
-
Upgrade Process
- Run Moodle CLI upgrade script
- Update database schema
- Clear caches
-
Finalization
- Disable maintenance mode
- Restart web server
- Verify installation
- Display success status
If an update fails, recovery steps:
-
Automatic Recovery:
# Restore from automatic backup BACKUP_DIR="/tmp/moodle_backup_YYYYMMDD_HHMMSS" sudo cp -r $BACKUP_DIR/moodle_code/* /var/www/html/moodle/ sudo cp $BACKUP_DIR/config.php.backup /var/www/html/moodle/config.php
-
Database Recovery:
# Restore database from backup mysql -u username -p database_name < $BACKUP_DIR/database_backup.sql
-
Manual Maintenance Mode Disable:
# If maintenance mode is stuck sudo sed -i '/maintenance_enabled.*true/d' /var/www/html/moodle/config.php
# If PHP is in non-standard location
export PATH="/opt/php8.3/bin:$PATH"
./updater.sh [paths]# The script auto-detects, but you can modify the script:
# Edit line: WEB_USER="${3:-www-data}"
# Change www-data to your web server user# Ensure outbound HTTPS is allowed
sudo ufw allow out 443
sudo ufw allow out 80- Check Requirements: Ensure all prerequisites are met
- Review Logs: Check web server and PHP error logs
- Test Manually: Verify Moodle works after update
- Backup Status: Confirm backups were created successfully
- GitHub Issues: Report bugs or request features
- GitHub Discussions: Community support and questions
- Moodle Community: moodle.org/support
- Documentation: docs.moodle.org
We welcome contributions! Please see our Contributing Guidelines.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
When reporting issues, include:
- Operating system and version
- Current Moodle version
- Target Moodle version
- PHP version
- Error messages
- Relevant log excerpts
Important Security Considerations:
- Always test updates in a staging environment first
- Ensure you have complete system backups before major updates
- Review Moodle security announcements before updating
- Keep PHP and web server software updated
- Monitor file permissions after updates
- Verify database integrity after updates
This script is provided as-is for educational and operational purposes. Use at your own risk and always maintain proper backups.
- Fixed Moodle 5.1+
public/webroot handling - Added explicit stable/dev target resolution for versions such as 5.3dev
- Fixed database backup config parsing by defining
CLI_SCRIPT - Fixed polluted backup path output during command substitution
- Updated PHP requirements for Moodle 5.2+
- Added
--yes,--allow-unstable,--allow-system-changes, and restart/webroot controls
- Initial release
- Automated Moodle updates with backup
- PHP compatibility checking
- Database backup support
- Maintenance mode handling
- Multi-platform support
- Comprehensive error handling
Happy Updating!
For the latest version and updates, visit: https://github.com/EverHype-Systems/moodle-update