Пример #1
0
 /**
  * returns file name for storage in the ARCHIVED of DELETED storage areas
  */
 function getStorageName()
 {
     return BackupFile::makeStorageName($this->fileid, $this->fname, $this->version);
 }
Пример #2
0
 function checkDatabase()
 {
     global $LNK;
     $sql = "SELECT * FROM files;";
     $result = @mysqli_query($LNK, $sql);
     if (!$result) {
         $e = new FileCopyMessage(__FILE__ . "@" . __LINE__ . ": " . mysqli_error($LNK) . " ( SQL = " . $sql . ")");
         return false;
     }
     $cnt = 0;
     while ($row = mysqli_fetch_assoc($result)) {
         $f = new BackupFile($row['fileid']);
         $f->version = $row['version'];
         $f->fromDB();
         $origName = $f->getFullOrigPath();
         $storageName = $f->getFullBackupPath($this);
         // migrate archived and deleted files to their new position:
         if (FALSE) {
             if ($row["status"] == 'A' or $row["status"] == 'D') {
                 $version = $row["version"];
                 $newF = BackupFile::makeStorageName($row["fileid"], $row["fname"], $version);
                 $oldF = explode("/", $newF);
                 array_shift($oldF);
                 $oldF = implode("/", $oldF);
                 if ($row["status"] == 'A') {
                     $dirname = $this->archiveDir;
                 }
                 if ($row["status"] == 'D') {
                     $dirname = $this->delDir;
                 }
                 $oldF = $dirname . $oldF;
                 $newF = $dirname . $newF;
                 if (file_exists($oldF)) {
                     if (copy($oldF, $newF)) {
                         $msg = "moving: " . $oldF . " TO " . $newF;
                         $e = new FileCopyMessage($msg, 'NOTICE');
                         echo $msg . "\n";
                         if (!unlink($oldF)) {
                             $msg = "Cannot delete old archive/deleted file " . $oldF;
                             $e = new FileCopyMessage($msg, 'WARN');
                             echo $msg . "\n";
                         }
                     } else {
                         $msg = "Could not move " . $oldF . " TO " . $newF;
                         $e = new FileCopyMessage($msg, 'WARN');
                         echo $msg . "\n";
                     }
                 } else {
                     if (!file_exists($newF)) {
                         if ($row["status"] == 'A') {
                             $oldDelF = str_replace($this->archiveDir, $this->delDir, $oldF);
                             if (file_exists($oldDelF)) {
                                 $msg = "Found " . $oldF . " erroneously in delDir.";
                                 $e = new FileCopyMessage($msg, 'WARN');
                                 echo $msg . "\n";
                                 copy($oldDelF, $newF);
                                 unlink($oldDelF);
                             } else {
                                 /*
                                 $msg = $oldF." does not exist at all (".$oldDelF.").";
                                 $this->dbreport('NOTICE', $msg); echo $msg."\n";
                                 */
                             }
                         }
                         if ($row["status"] == 'D') {
                             $oldArchiveF = str_replace($this->delDir, $this->archiveDir, $oldF);
                             if (file_exists($oldArchiveF)) {
                                 $msg = "Found " . $oldF . " erroneously in archiveDir.";
                                 $e = new FileCopyMessage($msg, 'WARN');
                                 echo $msg . "\n";
                                 $sql2 = "UPDATE files SET status='A' WHERE uid=" . $row["uid"] . ";";
                                 // echo $sql2."\n";
                                 mysqli_query($LNK, $sql2);
                             }
                         }
                     }
                     if (!file_exists($newF)) {
                         /*
                         $msg = $newF." not found.";
                         $this->dbreport('NOTICE', $msg);
                         echo $msg."\n";
                         */
                         if ($row["status"] == 'A') {
                             $newDelF = str_replace($this->archiveDir, $this->delDir, $newF);
                             if (file_exists($newDelF)) {
                                 $msg = "Found " . $newF . " erroneously in delDir.";
                                 $e = new FileCopyMessage($msg, 'WARN');
                                 echo $msg . "\n";
                                 copy($newDelF, $newF);
                                 unlink($newDelF);
                             } else {
                                 $cnt++;
                                 $msg = $newF . " does not exist at all (" . $newDelF . ").";
                                 $e = new FileCopyMessage($msg, 'WARN');
                                 echo $msg . "\n";
                                 $sql2 = "DELETE FROM files WHERE uid=" . $row["uid"] . ";";
                                 mysqli_query($LNK, $sql2);
                             }
                         }
                         if ($row["status"] == 'D') {
                             $newArchiveF = str_replace($this->delDir, $this->archiveDir, $newF);
                             if (file_exists($newArchiveF)) {
                                 $msg = "Found " . $newF . " erroneously in archiveDir.";
                                 $e = new FileCopyMessage($msg, 'WARN');
                                 echo $msg . "\n";
                                 copy($newArchiveF, $newF);
                                 unlink($newArchiveF);
                             } else {
                                 $found = false;
                                 if ($version > 0) {
                                     $previousF = $this->archiveDir . BackupFile::makeStorageName($row["fileid"], $row["fname"], $version - 1);
                                     if (file_exists($previousF)) {
                                         $found = true;
                                         $msg = "Found previous version of missing del file: " . $previousF;
                                         $e = new FileCopyMessage($msg, 'NOTICE');
                                         echo $msg . "\n";
                                         // find previous record:
                                         $sql2 = "SELECT uid FROM files WHERE fileid=" . $row["fileid"] . " AND version=" . ($version - 1) . ";";
                                         // echo $sql2."\n";
                                         if ($result2 = mysqli_query($LNK, $sql2)) {
                                             if ($row2 = mysqli_fetch_assoc($result2)) {
                                                 // ...and update it to 'D'
                                                 $sql2 = "UPDATE files SET status='D' WHERE uid=" . $row2["uid"] . ";";
                                                 // echo $sql2."\n";
                                                 mysqli_query($LNK, $sql2);
                                                 // delete this 'D' record (now renegade)
                                                 $sql2 = "DELETE FROM files WHERE uid=" . $row["uid"] . ";";
                                                 // echo $sql2."\n";
                                                 mysqli_query($LNK, $sql2);
                                                 // move the archive file to del file, maintain lower version!
                                                 $delFile = str_replace($this->archiveDir, $this->delDir, $previousF);
                                                 copy($previousF, $delFile);
                                                 unlink($previousF);
                                             }
                                         }
                                         $cnt++;
                                     }
                                 }
                                 if (!$found) {
                                     $msg = $newF . " does not exist at all.";
                                     $e = new FileCopyMessage($msg, 'WARN');
                                     echo $msg . "\n";
                                     $sql2 = "DELETE FROM files WHERE uid=" . $row["uid"] . ";";
                                     // echo $sql2."\n";
                                     mysqli_query($LNK, $sql2);
                                 }
                             }
                         }
                     }
                 }
             }
         }
         // if ($cnt > 0) break;
         // continue;
         // trifft ein Ausschluss-Muster auf diese Datei zu?
         foreach ($this->excludePatterns as $excludeDir) {
             if (!(strpos($origName, $excludeDir) === false)) {
                 $cnt++;
                 $fileOkay = "";
                 if (!file_exists($storageName)) {
                     $fileOkay = ", NOT FOUND";
                 }
                 $msg = $origName . " (" . $row["status"] . ", " . $storageName . $fileOkay . ") besitzt Backup(s), wird jetzt aber von einem Exclude-Pattern erfasst.";
                 if (mb_strlen($fileOkay) > 0) {
                     $e = new FileCopyMessage($msg, 'WARN');
                 } else {
                     $e = new FileCopyMessage($msg, 'NOTICE');
                     // delete record...
                     $sql2 = "DELETE FROM files WHERE uid=" . $row["uid"] . ";";
                     mysqli_query($LNK, $sql2);
                     // and delete file
                     unlink($storageName);
                 }
                 echo $msg . "\n";
                 // stop after first match.
                 break;
             }
         }
         // SHA1?
         if (mb_strlen($row["sha1"]) == 0) {
             if (file_exists($storageName)) {
                 $sha1 = sha1_file($storageName);
                 $msg = "Aktualisiere SHA1-Hash von " . $storageName . ": " . $sha1;
                 $e = new FileCopyMessage($msg, 'NOTICE');
                 echo $msg . "\n";
                 $sql2 = "UPDATE files SET sha1='" . $sha1 . "' WHERE uid=" . $row["uid"] . ";";
                 mysqli_query($LNK, $sql2);
             } else {
                 // delete record...
                 if (!file_exists($origName)) {
                     $sql2 = "DELETE FROM files WHERE uid=" . $row["uid"] . ";";
                     mysqli_query($LNK, $sql2);
                 }
             }
         }
         // maybe there is a record, but no file?
         if (!file_exists($storageName)) {
             // delete record...
             $sql2 = "DELETE FROM files WHERE uid=" . $row["uid"] . ";";
             mysqli_query($LNK, $sql2);
             $msg = 'A backup record has been deleted for ' . $origName . ' / ' . $storageName . ': The stored copy of the file does not actually exist.';
             $e = new FileCopyMessage($msg, 'WARN');
             echo $msg . "\n";
         }
         // if ($cnt > 100) break;
     }
 }