Knouen Posted September 16, 2007 Share Posted September 16, 2007 From this page, I have this. It isn't working, and I don't know why. It downloads the zip file but says the archive is damaged, and it has zero bytes. And this for backing up the database. Also not working, also no idea why. Any ideas? Kind of desperate for assistance here. Quote Link to comment https://forums.phpfreaks.com/topic/69521-backup-script/ Share on other sites More sharing options...
Knouen Posted September 17, 2007 Author Share Posted September 17, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/69521-backup-script/#findComment-349974 Share on other sites More sharing options...
Knouen Posted September 19, 2007 Author Share Posted September 19, 2007 I really just need to know how to add the files and directories to that array properly. Quote Link to comment https://forums.phpfreaks.com/topic/69521-backup-script/#findComment-350843 Share on other sites More sharing options...
d22552000 Posted September 19, 2007 Share Posted September 19, 2007 you posted no code at all and thats why no one replied. Quote Link to comment https://forums.phpfreaks.com/topic/69521-backup-script/#findComment-350857 Share on other sites More sharing options...
Knouen Posted September 19, 2007 Author Share Posted September 19, 2007 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/69521-backup-script/#findComment-351046 Share on other sites More sharing options...
Knouen Posted September 19, 2007 Author Share Posted September 19, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/69521-backup-script/#findComment-351050 Share on other sites More sharing options...
Knouen Posted September 19, 2007 Author Share Posted September 19, 2007 Any ideas at all? Quote Link to comment https://forums.phpfreaks.com/topic/69521-backup-script/#findComment-351491 Share on other sites More sharing options...
Knouen Posted September 20, 2007 Author Share Posted September 20, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/69521-backup-script/#findComment-351601 Share on other sites More sharing options...
Knouen Posted September 22, 2007 Author Share Posted September 22, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/69521-backup-script/#findComment-352745 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.