Skip to content

Instantly share code, notes, and snippets.

@hansek
Created November 2, 2012 09:41
Show Gist options
  • Save hansek/3999791 to your computer and use it in GitHub Desktop.
Save hansek/3999791 to your computer and use it in GitHub Desktop.
MODX Revolution Backup script
<?php
/**
* MODX Revolution Backup script
*
* Useful for fast moving MODX Revolution from FTP (w/o SSH access) to local computer
*/
$starttime = microtime(true);
// ini_set('max_execution_time', 300);
echo 'TIME LIMIT: ', ini_get('max_execution_time') ,' s <br />';
echo 'MEMORY LIMIT: ', ini_get('memory_limit') ,' <br />';
echo '<br />';
echo 'START MEMORY: ', convert(memory_get_usage (true)) ,'<br />';
error_reporting(E_ALL);
global $exclude, $allowed_files;
$windows = false; // set to true if here is Windows platform
$exclude = array(
dirname(__FILE__) .'/plesk-stat' => false,
dirname(__FILE__) .'/vapor' => false,
dirname(__FILE__) .'/_build' => false,
//dirname(__FILE__) .'/media/' => false,
dirname(__FILE__) .'/assets/components/phpthumbof/cache/' => false,
dirname(__FILE__) .'/assets/cache/' => false,
dirname(__FILE__) .'/core/cache/' => true,
dirname(__FILE__) .'/core/packages/' => true,
dirname(__FILE__) .'/media/site_backup.zip' => null,
dirname(__FILE__) .'/rbak.php' => null
);
$allowed_files = array(
'.gitignore',
);
$source = './';
$destination = './media/site_backup.zip';
// ----------------------------------------------------------------------------
if ($windows) {
$ex = $exclude;
$exclude = array();
foreach ($ex as $key => $value) {
$exclude[str_replace('/', '\\', $key)] = $value;
}
}
$a = Zip($source, $destination);
echo 'END MEMORY: ', convert(memory_get_usage (true)) ,'<br /><br />';
echo 'Working time: '. (microtime(true) - $starttime) .' s<br /><br />', "\n\n";
echo 'Destination: ', $destination , '<br /><br />';
if($a) { print 'OK'; }
else { print 'Failed ...'; }
// ----------------------------------------------------------------------------
function Zip($source, $destination)
{
if (extension_loaded('zip') === true)
{
if (file_exists($source) === true)
{
$zip = new ZipArchive();
if ($zip->open($destination, ZIPARCHIVE::OVERWRITE) === 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_excluded($file)) {
continue;
}
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;
}
function is_excluded($path) {
global $exclude, $allowed_files;
if (in_array(basename($path), $allowed_files)) {
return false;
}
foreach ($exclude as $url => $value) {
if (strpos($path, $url) !== false) {
return true;
}
}
return false;
}
function convert($size)
{
$unit=array('b','kb','mb','gb','tb','pb');
return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment