This script is used to backup courses from moodle using command line when given the course IDs
This script automates the backup of Moodle courses by their course short names. It queries the Moodle database to retrieve the corresponding course IDs and then uses Moodle’s CLI backup tool to create a backup while excluding student/user data.
Course Identification by Short Name:
The script accepts course short names and queries the Moodle database to get the corresponding course ID.
Exclusion of User Data:
The backup is performed with user data excluded. This is achieved by passing the --users=0 parameter to the backup command (verify that your Moodle version supports this flag).
Logging:
All actions and errors are logged to a daily log file in the backup destination directory.
Flexible Configuration:
The Moodle root directory, database credentials, backup destination, and course short names are configurable within the script or via an external file.
Moodle Environment:
Ensure you have a working Moodle installation with CLI backup functionality available at admin/cli/backup.php.
MySQL Access:
The script uses MySQL to query course information. Make sure the MySQL credentials provided in the script have access to the Moodle database.
PHP:
The script invokes PHP to run Moodle’s CLI backup. Verify that PHP is installed and accessible from your command line.
Bash Shell:
The script is written in Bash and requires a Unix-like environment.
Clone or Download the Script:
Save the script (e.g., backup_moodle_courses.sh) to your desired directory.
Edit Configuration Variables:
Open the script and adjust the following variables as needed:
MOODLE_ROOT — Path to your Moodle installation (e.g., /var/www/html/oukv2).DB_HOST, DB_USER, DB_PASS, DB_NAME — MySQL database connection details.DESTINATION_DIR — Where backups and logs will be stored.COURSE_SHORTNAMES — Default list of course short names (if not using an external file).File Permissions:
Make the script executable:
chmod +x backup_moodle_courses.sh
You can run the script in two ways:
Create a text file (e.g., courses.txt) containing one course short name per line:
course_short1
course_short2
course_short3
Run the script by providing the filename as an argument:
./backup_moodle_courses.sh courses.txt
If no file is provided as an argument, the script uses the predefined list of course short names defined within the script:
COURSE_SHORTNAMES=("course_short1" "course_short2" "course_short3")
Simply run:
./backup_moodle_courses.sh
backup_YYYYMMDD.log (based on the current date).--users=0 parameter.Course Not Found:
If a course short name is not found in the database, the script logs an error and skips that course.
Backup Failures:
Review the log file in the backup directory for detailed error messages and troubleshooting information.
Testing:
It is highly recommended to test the backup process in a development or staging environment before using it in production.
Data Integrity:
This script relies on Moodle’s CLI backup tool. Make sure to verify your backup settings and regularly test restore procedures to ensure data integrity.
Specify your license here (if applicable).
This README.md should help users understand how to configure, run, and troubleshoot the Moodle Course Backup Script.