Updated on | #modx, #backup, #bash | 0 User comments

MODX update and backup by using Git, MySQL and Cronjob

MODX is a free, open source content management system and web application framework for publishing content on the web where it is essential that you always back up your content and have a plan for recovering from a system failure. Hence, you should back up your personal or critical work data on a regular basis. In this tutorial I will show you how you can backup MODX very easily as well as how you can upgrade MODX by using the source code on GitHub.

Backup MODX

Backing up is one of the most important things to do when running MODX. There are so many things that can go wrong, whether you upgraded MODX, or you installed a nonsense Extra, that you can't delete very easily. There are different ways to backup your MODX system. Of course you can use several MODX plugins for automated or manually backups using database and file server programs. Personally, I like it more to do such important jobs directly in the linux shell by using robust "onboard" tools.

Therefore, I created a simple shell script that has the following features:

  • It creates a full backup archive of the MODX website's directory, including all subdirectories
  • It back up the MODX MySQL Database, where you don't have to remember your username, password or table name
  • It compress all backup files in order to save space on your drive
  • As you decided, it will back up MODX hourly, daily, weekly or monthly
  • It will delete old backups after X days
  • It repairs your MODX MySQL Database

MODX Backup Script

This script will backup your MODX MySQL database table as one sql dump file as well as your MODX directory. Please have a look into this script and change the variables BACKUP_DIR, SOURCE_DIR and DEL_DAYS.

  1. #!/bin/bash
  2. #title           : modx-backup.sh
  3. #description     : Backup your ModX installation by using this bash script
  4. #author          : Thomas Ludwig
  5. #website         : https://ludwig.im
  6. #date            : 20142810
  7. #version         : 1
  8. #notes           : You can use this script together with  CronJob
  9. #==========================================================================================================
  10.  
  11. # Backup Folder
  12. BACKUP_DIR="/var/backups"
  13.  
  14. # ModX Source Folder
  15. SOURCE_DIR="/var/www/modx"
  16.  
  17. # Delete Backups after x days
  18. DEL_DAYS=30
  19.  
  20. # Backup Routine
  21. #==========================================================================================================
  22. DATE=$(date +%Y-%m-%d-%H%M%S)
  23.  
  24. # Backup Files
  25. mkdir -p "$BACKUP_DIR"
  26. tar -czf "$BACKUP_DIR/modx_$DATE.tar.gz" "$SOURCE_DIR" --exclude="$SOURCE_DIR/core/cache/*" &> /dev/null
  27.  
  28. # Backup MySQL Database Options
  29. MODXDBNAME=`cat $SOURCE_DIR/core/config/config.inc.php | grep 'dbase' | cut -d \' -f 2`
  30. MODXUSER=`cat $SOURCE_DIR/core/config/config.inc.php | grep 'database_user' | cut -d \' -f 2`
  31. MODXPASS=`cat $SOURCE_DIR/core/config/config.inc.php | grep 'database_password' | cut -d \' -f 2`
  32.  
  33. # Execute MySQL Backup
  34. mysqlcheck --repair --force -u "$MODXUSER" -p"$MODXPASS" --databases "$MODXDBNAME" &> /dev/null
  35. mysqldump --opt -u "$MODXUSER" -p"$MODXPASS" "$MODXDBNAME" | gzip > "$BACKUP_DIR/modx_$DATE.sql.gz"
  36.  
  37. # Delete files that are older than X days
  38. find $BACKUP_DIR/ -name 'modx_*' -type f -mtime +$DEL_DAYS -exec rm -rf {} \;

Cronjob

Cron is a utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon. Furthermore, I'm using Anacron, which provides other approaches to scheduled tasks. For example, anacron offers simple system-wide directories for running commands hourly, daily, weekly, and monthly. The scripts in /etc/cron.hourly/, /etc/cron.daily/, /etc/cron.weekly/ and /etc/cron.monthly are running as root. For me it's enough to backup MODX weekly. Therefore, I created a cron script in /etc/cron.weekly by using

  1. sudo nano /etc/cron.weekly/modx-backup.sh

where you have to copy the script inside. After this, please set the execution rights with

  1. sudo chmod +x /etc/cron.weekly/modx-backup.sh

and restart cron daemon with:

  1. sudo service cron restart

Upgrade MODX using Git

Git is the distributed version control system designed to handle everything from small to very large projects. MODX is using it for source code collaboration and version control on GitHub, which is a social network for coders. Please use this tutorial only if you already installed the Git version of MODX.

Based on the backup article above, please run the script at first

  1. sudo /etc/cron.weekly/modx-backup.sh

and have a look in your backup directory about the files:

  1. ls -l /var/backups/modx_*
-rw-r--r-- 1 root root   2136933 Okt 26 15:16 /var/backups/modx_2014-10-26-151554.sql.gz
-rw-r--r-- 1 root root 351963315 Okt 26 15:16 /var/backups/modx_2014-10-26-151554.tar.gz

If the backup was made successfully, you can go on to upgrade MODX from Git.

Go to your MODX directory cd /var/www/modx and have look what is the latest git tag:

  1. sudo git fetch --all
  2. git tag
.
.
.
v2.2.5-pl
v2.2.6-pl
v2.2.7-pl
v2.2.8-pl
v2.2.9-pl
v2.3.0-pl
v2.3.1-pl
v2.3.2-pl

In this case, the latest git tag is called v2.3.2-pl, which we want to use. You can also get the latest tag by using:

  1. git tag | sort -n | tail -1

After this step, we need to use "git checkout tags/"tag_name"" to get this version. You need to force the checkout by using "-f":

  1. sudo git checkout tags/v2.3.2-pl -f

Now you are able to build the core of MODX with

  1. cd /var/www/modx/_build
  2. sudo mv build.config.sample.php build.config.php
  3. sudo mv build.properties.sample.php build.properties.php
  4. sudo php transport.core.php

and see the results

  1. ls -l ../core/packages/core.transport.zip
  2. ls -l ../core/packages/core

as well as remove the build script by using:

  1. sudo chown -R www-data:www-data /var/www/modx/
  2. sudo rm -R /var/www/modx/_build

After you've done so, run the setup again by visiting the following page:

https://YOURDOMAIN.TLD/setup/

When you finished successfully the setup you, are able to remove the setup files and change the MODX folder from user root to www-data:

  1. sudo rm -R /var/www/modx/setup
  2. sudo chown -R www-data:www-data /var/www/modx/

The last step should be to make a backup again:

  1. sudo /etc/cron.weekly/modx-backup.sh

Thats it!

Well, what do you think?

Comments powered by LudwigDisqus for ModX