prepare($insert); $update = "UPDATE signatures SET filename = :filename , filesize = :filesize WHERE sha1 = :sha1"; $stmt2 = $file_db->prepare($update); $delete = "DELETE FROM signatures WHERE sha1 = :sha1"; $stmt3 = $file_db->prepare($delete); my_echo_with_eol("Searching SHA1 in database for $basename"); $result = $file_db->query("SELECT filename, filesize FROM signatures WHERE sha1 = '$sha1_fichier'"); if ($result !== FALSE) { foreach($result as $f) { my_echo_with_eol("Found ".$f['filename']); if ($f['filesize'] != $filesize) my_echo_with_eol("Oups, database and file filesize differ !"); $in = $destination.'/'.$f['filename']; $out = $archive; if (file_exists($in)) link($in, $out); if (file_exists("$in.gz")) link("$in.gz", "$out.gz"); if (file_exists($out)||file_exists("$out.gz")) { $stmt2->execute(array(':sha1' => $sha1_fichier, ':filename' => $filename, ':filesize' => $filesize)); $flag = true; my_echo_with_eol("Update database filename : $filename"); } if (!$flag) { my_echo_with_eol("Delete database sha1 : $sha1_fichier"); $stmt3->execute(array(':sha1' => $sha1_fichier)); } } } if (!$flag) { my_echo_with_eol("Not Found $basename"); // Les formats compressés sont juste copiés $ext = pathinfo($fichier, PATHINFO_EXTENSION); if ($ext == "avi") copy($fichier, $archive); if ($ext == "mkv") copy($fichier, $archive); if ($ext == "mp4") copy($fichier, $archive); if ($ext == "mp3") copy($fichier, $archive); if ($ext == "bz2") copy($fichier, $archive); if ($ext == "gz") copy($fichier, $archive); if ($ext == "zip") copy($fichier, $archive); if ($ext == "iso") copy($fichier, $archive); if (!file_exists($archive)) { my_echo_with_eol("Compress $basename"); gzCompressFile($fichier, "$archive.gz"); } else my_echo_with_eol("Copy $basename"); my_echo_with_eol("Insert database filename : $filename"); $stmt1->execute(array(':sha1' => $sha1_fichier, ':filename' => $filename, ':filesize' => $filesize)); } my_echo_with_eol("--------- "); return TRUE; } function listeDossier($file_db, $dossier, $destination) // Fonction qui liste un dossier de façon récursive { $ladate = date('Ymd'); if (file_exists("$destination/$ladate/$dossier")) { my_echo_with_eol('BACKUP already done'); return true; } if (is_dir($dossier)) { if (!file_exists("$destination/$ladate")) mkdir("$destination/$ladate"); $path = explode("/", $dossier); $init_path = "$destination/$ladate"; foreach($path as $p) { $init_path .= '/'.$p; if (!file_exists($init_path)) mkdir($init_path); } my_echo_with_eol("BACKUP OF $dossier"); if($dossierOuvert=opendir($dossier)) { $n = 0; while(($fichier=readdir($dossierOuvert))!== false) { // if ($n++ == 8) break; if ($fichier[0] == '.') continue; if(is_dir("$dossier/$fichier")) listeDossier($file_db, "$dossier/$fichier", $destination); else fileBackup($file_db, "$dossier/$fichier", $destination); } } } else { my_echo_with_eol("Erreur, le paramètre précisé dans la fonction n'est pas un dossier!"); } } $source = $argv[1]; $backup = $argv[2]; $signatures=$backup.'/signatures.sqlite'; if (!file_exists($source)) die("$source NOT FOUND !"); if (!is_dir($backup)) die("$backup NOT FOUND !"); if (!file_exists($signatures)) die("$signatures NOT FOUND !"); $connexion = new PDO('sqlite:'.$signatures); my_echo_with_eol("DATABASE IS OPEN"); if(is_dir($backup)) listeDossier($connexion, $source, $backup); else fileBackup($connexion, $source, $backup); $connexion = null; my_echo_with_eol("DATABASE IS CLOSED"); ?>