I'm attempting to backup lots of website using this php code. The script basically goes through the whole public_html directory and adds all the folders and files recursively and zips them.
Here's what I've got so far:
if (!file_exists('/home/sites/'.$_SERVER['SERVER_NAME'].'/cbitsbackup/')) {
mkdir('/home/sites/'.$_SERVER['SERVER_NAME'].'/cbitsbackup/', 0777, true);
}
$filefront = '/home/sites/'.$_SERVER['SERVER_NAME'].'/cbitsbackup/backup-'.$_SERVER['SERVER_NAME'].'-day1';
$fileend = '.zip';
$time = time();
if(file_exists($filefront.$fileend)){
$file = $filefront.'-'.$time.$fileend;}
else{$file = $filefront.$fileend;};
zip_directory('/home/sites/'.$_SERVER['SERVER_NAME'].'/public_html',$file);
function zip_directory($source,$tempfile){
if(!extension_loaded('zip') || !file_exists($source)) return false;
$zip = new ZipArchive();
if(!$zip->open($tempfile,ZIPARCHIVE::CREATE)) return false;
$source = str_replace('\\','/',realpath($source));
if(is_dir($source) === true){
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
foreach($files as $file){
$file = str_replace('\\', '/', 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));}}
elseif(is_file($source) === true) $zip->addFromString(basename($source), file_get_contents($source));
return $zip->close();}
The main problem at the moment is memory constraints (64M) because I'm running on a shared hosting platform. As far as I can see, the zip file is stored in memory until it is 'completed'. This is problem because I have a lot of websites which are much over this size. Is there anyway to write the zip file progressively to disk so my script isn't killed by the system? Could I even write this in Perl to get around this limit?