$table) {
// Rows in this chunk less than $pp value and Still Rows to Count (or no rows)
if($r < $pp and ($tables[$key][progress] < $tables[$key][rows] or ($tables[$key][rows] == 0 and $tables[$key][progress] <= 0))) {
// 'Create Table' Code (if not yet done)
if($tables[$key][create] == 0) {
$qry = f(q("SHOW CREATE TABLE ".$tables[$key][name],MY_D,false));
$text .= str_replace('CREATE TABLE','CREATE TABLE IF NOT EXISTS',$qry[1]).";\r\n\r\n";
$tables[$key][create] = 1;
}
// Rows?
if($tables[$key][rows] > 0) {
// Get Rows (With Primary Key)
if($tables[$key][primary]) {
$end = $tables[$key][progress] + $pp;
$query = "SELECT * FROM ".$tables[$key][name]." WHERE ".$tables[$key][primary]." > ".$tables[$key][progress]." AND ".$tables[$key][primary]." <= ".$end." ORDER BY ".$tables[$key][primary]." ASC";
}
// Get Rows (Without Primary Key)
else $query = "SELECT * FROM ".$tables[$key][name]." LIMIT ".$tables[$key][progress].",$pp";
// Query Rows
//print "$query
";
$q = 0;
$sql = q($query,MY_D,false);
if(n($sql)) {
// Loop Through Rows
$id = $tables[$key][progress];
while($qry = f($sql,MYSQL_ASSOC)) {
// Columns
if($q == 0) {
$text .= "INSERT IGNORE INTO ".$tables[$key][name]." (";
$z = 0;
foreach($qry as $column => $val) {
if($z > 0) $text .= ",";
$text .= $column;
$z++;
}
$text .= ") VALUES ";
}
// Values
if($q > 0) $text .= ",";
$text .= "(";
$z = 0;
foreach($qry as $val) {
if($z > 0) $text .= ",";
$text .= "'".$val."'";
$z++;
}
$text .= ")";
// Query Row Counter
$q++;
// Totoal Row Counter
$r++;
// Store Last Primary ID value
if($tables[$key][primary]) $id = $qry[$tables[$key][primary]];
}
$text .= ";\r\n";
// No Primary ID, Store Last Row #
if(!$tables[$key][primary]) {
$id = $tables[$key][progress] + $pp;
if($id > $tables[$key][rows]) $id = $tables[$key][rows];
}
// Print Progress
print "Rows ".$tables[$key][progress]."-".($id)." of table ".$tables[$key][name]." were successfully backed up.
";
// Store Progress
$tables[$key][progress] = $id;
// Rows in Query < $pp, but still Rows in Table (set row count to max rows)
if($tables[$key][rows] > $tables[$key][progress]) $r = $pp;
}
// No Rows in Query, Place Progress Marker at end of No Rows Selection
else $tables[$key][progress] += $pp;
// Clear Query Memory
mysql_free_result($sql);
// Finished Table
if($tables[$key][progress] >= $tables[$key][rows]) {
$text .= "\r\n\r\n";
print "Table ".$tables[$key][name]." has been sucessfully backed up.
";
$t++;
}
}
// No Rows in Table
else {
$tables[$key][progress] = 1;
print "Table ".$tables[$key][name]." has been sucessfully backed up.
";
$t++;
}
}
}
// Save Backup to File
if($f = fopen($file, 'a')) fwrite($f, $text);
fclose($f);
// File Size
clearstatcache();
$size = filesize($file);
print "
Backed Up Tables - $t
Remaining Tables - ".(count($tables) - $t)."
";
// No Tables Left to Store - or - Filesize fast approaching Max File Size (need to create new file)
if($t >= count($tables) or $size >= $max_file_size) $status = 2;
}
/********************** Compress ************************/
/* Compresses .sql file into .gz file */
/********************************************************/
if($status == 2) {
$dest = $file.'.gz';
$mode = 'wb1';
if($fp_out = gzopen($dest,$mode)) {
if($fp_in = fopen($file,'rb')) {
while(!feof($fp_in)) gzwrite($fp_out,fread($fp_in,4096));
fclose($fp_in);
}
else print "Error compressing file.
";
gzclose($fp_out);
}
else print "Error creating compressed file.
";
// Backup Complete
if($t >= count($tables) or $t > $max_tables) {
// Calcualte Time
$time = time() - $time_start;
if($time < 60) $time.= " seconds";
else {
$min = floor($time / 60);
$sec = $time - ($min * 60);
$time = $min.":".str_pad($sec,2,0,STR_PAD_LEFT);
}
print "
Backup Complete ($time) - $c file".($c == 1 ? '' : 's')."
";
// Stop Looping
$c = 9999999;
}
}
}
}
/******************** Clear Backups *********************/
/* Deletes backups than $bak days old */
/********************************************************/
$past = time() - (86400 * $life);
if($f = opendir($path)) {
while(false !== ($file = readdir($f))) {
if(strlen($file) > 2) {
preg_match('/([0-9]{10})/',$file,$time);
if($time[1]) {
if($time[1] < $past) {
print "Deleted Backup - $file (".date('m/d/Y',$time[1]).")
";
unlink($path.$file);
}
else print "Keeping Backup - $file (".date('m/d/Y',$time[1]).")
";
}
}
}
}
?>