Posted on 31-08-2009
Filed Under (mysql, php) by Elfin Ruler

MySQL Daily Backup Script

Create a script file :- /usr/local/sbin/mysqlbackup.sh

Add below code :-

#!/bin/sh

# Current date
datum=`/bin/date +%Y%m%d-%H`

/usr/bin/mysqldump –user=root –password=RootPassword  \
Database_name | gzip -9 > /home/sqlbackup/${filename}-${datum}.sql.gz
for file in “$( /usr/bin/find /home/sqlbackup -type f -mtime +5 )”
do
/bin/rm -f $file
done

exit 0

crontab -e

Add Below Line :-

0 0 * * * /usr/local/sbin/mysqlbackup.sh &> /dev/null ( For Backup Daily)

Back up with php code

1.create backup folder at your root folder

2.copy paste your create my sql connection code from -Here

3. copy paste below code

<?php

$filename=backup_tables(HOST,USER,PASSWORD,NAME);
$files_to_zip = array(‘backup/’.$filename);
$result = create_zip($files_to_zip,’backup/my-archive.zip’);

/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = ‘*’)
{

$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);

//get all of the tables
if($tables == ‘*’)
{
$tables = array();
$result = mysql_query(‘SHOW TABLES’);
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(‘,’,$tables);
}

//cycle through
foreach($tables as $table)
{
$result = mysql_query(‘SELECT * FROM ‘.$table);
$num_fields = mysql_num_fields($result);

$return.= ‘DROP TABLE ‘.$table.’;';
$row2 = mysql_fetch_row(mysql_query(‘SHOW CREATE TABLE ‘.$table));
$return.= “\n\n”.$row2[1].”;\n\n”;

for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= ‘INSERT INTO ‘.$table.’ VALUES(‘;
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace(“\n”,”\\n”,$row[$j]);
if (isset($row[$j])) { $return.= ‘”‘.$row[$j].’”‘ ; } else { $return.= ‘”"‘; }
if ($j<($num_fields-1)) { $return.= ‘,’; }
}
$return.= “);\n”;
}
}
$return.=”\n\n\n”;
}

//save file
$filename=’db-backup-’.date(‘Y-m-d-H-i-s’,time()).’-’.(md5(implode(‘,’,$tables))).’.sql’;
$handle = fopen(‘backup/’.$filename,’w+’);
fwrite($handle,$return);
fclose($handle);
return $filename;
}

/* creates a compressed zip file */
function create_zip($files = array(),$destination = ”,$overwrite = false) {
//if the zip file already exists and overwrite is false, return false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in…
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files…
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
//debug
//echo ‘The zip archive contains ‘,$zip->numFiles,’ files with a status of ‘,$zip->status;

//close the zip — done!
$zip->close();

//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}

?>

(1) Comment    Read More   
BACK-LINKS and PAGE-RANK WEB DIRECTORY

Free targeted website traffic - www.ad-traffic.net