Cron Backups for Moodle Db and Moodledata

Create directory /var/backups

mkdir /var/backups
crontab -e

Add Database backup to the cron file:

#Backup Database (Before each day starts, backup Moodle prod DB from MySQL/MariaDB)
4 4 * * * root mysqldump dbname -u user -ppsaaword | gzip > /var/backups/moodle-db-`date +\%Y-\%m-\%d-\%H-\%M-\%S`.sql.gz

Add moodledata backup to the cron file:

4 10 * * * zip -r /var/backups/moodledata-`date +\%Y-\%m-\%d-\%H-\%M-\%S`.zipĀ  /var/www/moodledata

 

Save 30 lasts backups

# Keep 30 SQL database backups and moodledata backups (last 30 backups of moodle)
 2 2 * * * find /var/backups/* -mtime +10 -exec rm {} \;

 

Moodle upload custom file in PHP

How to upload custom file Moodle function

 if(isset($_FILES['imageactivity']) && !empty($_FILES['imageactivity'])){ 
 $file_url = $_FILES['imageactivity']['tmp_name'];
 $file_content = file_get_contents($file_url);
 $file_name = $_FILES['imageactivity']['name'];
 
 
 $fs = get_file_storage();
 $draftitemid = file_get_unused_draft_itemid(); 
 $contextid_local = 70;
 
 // Prepare file record object
 $fileinfo = array(
 'contextid' => 165, // ID of context 165
 'component' => 'user', // usually = table name
 'filearea' => 'draft', // usually = table name
 'itemid' => $draftitemid, // usually = ID of row in table 0
 'filepath' => '/', // any path beginning and ending in /
 'userid' => $USER->id,
 'source' => $file_name, 
 'filename' => $file_name); // any filename
 
 // Create file containing content
 $fs->create_file_from_string($fileinfo, $file_content); 
 
 file_prepare_draft_area($draftitemid, $contextid_local, 'local_metadata', 'image', 0); 
 file_save_draft_area_files($draftitemid, $contextid_local, 'local_metadata', 'image', $draftitemid); 
 }