RecoveringVideosFromLostAndFound

From MythTV Official Wiki
Revision as of 20:34, 6 December 2007 by Alexp789 (talk | contribs) (Initial edit.)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Introduction

I recently had a problem with a hard drive that was playing up. As a result my JFS partition that was striped across 4 drives decided to drop all my mythtv recordings into "lost+found". These files were placed into the lost+found folder with filenames in the form of "I303105.RCN". This made restoring the files very difficult. By comparing the modified time on the files, with the end time of the recording it was possible (mostly) to determine which file was which recording. Thus i wrote the following script, which scans the "recorded" table (for recordings mythtv thinks it should have), and the modified times on the file, and restores the files accordingly.

How To:

I wrote this script for use on my machine, it will most definitely need changing if you wish to re-use it. DO NOT SIMPLY EXECUTE THIS SCRIPT, READ THROUGH IT AND MAKE NECESSARY CHANGES.

Save this script as lostfiles.sh, and make it executable (chmod u+x lostfiles.sh).

#!/usr/bin/php
<?php

$con = mysql_connect('mysql_server', 'mysql_user', 'mysql_password');

mysql_select_db('mythconverg');

$query = 'SELECT * from recorded order by title';
$result = mysql_query($query);

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $chanid = $line["chanid"];
        $dateid = $line["endtime"];
        $startdate = $line["starttime"];
        $startdate = ereg_replace("[^A-Za-z0-9]", "", $startdate );
        $dateid = ereg_replace("[^A-Za-z0-9]", "", $dateid );
        $filename = $chanid."_".$startdate.".mpg";
        echo "\nProgram: ".$line["title"]."-".$line["subtitle"].": \n";
        //echo $filename;
        $year = substr($dateid, 0,4);
        $month = substr($dateid, 4,2);
        $day = substr($dateid, 6,2);
        $hour = substr($dateid, 8,2);
        $minute = substr($dateid, 10,2);
        $second = substr($dateid, 12,2);
        //echo $year."-".$month."-".$day." ".$hour.":".$minute.":".$second."\n";
        $dbtime = mktime($hour, $minute, $second, $month, $day, $year);

        $lostdir = '/myth/lost+found/';
        $mythdir = '/myth/';
        $nummatch = 0;
        if ($handle = opendir($lostdir)) {
                while (false !== ($file = readdir($handle))) {
                        if ($file != "." && $file != "..") {
                                $modtime = stat($lostdir.$file);
                                $modtime = $modtime['mtime'];

                                //echo "DB: ".$dbtime."\tMT: ".$modtime."\n";

                                if (substr($dbtime, 0, 8) == substr($modtime, 0, 8)) {
                                        $nummatch = $nummatch + 1;
                                        $matchsource = $lostdir.$file;
                                        $matchdest = $mythdir.$filename;
                                        echo "MATCH - Source: $matchsource Dest: $matchdest\n";
                                }

                        }
                }
        }
        if ($nummatch == 1) {
                //only one match, lets fix the file!
                echo "Moving ".$matchsource." to ".$matchdest."\n";
                rename ($matchsource, $matchdest);
        }
        closedir($handle);

}

mysql_free_result($result);

mysql_close($con);

?>

Unfortunately I've not got chance to write this up any further, this script could by much better, feel free to make changes/improvements and post them here.