Mes scripts de backup
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 

177 rindas
5.6 KiB

<?php
function my_echo_with_eol($msg)
{
  if (false) echo $msg.PHP_EOL;
}
function gzCompressFile($source, $dest, $level = 9){ 
    $mode = 'wb' . $level; 
    $error = false; 
    if ($fp_out = gzopen($dest, $mode)) { 
        if ($fp_in = fopen($source,'rb')) { 
            while (!feof($fp_in)) 
                gzwrite($fp_out, fread($fp_in, 1024 * 512)); 
            fclose($fp_in); 
        } else {
            $error = true; 
        }
        gzclose($fp_out); 
    } else {
        $error = true; 
    }
    if ($error)
        return false; 
    else
        return $dest; 
} 
function fileBackup($file_db, $fichier, $destination) // Fonction qui liste un dossier de façon récursive
{
    $ladate = date('Ymd');
    $basename = basename($fichier);
    $filename = str_replace('//', '/', "$ladate/$fichier");
    $filesize = filesize($fichier);
    $archive  = "$destination/$filename"; 
    $flag = false;
    // On sort si c'est un dossier ou un fichier caché
    if ($basename[0] == '.') return FALSE;
    if (is_dir($fichier))    return FALSE;
    // On sort si l'archive existe
    if (file_exists($archive))      return FALSE;
    if (file_exists("$archive.gz")) return FALSE;
    my_echo_with_eol("Compute SHA1 of $basename");
    $sha1_fichier = sha1_file($fichier);
    // Re-Hash with filesize to avoid conflict
    $sha1_fichier = sha1($sha1_fichier.$filesize);
    $insert = "INSERT INTO signatures (sha1, filename, filesize) VALUES (:sha1, :filename, :filesize)"; 
    $stmt1  = $file_db->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");
?>