by Felina Evangelica Setiawan.
Of course. Here is the full explanation, translated into English.
Let's break down Ken's answer.
Ken's response is excellent and directly answers your question. It's a practical solution from an experienced Moodle administrator. Ken is essentially saying:
"Moodle doesn't have a built-in button or plugin to perform backups by category. However, if you have access to the server terminal, we can 'trick' the system by combining two existing tools to get exactly the result you want."
Let's explain each part of his answer in detail.
A Complete Analysis of Ken's Answer
1. The Core Problem: Moodle Works Per-Course, Not Per-Category
First, Ken confirms your issue. Whether through the web interface or Moodle's built-in command-line interface (CLI) scripts, the backup function is designed to operate on one course at a time. The script has a --courseid option, but no --categoryid option. He also checked the Moodle plugins directory and confirmed that no existing plugin does this directly.
2. The Smart, Two-Step Solution
Ken provides a clever solution that consists of two main steps:
Step A: Get a List of All Courses Within a Category.
How? By asking Moodle's "heart"—its database—directly.
The Command: select id,category,fullname from mdl_course where category=43;
Explanation: This command tells the MySQL database: "Please look inside the mdl_course table (the table that contains all courses) and show me the id column (the unique ID for each course), the category column (the ID of the category the course is in), and the fullname column. However, I only want to see the ones where the category is 43."
The Result: As shown in his example, the database will return a list of all the course IDs (42, 43, 44, 45, 387) that belong to Category ID 43.
Step B: Back Up Each Course Individually Using the List from Step A.
How? By using the standard command-line backup script (backup.php) provided by Moodle, but running it repeatedly for each ID obtained from the database.
The Command:
code
Bash
download
content_copy
expand_less
php ./backup.php --courseid=42 --destination=/home/neweployee2025/
php ./backup.php --courseid=43 --destination=/home/newemployee2025/
... and so on ...
Explanation: You now have an accurate list of course IDs. You just need to run Moodle's standard backup command for each of those IDs, one by one.
3. Simple Automation with a Script
Ken knows that running commands one by one is tedious. So, he suggests combining all the backup commands into a single file.
The Command: nano donewemplyeebu (You can name this anything, for example, backup_category_43.sh).
The Content: Simply copy and paste all the php ./backup.php ... command lines into that file.
How to Run It: source ./donewemployeebu. This command will execute every line in the file sequentially, ensuring all courses are backed up automatically.
Conclusion: Does This Answer Help You?
Yes, 100%. This is the most common and reliable method used by Moodle administrators to do exactly what you've asked for: a mass backup based on a category.
Additional Suggestion: Improving Ken's Method (Full Automation)
Ken's method is great, but it still has a manual step (copying the IDs from the database into the script file). If you want to make it more advanced and fully automatic, you can combine both steps into one intelligent BASH script.
Here is an example of a more automated script (you can save it as backup_by_category.sh):
code
Bash
download
content_copy
expand_less
IGNORE_WHEN_COPYING_START
IGNORE_WHEN_COPYING_END
#!/bin/bash
# --- CONFIGURATION ---
CATEGORY_ID="43" # Change this to the ID of the category you want to back up
BACKUP_DIR="/home/newemployee2025/" # Make sure this directory exists
MOODLE_DIR="/var/www/moodle-web/moodle/admin/cli/" # Path to your Moodle CLI directory
# Database Configuration (Change with your details)
DB_USER="moodle_db_user"
DB_PASS="moodle_db_password"
DB_NAME="moodle_db_name"
# --- END OF CONFIGURATION ---
echo "Starting backup process for category ID: $CATEGORY_ID"
# Step 1: Get the list of course IDs from the database
COURSE_IDS=$(mysql -u$DB_USER -p$DB_PASS $DB_NAME -e "SELECT id FROM mdl_course WHERE category=$CATEGORY_ID;" | grep -v id)
if [ -z "$COURSE_IDS" ]; then
echo "No courses found in category ID $CATEGORY_ID."
exit 1
fi
echo "Courses to be backed up: $COURSE_IDS"
# Step 2: Loop through and back up each course
for id in $COURSE_IDS; do
echo "------------------------------------"
echo "Backing up course with ID: $id"
php $MOODLE_DIR/backup.php --courseid=$id --destination=$BACKUP_DIR
echo "Backup for course ID $id completed."
done
echo "------------------------------------"
echo "MASS BACKUP PROCESS COMPLETED!"
ls -l $BACKUP_DIR
With this script, you only need to change the CATEGORY_ID at the top, and the script will automatically fetch the course list and back up everything without any manual intervention.
Is this answer what you meant?
Then,
Can the entire workflow be accomplished through API calls? Specifically, I want to check if the following steps are possible:
API Checks:
Can I get a Category ID (for example, by searching for its name)?
Can I get all the Course IDs that belong to that specific Category ID?
Can I trigger a course backup for each of those course IDs, perhaps inside a loop?
Let's break down Ken's answer.
Ken's response is excellent and directly answers your question. It's a practical solution from an experienced Moodle administrator. Ken is essentially saying:
"Moodle doesn't have a built-in button or plugin to perform backups by category. However, if you have access to the server terminal, we can 'trick' the system by combining two existing tools to get exactly the result you want."
Let's explain each part of his answer in detail.
A Complete Analysis of Ken's Answer
1. The Core Problem: Moodle Works Per-Course, Not Per-Category
First, Ken confirms your issue. Whether through the web interface or Moodle's built-in command-line interface (CLI) scripts, the backup function is designed to operate on one course at a time. The script has a --courseid option, but no --categoryid option. He also checked the Moodle plugins directory and confirmed that no existing plugin does this directly.
2. The Smart, Two-Step Solution
Ken provides a clever solution that consists of two main steps:
Step A: Get a List of All Courses Within a Category.
How? By asking Moodle's "heart"—its database—directly.
The Command: select id,category,fullname from mdl_course where category=43;
Explanation: This command tells the MySQL database: "Please look inside the mdl_course table (the table that contains all courses) and show me the id column (the unique ID for each course), the category column (the ID of the category the course is in), and the fullname column. However, I only want to see the ones where the category is 43."
The Result: As shown in his example, the database will return a list of all the course IDs (42, 43, 44, 45, 387) that belong to Category ID 43.
Step B: Back Up Each Course Individually Using the List from Step A.
How? By using the standard command-line backup script (backup.php) provided by Moodle, but running it repeatedly for each ID obtained from the database.
The Command:
code
Bash
download
content_copy
expand_less
php ./backup.php --courseid=42 --destination=/home/neweployee2025/
php ./backup.php --courseid=43 --destination=/home/newemployee2025/
... and so on ...
Explanation: You now have an accurate list of course IDs. You just need to run Moodle's standard backup command for each of those IDs, one by one.
3. Simple Automation with a Script
Ken knows that running commands one by one is tedious. So, he suggests combining all the backup commands into a single file.
The Command: nano donewemplyeebu (You can name this anything, for example, backup_category_43.sh).
The Content: Simply copy and paste all the php ./backup.php ... command lines into that file.
How to Run It: source ./donewemployeebu. This command will execute every line in the file sequentially, ensuring all courses are backed up automatically.
Conclusion: Does This Answer Help You?
Yes, 100%. This is the most common and reliable method used by Moodle administrators to do exactly what you've asked for: a mass backup based on a category.
Additional Suggestion: Improving Ken's Method (Full Automation)
Ken's method is great, but it still has a manual step (copying the IDs from the database into the script file). If you want to make it more advanced and fully automatic, you can combine both steps into one intelligent BASH script.
Here is an example of a more automated script (you can save it as backup_by_category.sh):
code
Bash
download
content_copy
expand_less
IGNORE_WHEN_COPYING_START
IGNORE_WHEN_COPYING_END
#!/bin/bash
# --- CONFIGURATION ---
CATEGORY_ID="43" # Change this to the ID of the category you want to back up
BACKUP_DIR="/home/newemployee2025/" # Make sure this directory exists
MOODLE_DIR="/var/www/moodle-web/moodle/admin/cli/" # Path to your Moodle CLI directory
# Database Configuration (Change with your details)
DB_USER="moodle_db_user"
DB_PASS="moodle_db_password"
DB_NAME="moodle_db_name"
# --- END OF CONFIGURATION ---
echo "Starting backup process for category ID: $CATEGORY_ID"
# Step 1: Get the list of course IDs from the database
COURSE_IDS=$(mysql -u$DB_USER -p$DB_PASS $DB_NAME -e "SELECT id FROM mdl_course WHERE category=$CATEGORY_ID;" | grep -v id)
if [ -z "$COURSE_IDS" ]; then
echo "No courses found in category ID $CATEGORY_ID."
exit 1
fi
echo "Courses to be backed up: $COURSE_IDS"
# Step 2: Loop through and back up each course
for id in $COURSE_IDS; do
echo "------------------------------------"
echo "Backing up course with ID: $id"
php $MOODLE_DIR/backup.php --courseid=$id --destination=$BACKUP_DIR
echo "Backup for course ID $id completed."
done
echo "------------------------------------"
echo "MASS BACKUP PROCESS COMPLETED!"
ls -l $BACKUP_DIR
With this script, you only need to change the CATEGORY_ID at the top, and the script will automatically fetch the course list and back up everything without any manual intervention.
Is this answer what you meant?
Then,
Can the entire workflow be accomplished through API calls? Specifically, I want to check if the following steps are possible:
API Checks:
Can I get a Category ID (for example, by searching for its name)?
Can I get all the Course IDs that belong to that specific Category ID?
Can I trigger a course backup for each of those course IDs, perhaps inside a loop?