Undeleting a recording in mythtv and mysql wizardry

by administrator 23. January 2010 02:11

 

 MySql is a registered trademark of Sun Microsystems, Inc.

 Disclaimer: This documentation is not official or authorized in any way by any third parties.  Use the information at your own risk.

I've been lucky enough to be able to live without an undelete feature on my computer for years.  For the most part, I haven't had many problems - usually I have a backup copy of the file I can work with.  In this case, I had a backup, but the problem was it wasn't just a file that needed to be restored.  I was trying to delete a recording in my mythtv system and I managed to hit the arrow key at the same time I hit the menu key.  Without realizing it, I had deleted a recording from the system that I hadn't watched yet.  If that wasn't bad enough, I also don't have the system set up in such a way that I can undelete any recordings.  Since I have the actual video file in a backup location and since I have running backups of the mysql database, I decided to take a quick look at how I might be able to recover the recording.

Putting the recording back into the storage group is by far the easiest part of the process.  The much larger problem I had to deal with is the database and I actually expected the task at hand to be a lot harder than it turned out to be.  What I wasn't prepared for, however, is just how much data mythtv stores about a single program.  To give an overview, here are some of the more important tables (as of 0.21-fixes):

  • recorded - This table contains the program start time, end time, title, subtitle, description and a few other pieces of information.
  • recordedmarkup - Contains the bookmarks used by the automatic commercial skip feature
  • recordedseek - A huge table containing seek information for each 1/2 second of recorded video.  A one hour program has about 13,000 entries on my particular system.
  • oldrecorded - This table contains much the same information as the recorded table, but it's only used by the scheduler.
  • recordedprogram - Contains in-depth program details for each recording in the system.
  • recordedrating - Contains rating information for each recorded program
  • recordmatch - A mystery table.  I assume this is used by the scheduler based on what I see of the table's columns, but the documentation in the wiki doesn't describe it. (http://www.mythtv.org/wiki/Category:DB_Table)

...and that's all of the places where data is changed when a program is deleted as far as I could figure out.  The information about the first three tables I got from the backend server source code in mainserver.cpp:DoDeleteInDB().  For my particular situation, I created a new database on another Linux system and restored the database to the state it was in prior to me deleting the program.  I then spent some time comparing the actual database to my restored database to figure out where the changes were.  For each place where I found data had been altered or deleted, I used mysqldump to create a script I could use to bring the data back into my live system.

mysqldump is the  reason I'm writing this post.  Sometimes it's easy to overlook the command line possibilities, but they sometimes contain some very powerful features.  In this case, I needed to dump out large amounts of information in a format that could be imported to the other database without having to do a lot of editing of the dump file to get the correct results.  As a rule of thumb, the more you can eliminate human involvement in a process, the less mistakes you'll make along the way - so editing dump files was something to be avoided. 

The primary information needed for this is the chanid and the starttime of the program.  Once you have that information, you can use it to select the information you want when you make the dump from the database.  Here is an example:

$ mysqldump --skip-opt --complete-insert --no-create-info --order-by-primary mythconverg --tables recordedseek --where="chanid = 1311 and starttime = '2010-01-14 21:00:00'" -u root -p >>program.sql

In this example, most of the options cause mysqldump to only output insert statements and the reason behind that is I didn't want or need to drop or create any tables which may have caused a much larger loss of data.  The --tables parameter tells mysqldump which tables you want in the output file.  The --where parameter allows you to select specific information from the table so that you don't get any information you don't need or want.  This is the key to using mysqldump effectively when you need to do a partial restore of information between two databases.  I repeated the above command for each table in the list above and created a script that could be run on the live system to import the information back into the live database.  Restoring the database information is easy:

$ mysql -u root -p mythconverg <program.sql

After entering a password and waiting a few seconds, I simply restarted mythfrontend and all of the information about the recording showed up in the watch recordings section of mythtv.  Playback is just as I would expect if I hadn't accidentally deleted the recording, so I'm going to consider this a success.

 

Tags: ,

Comments

3/2/2010 2:53:30 AM #

Very interesting post. Couldn't of written any better. Reading this post reminds me of my old bud. He constantly kept talking about this. I will send this post to him. Am sure he will have a good read. Thanks for sharing! Smile

Jennefer Russum United States | Reply

3/6/2010 2:26:05 AM #

Thanks for taking this opportunity to talk about this, I feel strongly about it and I benefit from learning about this subject. If possible, as you gain data, please update this blog with new information. I have found it extremely useful.

moon in my room United States | Reply

3/6/2010 6:46:14 AM #

Although I do concur with your move, I hit my own reservations.

Regards
Sheppard



granite kitchen counter United States | Reply

3/8/2010 3:36:36 AM #

WOW your page came up premier in Google. And this is what I was superficial for.

Regards
Sacco



plastic surgeon manhattan United States | Reply

Add comment




  Country flag

biuquote
Loading



Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About this website...

Sometime ago I wanted a place to post about the various computer, technology, and other stuff I run into periodically so I can give back to the many communities that have been helping me over the last 20 years.  So I registered this domain name, not knowing that my ability to spell accurately was going to be affected by my age.  Really - I used to spell a lot better when I was in high school than I do now, apparently. 

Frown

So, for those of you who are wondering why the domain is mis-spelled, it's because I didn't figure it out until after the domain was already registered.

There is also a possibility that you wound up here by mistake and that you are also afflicted with the same poor spelling that I am.  If that's the case, the site you were looking for is http://www.aperture.org    I point this out because I'm really not trying to get traffic that belongs to the other site.  I would really prefer that you get to where you intended to go.