1
This code is great for a cron job for your XOOPS System, it will create a backup of your MySQL Database on any Unix machine, I have it running as a cron on my systems. It is only compatible with XOOPS 2.3 and later, you can adjust it for earlier versions.
Place the code in a file called backup.php in your XOOPS_ROOT_PATH/cron as all XOOPS code has to operate within this area and run it going to
http://www.yoursite.com/cron/backup.php
// Call File backup.php in your /public_html/cron path
include('../mainfile.php');
define('lnbr', "n");
set_time_limit(1200);
$path = XOOPS_VAR_PATH.'/backups/'.strtolower(date('l')).'/'.strtolower(date('Y')).'/'.strtolower(date('m')).'/';
if (!is_dir($path))
foreach(explode('/', $path) as $folder) {
$tmp .= '/' . $folder;
mkdir($tmp.'/', 0777);
}
$filename = $path .XOOPS_DB_NAME.'_'.XOOPS_DB_PREFIX.'.sql';
if (file_exists($filename))
unlink ($filename);
if ($file = fopen($filename, 'w+')) {
$dump = mysql_dump(XOOPS_DB_NAME, $file);
fclose($file);
echo 'Backup Successful wrote :: '. $filename.' - Filesize ' . filesize($filename);
} else
echo 'Backup Failed wrote :: '. $filename.' - Filesize ' . filesize($filename);
function mysql_dump($database, $file) {
$query = '';
$tables = @mysql_list_tables($database);
while ($row = @mysql_fetch_row($tables)) { $table_list[] = $row[0]; }
for ($i = 0; $i < @count($table_list); $i++) {
$results = mysql_query('DESCRIBE ' . $database . '.' . $table_list[$i]);
$query = 'DROP TABLE IF EXISTS `' . $database . '.' . $table_list[$i] . '`;' . lnbr;
fwrite($file, $query, strlen($query));
$query = lnbr . 'CREATE TABLE `' . $database . '.' . $table_list[$i] . '` (' . lnbr;
fwrite($file, $query, strlen($query));
$tmp = '';
while ($row = @mysql_fetch_assoc($results)) {
$query .= '`' . $row['Field'] . '` ' . $row['Type'];
if ($row['Null'] != 'YES') { $query .= ' NOT NULL'; }
if ($row['Default'] != '') { $query .= ' DEFAULT '' . $row['Default'] . '''; }
if ($row['Extra']) { $query .= ' ' . strtoupper($row['Extra']); }
if ($row['Key'] == 'PRI') { $tmp = 'primary key(' . $row['Field'] . ')'; }
$query .= ','. lnbr;
}
$query .= $tmp . lnbr . ');' . str_repeat(lnbr, 2);
$results = mysql_query('SELECT * FROM ' . $database . '.' . $table_list[$i]);
while ($row = @mysql_fetch_assoc($results)) {
$query .= 'INSERT INTO `' . $database . '.' . $table_list[$i] .'` (';
$data = Array();
while (list($key, $value) = @each($row)) { $data['keys'][] = $key; $data['values'][] = addslashes($value); }
$query .= join($data['keys'], ', ') . ')' . lnbr . 'VALUES ('' . join($data['values'], '', '') . '');' . lnbr;
}
$query .= str_repeat(lnbr, 2);
fwrite($file, $query, strlen($query));
$query = '';
}
return true;
}
?>