Jump to content

Backup Script


Knouen

Recommended Posts

Again from that tutorial: http://pastebin.com/m7bbfb985

 

Ok, next question, while someone is hopefully checking the previous... If I want to download:

 

index.php

/admin/index.php

 

Two files with the same name, in different directories, and this script supposedly walks through the directories grabbing all specified files for archiving and downloading, how do I add the names to the list at line 27 where it has:

 

$list = array('download.php', '...');

 

?

 

Not sure how to include all the files from different directories.

Link to comment
Share on other sites

The code is on the linked Pastebin pages.  But if it helps, I'll add it here instead.

 

 

 

<?php

include_once("Archive/Zip.php");
include_once("Archive/Tar.php");
/* Include database connection. */
include_once('include/dbobj.inc.php');


// Name of the archive
$name = (!empty($_GET['name'])) ? $name = $_GET['name'] : $name = 'script';

// Just some protection
$name = str_replace("\\\\", '', $name);
$name = str_replace("\\", '', $name);
$name = str_replace('/', '', $name);

// Type specified?
$type = (!empty($_GET['type'])) ? $type = $_GET['type'] : $type = 'zip';
if ($type != 'zip' AND $type != 'tar') { $type = 'zip'; }

/* Perform MySQLdump. */
$command = "mysqldump --opt -u myusername -p mydb > mydb.bak.dump";
system($command);


// Add this script to the list
$list = array('download.php','mydb.bak.dump');

// Create the archive
$data = create_archive ($name, $list, $type);

if ($type == 'zip') {
$mimetype = 'application/zip';
$name .= '.zip';
} else {
$mimetype = 'application/x-tar-gz';
$name .= '.tar.gz';
}

// Send file
force_download ($data, $name, $mimetype);
die();

function create_archive ($name, $list, $type='zip') {
// Get temporary directory
if (!empty($_ENV['TMP'])) {
	$tempdir = $_ENV['TMP'];
} elseif (!empty($_ENV['TMPDIR'])) {
	$tempdir = $_ENV['TMPDIR'];
} elseif (!empty($_ENV['TEMP'])) {
	$tempdir = $_ENV['TEMP'];
} else {
	$tempdir = dirname(tempnam('', 'na'));
}

if (empty($tempdir)) { die ('No temporary directory'); }

// Make sure trailing slash is there
$tempdir = rtrim($tempdir, '/');
$tempdir .= '/';

// Make sure temporary directory is writable
if (is_writable($tempdir) == false) {
	die ('Temporary directory isn\'t writable');
}

// Create temp name	for our own directory
$dir = tempnam($tempdir, 'temp');

// Make sure another file or directory doesn't already exist with this name
@unlink($dir);
@rmdir($dir);

// Create directory
mkdir($dir);
$dir .= '/';

// Copy files & directories so relative paths still work
foreach ($list as $item) {
	if (is_file($item)) {
		// Copy file
		copy ($item, $dir . $item);
	} elseif (is_dir ($item)) {
		// Copy directory
		move_directory ($item, $dir . $item, true);
	} else {
		// Invalid file, just ignore
		continue;
	}
}

// Change current working directory, so that the zip file gets created in the temp dir
chdir($dir);

// Create zip or tar.gz file
if ($type == 'zip') {
	$name .= '.zip';
	$zipfile = New Archive_Zip($name);
	$zipfile->create($list);
} else {
	$name .= '.tar.gz';
	$tarfile = New Archive_Tar($name, 'gz');
	$tarfile->create($list);
}

// Get file data
$data = implode('', file($name));
return $data;
}

function move_directory($source, $dest, $copy=false) {
// Standardize paths
$source = str_replace('\\\\', '/', $source);
$source = str_replace('\\', '/', $source);

$dest = str_replace('\\\\', '/', $dest);
$dest = str_replace('\\', '/', $dest);

// Remove trailing slashes
$source = rtrim($source, '/');
$dest = rtrim($dest, '/');

// Add trailing slashes
$source .= '/';
$dest .= '/';

// Source doesn't exist?
if (file_exists($source) == false) { return false; }

// Try to create destination
@mkdir($dest);

// Destination doesn't exist?
if (file_exists($dest) == false) { return false; }

// Copy all files
$d = dir($source);

while (false !== ($entry = $d->read())) {
	if ($entry == '.' OR $entry == '..') continue;
	$file = $d->path . $entry;

	// Is a sub dir?
	if (is_dir($file)) {
		// Try to create sub dir
		$subdir = $dest . $entry . '/';
		$result = @mkdir($subdir);
		if ($result == false) { continue; }

		// Move files in sub directory
		move_directory($file, $subdir, $copy);
		continue;
	}

	// Copy file
	copy($file, $dest . $entry);

	// Remove old file?
	if ($copy == false) {
		// Moving the directory, so remove original
		@unlink($file);
	}
}	

if ($copy == false) {
	@rmdir($source);
}

return true;
}

function force_download ($data, $name, $mimetype='', $filesize=false) {
    // File size not set?
    if ($filesize == false OR !is_numeric($filesize)) {
        $filesize = strlen($data);
    }

    // Mimetype not set?
    if (empty($mimetype)) {
        $mimetype = 'application/octet-stream';
    }

    // Make sure there's not anything else left
    ob_clean_all();

    // Start sending headers
    header("Pragma: public"); // required
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false); // required for certain browsers
    header("Content-Transfer-Encoding: binary");
    header("Content-Type: " . $mimetype);
    header("Content-Length: " . $filesize);
    header("Content-Disposition: attachment; filename=\"" . $name . "\";" );

    // Send data
    echo $data;
    die();
}

function ob_clean_all () {
    $ob_active = ob_get_length () !== false;
    while($ob_active) {
        ob_end_clean();
        $ob_active = ob_get_length () !== false;
    }

    return true;
}

/* Close database object. */
ob_flush();

?>

 

Link to comment
Share on other sites

The part where things are added to the array of files to be included is close to the top:

// Add this script to the list
$list = array('download.php','mydb.bak.dump');

 

This part is not actually from that tutorial:

/* Perform MySQLdump. */
$command = "mysqldump --opt -u myusername -p mydb > mydb.bak.dump";
system($command);

 

I just thought I'd try to add a DB backup also.  Anyway, even without those added lines, I'm unsure how add all the files and directories properly.

 

Link to comment
Share on other sites

Ok, I've actually done most of it now.  However, there remain a few things to sort out.

 

One of the tables is exceptionally large (several GBs), the backup script reaches a timeout when trying to mysqldump the table. Can you suggest any way around that please? Is there perhaps some setting on the server which can be sued to allow the script to continue running for a long time?

 

Link to comment
Share on other sites

Adjusted so it automatically generates the list of files in a directory for adding to the zip:

<?php

include_once("Archive/Zip.php");
include_once("Archive/Tar.php");
/* Include database connection. */
include_once('include/dbobj.inc.php');


// Name of the archive
$name = (!empty($_GET['name'])) ? $name = $_GET['name'] : $name = 'base';

// Just some protection
$name = str_replace("\\\\", '', $name);
$name = str_replace("\\", '', $name);
$name = str_replace('/', '', $name);

// Type specified?
$type = (!empty($_GET['type'])) ? $type = $_GET['type'] : $type = 'zip';
if ($type != 'zip' AND $type != 'tar') { $type = 'zip'; }

// Add this script to the list
$list = listpwdfiles(getcwd());

// Create the archive
$data = create_archive ($name, $list, $type);

if ($type == 'zip') {
$mimetype = 'application/zip';
$name .= '.zip';
} else {
$mimetype = 'application/x-tar-gz';
$name .= '.tar.gz';
}

// Send file
force_download ($data, $name, $mimetype);
die();

function create_archive ($name, $list, $type='zip') {
// Get temporary directory
if (!empty($_ENV['TMP'])) {
	$tempdir = $_ENV['TMP'];
} elseif (!empty($_ENV['TMPDIR'])) {
	$tempdir = $_ENV['TMPDIR'];
} elseif (!empty($_ENV['TEMP'])) {
	$tempdir = $_ENV['TEMP'];
} else {
	$tempdir = dirname(tempnam('', 'na'));
}

if (empty($tempdir)) { die ('No temporary directory'); }

// Make sure trailing slash is there
$tempdir = rtrim($tempdir, '/');
$tempdir .= '/';

// Make sure temporary directory is writable
if (is_writable($tempdir) == false) {
	die ('Temporary directory isn\'t writable');
}

// Create temp name	for our own directory
$dir = tempnam($tempdir, 'temp');

// Make sure another file or directory doesn't already exist with this name
@unlink($dir);
@rmdir($dir);

// Create directory
mkdir($dir);
$dir .= '/';

// Copy files & directories so relative paths still work
foreach ($list as $item) {
	if (is_file($item)) {
		// Copy file
		copy ($item, $dir . $item);
	} elseif (is_dir ($item)) {
		// Copy directory
		move_directory ($item, $dir . $item, true);
	} else {
		// Invalid file, just ignore
		continue;
	}
}

// Change current working directory, so that the zip file gets created in the temp dir
chdir($dir);

// Create zip or tar.gz file
if ($type == 'zip') {
	$name .= '.zip';
	$zipfile = New Archive_Zip($name);
	$zipfile->create($list);
} else {
	$name .= '.tar.gz';
	$tarfile = New Archive_Tar($name, 'gz');
	$tarfile->create($list);
}

// Get file data
$data = implode('', file($name));
return $data;
}

function move_directory($source, $dest, $copy=false) {
// Standardize paths
$source = str_replace('\\\\', '/', $source);
$source = str_replace('\\', '/', $source);

$dest = str_replace('\\\\', '/', $dest);
$dest = str_replace('\\', '/', $dest);

// Remove trailing slashes
$source = rtrim($source, '/');
$dest = rtrim($dest, '/');

// Add trailing slashes
$source .= '/';
$dest .= '/';

// Source doesn't exist?
if (file_exists($source) == false) { return false; }

// Try to create destination
@mkdir($dest);

// Destination doesn't exist?
if (file_exists($dest) == false) { return false; }

// Copy all files
$d = dir($source);

while (false !== ($entry = $d->read())) {
	if ($entry == '.' OR $entry == '..') continue;
	$file = $d->path . $entry;

	// Is a sub dir?
	if (is_dir($file)) {
		// Try to create sub dir
		$subdir = $dest . $entry . '/';
		$result = @mkdir($subdir);
		if ($result == false) { continue; }

		// Move files in sub directory
		move_directory($file, $subdir, $copy);
		continue;
	}

	// Copy file
	copy($file, $dest . $entry);

	// Remove old file?
	if ($copy == false) {
		// Moving the directory, so remove original
		@unlink($file);
	}
}	

if ($copy == false) {
	@rmdir($source);
}

return true;
}

function force_download ($data, $name, $mimetype='', $filesize=false) {
    // File size not set?
    if ($filesize == false OR !is_numeric($filesize)) {
        $filesize = strlen($data);
    }

    // Mimetype not set?
    if (empty($mimetype)) {
        $mimetype = 'application/octet-stream';
    }

    // Make sure there's not anything else left
    ob_clean_all();

    // Start sending headers
    header("Pragma: public"); // required
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false); // required for certain browsers
    header("Content-Transfer-Encoding: binary");
    header("Content-Type: " . $mimetype);
    header("Content-Length: " . $filesize);
    header("Content-Disposition: attachment; filename=\"" . $name . "\";" );

    // Send data
    echo $data;
    die();
}

function ob_clean_all () {
    $ob_active = ob_get_length () !== false;
    while($ob_active) {
        ob_end_clean();
        $ob_active = ob_get_length () !== false;
    }

    return true;
}

function listpwdfiles($directory){
$list = array();
$handler = opendir($directory);
while($file = readdir($handler)){
  if ($file != '.' && $file != '..')
   $list[] = $file;
  }
closedir($handler);
return $list;
}

/* Close database object. */
ob_flush();

?>

However, that still only does the one directory, and won't walk into other folders to do the whole site.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.