Last active
July 30, 2024 09:19
-
-
Save jonmaim/1141513 to your computer and use it in GitHub Desktop.
PHP script to remotely create zip archives of your FTP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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." ); | |
?> | |
Is anyone going to add to this to fix up the basic errors and make it more useable. such as being able tto put backup in ANY folder in the root /public_html or /htdocs folder (chosen by user on some kind of UI/Interface which could have other options also)? A design for interface is needed also IMHO. Tweet Me if you want to work on this with me @Code_Collective
Will backup 5 to temporary/backup_1657823074.zip
Archive created successfully with 5 files.
So, where is the temporary folder?
There are any bash version to compress public_html and send to remote destination?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, without manually creating folder "temporary", where zip.php is located on ftp, it wouldnt work :)
Maybe thats what others encountered too.