Backing up Moodle Datafiles to a remote server

 As part of any disaster recovery plan, backup of the MoodleData folder is also critical. The critical variable in this equation is Disk Space – there never seems to be enough!

Making regular backups of the entire MoodleData folder is a bit impractical – at least in my scenario, where it is over 100 GB. Instead, I chose to mirror the moodledata folder on a different server, using rsync. This is purely for a disaster recovery scenario and will not consider data recovery on a per-user basis. In an environment of 22000+ users, this is highly impractical.

Assumptions: Another server has already been configured with CentOS, all necessary firewall/SELinux/networking settings have been completed.
In this example, I will use:
192.168.100.1 as the Moodle Server
192.168.100.2 as the Backup Server

0. Login as root on Moodle Server

A “trust” needs to be setup between the Moodle Server and the Backup Server, so that the automated backup does not fail due to password prompts! This is also an added measure of security as well, so that root / sudo passwords are not stored in plain-text in the shell scripts.


1. Setup a trust for password free rsync over ssh
ssh-keygen -t dsa
cat ~/.ssh/id_dsa.pub | ssh root@192.168.100.2 ‘cat >> .ssh/authorized_keys’
Prompted for the password ONCE. Enter it correctly and that’s it!


2. Test the password free connection (this will only be one way). From Moodle Server:
ssh -l root 192.168.100.2
The backup server should be accessible without entering a password!


3. On the Moodle Backup, create a folder on the largest available disk.
mkdir /path/to/mirror

4. On the Moodle Server, create the script that will mirror the moodledata folder.
cd /temp/scripts
touch datafiles_backup.run
chmod +x datafiles_backup.run

5. Edit the script –  log each time the folder is mirrored. Ensure that the folder exists [/temp/logs]
nano datafiles_backup.run
Add to file:

rsync –delete-after -e ssh -avz /path/to/your/moodledata/folder/ root@192.168.100.2:/path/to/mirror
date >> /temp/logs/moodledatamirror.log Save and exit



6. Test script
cd /temp/scripts
./datafiles_backup.run
Some output should be visible and the script will notify when complete.

7. Check to see if the files went across properly!
Login via ssh to Moodle Backup server
ssh -l root 192.168.100.2
cd /path/to/mirror
ls -all

Compare the results of the list to the original folder on the Moodle Server!

8.Add the script to the crontab to execute nightly
Schedule to run this script at periods of low activity – 2:00am should be fine for most installs. Remember to factor in the Database backup time!

nano /etc/crontab
Append the following:

#database backup script

00 2 * * *  root /bin.sh /temp/scripts/datafiles_backup.run

The MoodleData files will now be mirrored nightly!
This same strategy can be used to transfer the database backups to the backup server.

-n