recovery of accidentally deleted courses (v.2.2.6)

recovery of accidentally deleted courses (v.2.2.6)

by sue sugizaki -
Number of replies: 20

Hi,

I've just accidentally deleted a course and am trying to find a way to recover it. As far as I know, it's not so easy as on 1.9 I could easily find course backups.

Q: what should I do?

I'd appreciate any advice.

BR, sue

Average of ratings: Useful (1)
In reply to sue sugizaki

Re: recovery of accidentally deleted courses (v.2.2.6)

by Mike Shurance -

Sue,

Did you make a backup of the course? If so, is it in the Course backup area or the Private backup area?

In reply to sue sugizaki

Re: recovery of accidentally deleted courses (v.2.2.6)

by Ken Task -
Picture of Particularly helpful Moodlers

Not so easy is right! And could be an argument for 'automated backups' to not only the moodledata directory, but a directory outside of Moodle.

But, there still might be a backup file of the course if the cron job hasn't moved files to trashdir and emptied the trash! :|

A query like:

select * from `mdl_files` where (`component` like "backup" and `filename` like "%.mbz%")

will show if there are any backups … automated or course.

You'll have to determine which row of data to work with by the filename column - which isn't very descriptive unless you've turned on the option to include the course name/title in the backup file names (may not be a feature in your version).

Use the contenthash information to determine the directories the hashed named backups are located in.   Example: a contenthash of c20151cef6e8607f501e970be52eb48d6fe11335

cd moodledata/filedir/c2/01/

ls -l

will show a file: c20151cef6e8607f501e970be52eb48d6fe11335

To determine if that is a backup:

head -n 1 c20151cef6e8607f501e970be52eb48d6fe11335

should show PK or P on the first line.

Copy the c20151cef6e8607f501e970be52eb48d6fe11335 file out into a test directory.  Do NOT move ... copy! ;)


cp c20151cef6e8607f501e970be52eb48d6fe11335 /home/test/

Change into the test directory

cd /home/test/

Rename the c20151cef6e8607f501e970be52eb48d6fe11335 file to 'abackup.zip'.

Unzip the file:

unzip abackup.zip

If you see folders/files like:

files.xml      moodle_backup.log  questions.xml  users.xml
completion.xml  gradebook.xml  moodle_backup.xml  roles.xml
course          groups.xml     outcomes.xml       scales.xml

It is a backup! smile

To restore to a 2, change the abackup.zip file name to abackup.mbz, download the .mbz file, then attempt restore.

Best of luck!

'spirit of sharing', Ken

Average of ratings: Useful (5)
In reply to Ken Task

Re: recovery of accidentally deleted courses (v.2.2.6)

by sue sugizaki -

Hi, Ken!

This is exactly what I didn't get when I found long file names in Moodledata on my test site. We have a server guy and I can't access to our Moodle server. So I usually test things on my test site first.

Thank you for your advice.

 

In reply to sue sugizaki

Re: recovery of accidentally deleted courses (v.2.2.6)

by Ken Task -
Picture of Particularly helpful Moodlers

Have a 2.2.6+ server for which I haven't run the cron job in a long time.  Thus, the trashdir in moodledata still contains courses/files to be removed.   So same process as mentioned before only in the 'trashdir'.

once again, the cron job that empties the trashier must not have run.

cd moodledata/trashdir/
if the directory exist

ls -lR
list directory long and recursive

Will show something like this:

[root@sos trashdir]# ls -l
total 8
drwxrwxrwx 3 root root 4096 Dec 21 09:58 8d
drwxrwxrwx 3 root root 4096 Dec 21 09:58 fa
[root@sos trashdir]# ls -lR
.:
total 8
drwxrwxrwx 3 root root 4096 Dec 21 09:58 8d
drwxrwxrwx 3 root root 4096 Dec 21 09:58 fa

./8d:
total 4
drwxrwxrwx 2 root root 4096 Dec 21 09:58 8f

./8d/8f:
total 27988
-rw-rw-rw- 1 root root 28625837 Oct  3 08:12 8d8fcf878bbfc553fa6ae3f6d4af748a878743cd

./fa:
total 4
drwxrwxrwx 2 root root 4096 Dec 21 09:58 4b

./fa/4b:
total 16
-rw-rw-rw- 1 root root 12748 Oct  3 08:12 fa4bbdc44a9b6a79ea513f9ea0833ae4441fa31d

Backups are more than likely larger than other files.  In this example, notice the  8d8fcf878bbfc553fa6ae3f6d4af748a878743cd file  in ./8d/8f/
is 28625837 in size.

It might be a backup that hasn't been removed.

cd moodledata/trashdir/8d/8f/

cp 8d8fcf878bbfc553fa6ae3f6d4af748a878743cd /home/test/

cd /home/test/

mv 8d8fcf878bbfc553fa6ae3f6d4af748a878743cd acoursebackup.zip

unzip acoursebackup.zip

Again, if you see files/folders like:

course     gradebook.xml      moodle_backup.xml  roles.xml   users.xml
activities         files      groups.xml         outcomes.xml       scales.xml
completion.xml     files.xml  moodle_backup.log  questions.xml      sections

It's a backup! ;)

To see the course name:

head -n 4 moodle_backup.xml

will show:

<?xml version="1.0" encoding="UTF-8"?>
<moodle_backup>
  <information>
    <name>backup-moodle2-course-testmobile-20121003-0812.mbz</name>

Again, best of luck!

'spirit of sharing', Ken

Average of ratings: Useful (1)
In reply to Ken Task

Re: recovery of accidentally deleted courses (v.2.2.6)

by Rosario Carcò -

Sue, Ken, thanks a lot. This is one point i criticised in the new file-system: admins have a lot more troubles restoring single files. In 1.9 I had plain file backups to tapes every night and automated backup-zip-files of Moodle. So things got twice on my tape, once as plain files and once neatly zipped. And when a teacher missed one file or a whole course was upset, e.g. by copying word-html or mac-html into a course text field, I simply could restore every single file or the whole course.

Now you just described the many steps you have to take, and again, backing up only the database is not enough, you have to do also a file-backup. And as I criticised, if you have not access to the Moodle database, you can not even guess the files that belonged to a course, or even worse, if the file records have gone from the database you have no clue and  can only rely on the trashdir or some other relict in the hashed directory structure. No problem in a worst case scenario where you have to restore OS, File-System, Database, etc. but in normal daily business you won't have to restore everything, you just have teachers missing one file or messing up their course for some reason. And this was easy to handle with in 1.9 (I never had a total system loss in 8 years, but a lot of tiny restores as described.)

So I have to change my backup and restore strategies for Moodle 2. OK. The only next best advice I can give and that I practiced in the last 4 years is NEVER DELETE COURSES. As you never delete records in databases, never delete ANYTHING from a Hard-Disk or from any information system like Moodle. SIMPLY COPY or MOVE it elswhere. I move courses to a TODELETE Category, I make the courses UNAVAILABLE FOR STUDENTS, remove the Teachers so that they can not even find those courses in their myCourses list. In 1.9 this had the advantage, that the courses still got backed up to tape, whilst automatic backup-zip-files were skipped on unavailable courses or courses that were not modified for a certain period. And of course in case of need I could simply reactivate such courses. After 6 months or so, depending on your academic year, I would even consider keeping them 12 months or longer, I really deleted them, relying on the tapes where the backup-zip-files were kept another 12 months after I delete the whole course.

Rosario

Average of ratings: Useful (3)
In reply to Ken Task

Re: recovery of accidentally deleted courses (v.2.2.6)

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
Ken,

You are my absolute hero right now!!!  After spending two weeks on gamifying a course to get ready for next semester, I deleted it by mistake.  Luckily I was able to find the backup in the trash directory (moodledata/trash) and get it to restore!  For those other idiots out there, I found that going through the trash (lol) was not that bad.  Just looked for the larger files - you do not even need to rename them, just drag them right to the restore window and let it tell you what is in there.  I actually found the first backup file, I think there were about four in there, was the latest.

Deleting a course by mistake is of course a heck of a time to find that despite the fact that my backup logs told me I was restoring just fine, my server was disconnected from the remote backup folder and had never backed up one course.

Which brings me to the following questions -
Why, when I told automated backups to backup only to a remote server, are there backup files in my file directory (very thankful that there are but still curious)?
Why is automated backup logs telling me that all backups are successful when in fact they are not?
Average of ratings: Useful (1)
In reply to Emma Richardson

Re: recovery of accidentally deleted courses (v.2.2.6)

by Chris Collman -
Picture of Documentation writers

Thanks Sue, Ken and Emma for helping me think of another way.

I am on 2.13.  I do have a 500MB backup of the course I accidentally  deleted on my computer(another story see below).  I am moving it to one of my repositories and as I type restoring my deleted course from there.  My max upload size is 100 MB.  

I know some people can not change max file size but do have an FTP program, so this might work for them.  I think with newer versions you can drag and drop files like Sue described above smile

For those who want to learn what happened and a  trick if you are quick,  here a longer  story:

Yesterday morning I was moving courses around in the course management menu system and decided to hide one.  Must have been asleep because I clicked on the X right next to the open eye, and then automatically clicked OK before I realized  it was the DELETE OK button andd not the OK to move resource I had been doing the previous hour.  I frantically hit the cancel button many times.  I had other backups but not for this course on my computer.  

The trick: I was using Firefox and left it open showing something was processing.  I opened a Chrome browser and was able to get into the course and tell it to download the most recent automatic backup.   I got lucky because 45 minutes later I had a 500 MB.mbz file on my computer.   And when I checked later the course was no longer listed.  As  usual it another 45 minutes to FTP it back to Moodle and more than an hour to restore (I left it running all night).  That was definitely dodging the bullet. 

 I still curse the "new" file system at times like these but that is progress. However, I still remember 1.5 and when I did a lot of edits in docs.moodle with a quill pensmile  I am 90 minutes from the server and don't like Putty.  However I think I better change the max file size to 900MB, next week when the students are gone.   AND MAKE SURE I HAVE  BACKUPS EVERY COURSE OFF SITE (ie on this computer).  We only run a total of  10 courses a year so it is not that  big a deal.

Ken and Emma: I was not looking forward to downloading a bunch of trash files.  I had  a 20 folders with a subfolder containing a file and no quick way to determine what was what. I will re-read Ken's words.  Once I downloaded the backup, I found the only file in trash that had a file of exactly the size of the backup, dated today.   

Another victory for a forum!!!!


In reply to Chris Collman

Re: recovery of accidentally deleted courses (v.2.2.6)

by Kim Solis -

This forum is really great except I just don't get the code talk.  I am sorry, I am new at this and I am the administrator for our site, but I am flying by the seat of my pants trying to learn as I go.  (I desperately need formal training but we just don't have the money for it as we are non-profit learning institution.)  Anyway... I cannot find our moodledata/trashdir directory.  I put in the name of our site www.cursosilebtoluca.org and then did /moodledata/trashdir and it said that the page could not be found.  Is there another way to find it?  I really need to recover a deleted course.  I also went back through my log and cannot find any time where it shows it was deleted.  Is there a site log and not just my private user log? 

And then saying I can locate the trash directory, I don't really understand what to do after that.

I need a handbook Moodle Administration for Dummies!!! 

THanks so much! smile


In reply to Kim Solis

Re: recovery of accidentally deleted courses (v.2.2.6)

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

So firstly, you have reactivated a two year old post which probably doesn't help too much.  On your server, you will have a moodle folder where the code is located and a moodledata folder which contains all the content.  It is in the moodledata that you will find the trashdir folder.  You are not going to find through the web site - you need to actually get on the server where the site is installed.

Average of ratings: Useful (1)
In reply to Emma Richardson

Re: recovery of accidentally deleted courses (v.2.2.6)

by Matt Sullivan -
Many thanks to you and to Ken for this thread. Yours was short and accurate, while aken supplied some of the details I needed to find a 71Mb course in my trashdir. 


For the sake of those in 3.0, my trashdir was in data, not moodledata,  and the file date was approximately the date I deleted the course.

Because I deleted the course 6 weeks before I went to restore it, I had to go into the backups of the www directory on my ISP and get the archive that hadn't yet purged the large trash file.

***It took me 2 days of research to track down his thread, but it still saved me days of rework, and ensured that 6 weeks of Moodle course tweaks didn't get lost. While I still had my recordings and file assets, the only other Moodle backup I had was a skeleton outline with less than 10% of pages/assets added to the course.***


LESSON LEARNED: Backup and download .mbz to a secure location on a regular basis.

Average of ratings: Useful (1)
In reply to Matt Sullivan

Re: recovery of accidentally deleted courses (v.2.2.6)

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

LESSON LEARNED: Backup and download .mbz to a secure location on a regular basis.

A lesson that most people learn just after deleting something (myself included) smile


In reply to Marcus Green

Re: recovery of accidentally deleted courses (v.2.2.6)

by Matt Sullivan -

Am I correct in my theory that the deletion of the course deleted the backup as well?

Or is the backup not stored on the site and needs to be manually downloaded at time of backup?

In reply to Matt Sullivan

Re: recovery of accidentally deleted courses (v.2.2.6)

by Ken Task -
Picture of Particularly helpful Moodlers

From what I understand (which may/may not be accurate with 2.2.6 now as that is old) IF a course is hidden and there is a backup in private files/course backups/automated backups that means there is a link from the course to that file and thus the file would never be sent to trashdir.  Hiding courses doesn't regain any free space ... matter of fact, quite the opposite.

If a course was deleted and that backup file is no longer seen in private files or other areas, or even other courses eventually, the backup file is moved to the trashdir and removed from the system.

The only way to regain space is to remove/erase.  And unless you manually remove contents of trashdir the space gained won't 'show up' until cron/task jobs empties the trash (4 days time from when a file was moved into trashdir).

'spirit of sharing', Ken


In reply to Ken Task

Re: recovery of accidentally deleted courses (v.2.2.6)

by Matt Sullivan -

Thanks Ken, 


That jibes with what I saw: Upon deletion of the course, (what I now understand to be) the backup file was moved to the trashdir.

Fortunately, I was able to recover the trashdir from a system backup that occured in the interval between deletion of the course and before the cron emptied the trash.

Am I understanding that correctly?


Either way, I'll now be somehow disabling the course, rather than deleting it.
For a course that likely runs once per quarter, what's my best approach for shutting down a session and then starting another?

In reply to Matt Sullivan

Re: recovery of accidentally deleted courses (v.2.2.6)

by Ken Task -
Picture of Particularly helpful Moodlers

Now we're off into areas that only you can answer for yourself cause not everyone's situation is the same.

Obviously, you don't want to have to do what you did to recover a backup.

Here's what I might do if I wasn't under a records retention policy or something like that ...

Full backup of the course ... including students enrolled and their work.

Download that backup to archive it ... I can restore that course if needed.

Do another backup this time with no users no data from users, etc.   It's a course shell with the activities/links, etc. in it.

Download that backup for archiving purposes.

After doing both of those, delete the backups found by going to restore in the course.

Now reset the course.   Choose options to remove all students, their data, their forum postings, their files, their grades, etc.   It then becomes like it was before you allowed students to enroll.

Some catch 22's ... iF the full/user backup you've downloaded to archive is larger than the limits to upload, can't use it until upload limits increased.   On some shared remotely hosted situations you don't control that limit.    There's a CAP.   Probably only option is to upgrade the package ... shared hosting to VPS.

The same could be true of the no user backup you downloaded.

See what I mean? smile

'spirit of sharing', Ken


In reply to Ken Task

Re: recovery of accidentally deleted courses (v.2.2.6)

by Matt Sullivan -

Excellent, thank you!

Yes, I already had to track down the php.ini settings with the help of my hosting company. They lock out direct access to php.ini (where, as you know, the upload and post limits are set) but allow a GUI where I was able to set upload to appropriate size.

In reply to Kim Solis

Re: recovery of accidentally deleted courses (v.2.2.6)

by Ken Task -
Picture of Particularly helpful Moodlers

Once you find where the moodle code is located, check the config.php file for the path to your moodledata directory.

That's where Moodle stores files ... including backups.   However, much has changed from 1.9 and one of those changes *IS* Moodles file system.   No longer is there /pathto/moodledata/#/ directories where # is the course ID of a course.  In 2.x and beyond backups files could be in /pathto/moodledata/filedir/ *but* they are not in humanly recognizable names ... like 'backup....blah-blah-blah.zip'.   One has to use the mdl_files table looking for backups to see their names AND use the contenthash to determine actual location in filedir.

A DB query like:

select contenthash,userid,filename from mdl_files where filename like "%.mbz%";

Will show something like the following:

+------------------------------------------+--------+-------------------------------------------------------------------+
| contenthash                              | userid | filename                                                          |
+------------------------------------------+--------+-------------------------------------------------------------------+
| f349a6cf359f9ff6cc95125c5835e28b13f9e6d7 |     99 | backup-moodle2-course-64-kerlegon_chemistry-20130611-1045-nu.mbz  |

If you have PHPMyAdmin ... or any tool that allows you to search the DB, use that.

The user ID will be the user that created the backup ... more than likely the teacher.

The content hash ... if one deciphers it shows the location of the backup file:

In the example above:

moodledata/filedir/f3/49/ there is located a f349a6cf359f9ff6cc95125c5835e28b13f9e6d7 file which is really:

backup-moodle2-course-64-kerlegon_chemistry-20130611-1045-nu.mbz

Sorry, not sure there is a dummies book for this!

'spriit of sharing', Ken

In reply to Ken Task

Re: recovery of accidentally deleted courses (v.2.2.6)

by Ken Task -
Picture of Particularly helpful Moodlers

Follow up .... IF you do the following *before* the cron job empties files in /moodledata/trashdir/ .. you could recover a deleted backup of a course.

Am showing command line ... use whatever tool you have to browse files on your server.   This assumes you have located moodledata directory (again, path to which is found in config.php file of your moodle code directory.

cd /pathto/moodledata/trashdir

ls -lR (this will show all directories/files in trashdir

Might be a long list (depending), but what one is looking for is date (which would be/should be the date the deletion was made), and file size.

Here's an example (the paths probably do NOT exist on your server ... remember this is an example).  [ENTER] is not part of the command but indicates where/when you press the ENTER key on your keyboard.

[root@moodle trashdir]# pwd [ENTER] (shows what directory am located in)
/mnt/data/hs2moodledata/trashdir

ls -lR [ENTER]

Displays something like this:
[root@moodle trashdir]# ls -lR
.:
total 4
drwxrwxrwx 3 apache apache 4096 Jul  1 19:46 bf

./bf:
total 4
drwxrwxrwx 2 apache apache 4096 Jul  1 19:46 51

./bf/51:
total 1772380
-rw-rw-rw- 1 apache apache 1813139102 Mar  5 14:33 bf51f6c603ccf47ec3d1e1f6d7364cc9b370d375

The backup file that was just deleted (it was first created in Mar of this year) is in:

moodledata/trashdir/bf/51/

and it's name is: bf51f6c603ccf47ec3d1e1f6d7364cc9b370d375

One could copy that file out to another location and rename it on the fly:

[root@moodle trashdir]# pwd
/mnt/data/hs2moodledata/trashdir
[root@moodle trashdir]# cd bf/51/
[root@moodle 51]# ls -l
total 1772380
-rw-rw-rw- 1 apache apache 1813139102 Mar  5 14:33 bf51f6c603ccf47ec3d1e1f6d7364cc9b370d375

mv bf51f6c603ccf47ec3d1e1f6d7364cc9b370d375 /home/myhomedir/somebackup.mbz

'somebackup.mbz' is now a file name that the restore process will recognize.

You could do the command line things with whatever you have to browse files.

If you can't do this, contact hosting provider's helpdesk and ask for assistance.  Send them the link to this forum and this thread.

'spirit of sharing', Ken


In reply to sue sugizaki

Re: recovery of accidentally deleted courses (v.2.2.6)

by Pablo Barroso -

Hi Sue,

I you have the id of your lost course, you can access it (because the content is still there in that id) and then make a backup. Finally, restore it. I did it and it worked.