Skip to content

Instantly share code, notes, and snippets.

@Ledzz
Created August 26, 2016 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ledzz/bb6c8f83edd6d47c2eeec175e0fa6ea3 to your computer and use it in GitHub Desktop.
Save Ledzz/bb6c8f83edd6d47c2eeec175e0fa6ea3 to your computer and use it in GitHub Desktop.
Creates archive
<?php
/*
*
* This script will backup your web site by remotely archiving all files on the root FTP directory.
* It will work even if your web server is memory limited buy splitting zips in several arhive files it they are too many files.
* All zip files will be stored in a directory called temporary which must be writable.
*
* How to use it:
* - Place the script at the root of your FTP.
* - Call http://yoursite.com/zip.php
* - In your FTP client go to temporary folder and download all backup_xxxxxx_x.zip files locally
* - Unzip everything with this command: for i in *.zip; do unzip $i; done;
* - Finally to avoid security issues, remove the script from the FTP.
*
*/
// increase script timeout value
ini_set('max_execution_time', 5000);
function show($str){
echo $str . "<br/>\n";
flush();
ob_flush();
}
$date = getdate();
$splitNum = 0;
$archive = "temporary/backup_" . $date[0];
$currentArchive = $archive . "_" . $splitNum . ".zip";
$zip = new ZipArchive();
if ($zip->open($currentArchive, ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}
$numFiles = 0;
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("./"));
foreach ($iterator as $key=>$value){
$numFiles += 1;
}
show( "Will backup $numFiles to $archive.zip" );
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("./"));
$numFiles = 0;
$counter = 0;
$maxFilePerArchive = 1000;
foreach ($iterator as $key=>$value){
$counter += 1;
if ($counter >= $maxFilePerArchive) {
$currentArchive = $archive . "_" . $splitNum++ . ".zip";
show( "Too many files: splitting archive, new archive is $currentArchive" );
$zip->close();
$zip = new ZipArchive();
if ($zip->open($currentArchive, ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}
$counter = 0;
}
//$i = $maxFilePerArchive*$splitNum + $counter;
if (! preg_match('/temporary\/backup_' . $date[0] . '/', $key)){
$zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
$numFiles += 1;
if ($numFiles % 300 == 0) {
show( "$numFiles" );
}
} else {
show( "Not backuping this file -> $key" );
}
}
// close and save archive
$zip->close();
show( "Archive created successfully with $numFiles files." );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment