Backing up your Moodle site is really important, and helps to ensure the long-term integrity of your data. Without a backup, your students could lose all of their hard work and progress if something were to go wrong. This is why it's crucial to make regular backups of your Moodle LMS, and store them securely.
Moodle backups consist of three parts:
- The database
- The Moodle platform files
- The moodledata files
Before starting your backup, you should put your site into maintenance mode, to prevent unnecessary writing to the database during the backup. If the database receives writes during a backup, it may alter its integrity, causing it to become corrupt.
- Login to your Moodle site as an administrator
- Navigate to Administration > Site administration > Server > Maintenance mode
- Enable maintenance mode for the Moodle site
Avoid making any changes to the database (i.e. don't alter site settings or configuration) whilst Moodle is in maintenance mode. This is the best way to ensure integrity of the database backup.
Now we're ready to begin creating the Moodle backup via the command line.
Via the command line, run the following command to create a dump of the MySQL database used by Moodle:
mysqldump -u [mysql_user] [moodle_database_name] > /path/to/name_of_dump.sql
Remember to replace the bold text above with credentials that match your server and MySQL database user. For example, if your Moodle database is called "moodle", and you want to export it as the root user, you would use the following command to export the database to the /home/root directory on the server:
mysqldump -u root moodle > /home/root/moodle_backup.sql
Specify an existing path on the server, that is not accessible over the web, as the destination for the MySQL dump.
Now, you must also create a backup of the Moodle platform files, as well as the moodledata folder.
There are various methods to do this, such as copying/cloning the files to another private location on the server, but the best solution is to save them to a local drive, in case the server fails or becomes corrupted.
To copy the files from your Moodle root to a local drive, you can use the scp command.
scp [username]@[server]:/path/to/moodle /path/on/local/drive
Switch out the username and server, as well as the path to the Moodle root on your server, and the local path where you wish to save the copied files.
For example, if your server user is "root" and the server IP you're connecting to is "127.0.0.0", you might use a command like this:
scp firstname.lastname@example.org:/var/www/html /Users/John/Documents/backups
This command assumes that your Moodle root is located at /var/www/html on the remote server, and you wish to save the files to /Users/John/Documents/backups on your local harddrive.
After the process has completed, you must run the same command for the moodledata directory, like so:
scp [username]@[server]:/path/to/moodledata /path/on/local/drive
And finally, use the same command to transfer the MySQL database dump, like so:
scp [username]@[server]:/path/to/database.sql /path/on/local/drive
And that's it!
Don't forget to remove the MySQL database dump from the server, using a command like so: