Write and external script to delete manual backups possible?

Write and external script to delete manual backups possible?

by Jerry Lau -
Number of replies: 8

We are on Moodle 3.4.2+ and soon we will upgrade to 3.6.x running on redhat enterprise 7.5, 64-bit with php 7.1.8 and MySQL 5.7.24-community edition.


We have decided to tell users that we will delete their manual course backup file that have been there for more than 6 months off the system. This is to free our moodledata space and for housekeeping.

Can I write a script (any language) to look through the file table, select the manual ones and then delete them on the /moodledata folder (I know which column in the table to look for its path)? would that affect the integrity of the table in any way after I delete the associated row in the table and the file itself?

thanks


Average of ratings: -
In reply to Jerry Lau

Re: Write and external script to delete manual backups possible?

by Darko Miletić -
Why? Moodle already has an option to delete backups older than some period

of time.



Check this document:

https://docs.moodle.org/36/en/Automated_course_backup



On Sun, Feb 17, 2019, 9:11 PM Jerry Lau (via Moodle.org) <noreply@moodle.org
In reply to Darko Miletić

Re: Write and external script to delete manual backups possible?

by Jerry Lau -
No talking about automated ones.. the manual ones created by users.
In reply to Jerry Lau

Re: Write and external script to delete manual backups possible?

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

The safest way to delete files is to write a PHP script that uses Moodle library functions to do the deletion (that way there is no danger that the file records + the actual files in Moodle data can get out of sync).

In your script you want to get a list of the mdl_file records that you want to delete, create stored_file instances from them, then call delete() on those instances:

$fs = get_file_storage();
$files = $DB->get_records_sql("query to return the file records you are interested in");
foreach ($files as $file) {
    $storedfile = $fs->get_file_instance($file);
    $storedfile->delete();
}

I'd have to look into the database to figure out how to write the query, so can't answer that part of the top of my head - please reply if you need help with that bit.

Average of ratings: Useful (3)
In reply to Davo Smith

Re: Write and external script to delete manual backups possible?

by Jerry Lau -

Hello Davo.

Yes please. That would be helpful.. I was just going to parse through the file table and delete any files longer than 6 months that were not updated and whose backup is not automated. The would return me the course id and the hash of where the files are stored and delete both the table entry and also the actual file itself in moodledata's filedir directory

In reply to Davo Smith

Re: Write and external script to delete manual backups possible?

by Jerry Lau -

Still can't get it LOL

In reply to Jerry Lau

Re: Write and external script to delete manual backups possible?

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Well, you'd have to look in the database for your system to check, but the SQL query is going to look something like:

$sql = "SELECT * FROM {files} WHERE component = 'user' AND filearea = 'backup' AND timemodified < :time";

$params = ['time' => time() - (6 * 30 * DAYSECS)];

$files = $DB->get_records_sql($sql, $params);

But, again, you'd have to look in your database to double-check exactly which 'filearea' and 'component' values you are looking for (the local DB only has a 'no user' backup, which, from memory, I think are stored slightly differently).

In reply to Davo Smith

Re: Write and external script to delete manual backups possible?

by Jerry Lau -

will these files be move to the /trash directory and when the cron is run, it will delete them right?