Create a gist now

Instantly share code, notes, and snippets.

@jonmaim /zip.php
Last active Nov 23, 2017

What would you like to do?
PHP script to remotely create zip archives of your FTP
<?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." );
?>

jezweb commented Dec 28, 2015

seems like if there is a folder that cant be accessed it will break the backup rather than skipping that folder/file?

eg

Will backup 5480 to temporary/backup_1451275847.zip
ERROR: Could not add file: ./aspnet_client/system_web

Also, there is a typo in the instructions, arhive to be archive

kmob2 commented Apr 4, 2016

Doesn't seem to work, on neither HostGator, BlueHost or GoDaddy hosting. Says it's creating several backups.
Splits a fresh wordpress installation into 6 zip files. That's a bit silly.
But then no zip files or temporary folder is actually being created. No error out put and no error log.

Not working , "Will backup 30368 to temporary/backup_1487883088.zip
ERROR: Could not add file: ./.."

TheLEAX commented Nov 23, 2017

Hello, without manually creating folder "temporary", where zip.php is located on ftp, it wouldnt work :)
Maybe thats what others encountered too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment