2010-02-07 18 views
7

मैंने एक प्रशासनिक पैनल समेत अपनी वेबसाइट के लिए एक मूल सामग्री-प्रबंधन प्रणाली लिखी। मैं मूल फ़ाइल IO के साथ-साथ PHP के माध्यम से प्रतिलिपि समझता हूं, लेकिन स्क्रिप्ट से कॉल करने योग्य बैकअप स्क्रिप्ट पर मेरे प्रयास विफल हो गए हैं। मैंने यह करने की कोशिश की:PHP रिकर्सिव बैकअप स्क्रिप्ट

//... authentication, other functions 
for(scandir($homedir) as $buffer){ 
    if(is_dir($buffer)){ 
     //Add $buffer to an array 
    } 
    else{ 
     //Back up the file 
    } 
} 
for($founddirectories as $dir){ 
    for(scandir($dir) as $b){ 
     //Backup as above, adding to $founddirectories 
    } 
} 

लेकिन ऐसा लगता है कि यह काम नहीं कर रहा था।

मुझे पता है कि मैं इसे एफ़टीपी का उपयोग करके कर सकता हूं, लेकिन मुझे पूरी तरह से सर्वर-साइड समाधान चाहिए जो पर्याप्त प्रमाणीकरण के साथ कहीं भी पहुंचा जा सकता है।

+2

यह भयानक है, मैं कर रहा हूँ इस समय एक ही मुद्दे पर काम कर रहे थे, और मैंने लगभग एक ही मिनट पहले एक ही सवाल पूछा :) मैंने इसे हटा दिया, हालांकि, क्योंकि मुझे एक अच्छा मौजूदा प्रश्न दिया गया था: http://stackoverflow.com/questions/1334613/zip -ए-डायरेक्टरी-इन-पीएचपी –

+0

मुझे आश्चर्य है कि आपकी फाइलें प्राप्त करने के लिए कोई भी डायरेक्टर इटरेटर का उल्लेख नहीं किया गया है। – Chris

उत्तर

9

यहाँ हालांकि एक विकल्प है: क्यों क्या आप Zip the source directory instead नहीं हैं?

function Zip($source, $destination) 
{ 
    if (extension_loaded('zip') === true) 
    { 
     if (file_exists($source) === true) 
     { 
      $zip = new ZipArchive(); 

      if ($zip->open($destination, ZIPARCHIVE::CREATE) === true) 
      { 
       $source = realpath($source); 

       if (is_dir($source) === true) 
       { 
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST); 

        foreach ($files as $file) 
        { 
         $file = realpath($file); 

         if (is_dir($file) === true) 
         { 
          $zip->addEmptyDir(str_replace($source . '/', '', $file . '/')); 
         } 

         else if (is_file($file) === true) 
         { 
          $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file)); 
         } 
        } 
       } 

       else if (is_file($source) === true) 
       { 
        $zip->addFromString(basename($source), file_get_contents($source)); 
       } 
      } 

      return $zip->close(); 
     } 
    } 

    return false; 
} 

तुम भी यह बाद में अनज़िप और एक ही प्रभाव संग्रह कर सकते हैं, हालांकि मुझे कहना होगा कि मैं अपने बैकअप ज़िप फ़ाइल प्रारूप में संकुचित होने पसंद करते हैं।

2

आप रिकर्सन का उपयोग कर सकते हैं।

for(scandir($dir) as $dir_contents){ 
    if(is_dir($dir_contents)){ 
     backup($dir_contents); 
    }else{ 
     //back up the file 
    } 
} 
4

आप कार्यकारी समारोह के माध्यम से टार बाइनरी फ़ाइल निष्पादित करने के लिए उपयोग कर सकते है अगर यह तेजी से और बेहतर होगा मुझे लगता है कि:

exec('tar -zcvf ' . realpath('some directory') .'/*); 

या

chdir('some directory') 
exec('tar -zcvf ./*'); 
+0

बस एक साधारण सुधार: exec ('tar -zcvf backup.gzip'। Realpath ('कुछ निर्देशिका')। '/ *'); – Michelangelo

2

आप लगभग सही

$dirs = array($homedir); 
$files = array(); 

while(count($dirs)) { 
    $dir = array_shift($dirs); 
    foreach(glob("$dir/*") as $e) 
     if(is_dir($e)) 
     $dirs[] = $e; 
     else 
     $files[] = $e; 
} 
// here $files[] contains all files from $homedir and below 

ग्लोब() scandir से बेहतर है यह मिल गया() और अधिक सुसंगत उत्पादन

1

मैं हमें कुछ UPHP कहा जाता है की वजह से। ऐसा करने के लिए बस zip() पर कॉल करें। यहां:

<?php 
    include "uphplib.php"; 
    $folder = "data"; 
    $dest = "backup/backup.zip"; 
    zip($folder, $dest); 
?> 

यूपीएचपी एक PHP पुस्तकालय है। डाउनलोड: here

+0

लिंक का संबंध नहीं है –

0

मैं एक फ़ाइल बैकअप के लिए एक सरल समारोह का उपयोग करें:

<?php 
$oldfile = 'myfile.php'; 
$newfile = 'backuped.php'; 
copy($oldfile, $newfile) or die("Unable to backup"); 

echo 'Backup is Completed'; 
?> 
1

यहाँ एफ़टीपी, SCP, mysqldump, pg_dump और फाइल सिस्टम क्षमताओं के साथ एक बैकअप स्क्रिप्ट है https://github.com/skywebro/php-backup

संबंधित मुद्दे