Moodle – Make backup each day on Ubuntu server

Simple Bash script to make every day backup of the code, database and moodledata files.

First of all, if you need a solution that “just works”, you can clone or copy the file from this link: https://github.com/devlionco/bashscripts/blob/master/backup-moodle-full.sh

To use it, just place it in your home directory (or wherever you want on the server) and make it executable with the command:

chmod +x backup-moodle-full.sh

Then launch the script with the only mandatory argument – the directory path, where Moodle’s config.php is located:

./backup-moodle-full.sh /var/www/moodle

The script will create backup_moodle directory near the /var/www/moodle folder and will place there archive with name, that looks like: 20181209.tar.gz and log file with name, that looks like backup_20181209.log. As you see, the script uses the current date as unique id for archive and log file names.

The archive is a file in tar format, that is additionally gzipped. 

The archive consists of the full path to moodle installation and keep root code files (Moodle itself), database dump (that is also located in root Moodle directory) and moodledata directory with all users’ files.

The log file keeps warnings and messages about the backup process. So if you occur any issues – the best decision is to view the log file.

Below I will try to explain, some the most important things, that script does.

  • So, first script tries to find or create the backup directory. It will be created as sibling to the root Moodle directory. 
  • Then, script looks for DB config options and moodledata directory location. 
  • The next step is DB dumping. Pay attention, that for now, only MySQL dumping is supported in our script! PostgreSQL dumping will be added in the future script release. 
  • After this, all the collected data is archived.
  • Finally, the cleaning process is launched: DB backup is removed from the root Moodle directory and all backups, that are older than 7 days, are removed from backup directory to save the disk space on the server (the age of files, that will be removed is specified in constant AUTODELETEAFTER in the beginning of the script).

The last recommendation is to setup cron to start the regular backup process. You can use this example:

# 4 10 * * * /bin/bash /home/yourusername/backup-wp-advanced.sh /var/www/wp

As an additional option, you can add copying backup data to your own FTP or cloud service (to protect data if the server will destroy). We will add this option in the future script versions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.