RE: backing up a database

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Here is a script that I found that has been working well for me.  I set up a
cron job to run this file every night just after midnight.  It e-mails a
gzipped sql dump file as an attachment to the specified e-mail address.

Right now the database this is backing up is not very big. I'm not sure how
scalable it is (probably not very).

Suggestions/improvements are welcome.

<?php
	set_time_limit(0);
	ini_set("memory_limit", "500M");
	
	$dbhost = 'localhost';
	$dbuser = 'username';
	$dbpass = 'password';
	$dbname = 'database';
	
	$backupDir = '/backups/';
	$backupFileName = $dbname . date("Y-m-d-H-i-s") . '.sql.gz';
	$backupFile = $backupDir . $backupFileName;
	
	$command = "mysqldump -u $dbuser -p$dbpass $dbname | gzip >
$backupFile";

	system($command);
	
	
	
	$email_from = "data@xxxxxxxxxxxx"; // Who the email is from  
	$email_subject = "MySQL Backup ".date("m/d/Y", time()); // The
Subject of the email  
	$email_txt = "Your file is attached."; // Message that the email has
in it  
	
	$email_to = "me@xxxxxxxxxxxx"; // Who the email is to  
	
	$headers = "From: ".$email_from;  
	
	$file = fopen($backupFile,'rb');  
	$data = fread($file,filesize($backupFile));  
	fclose($file);  
	
	$semi_rand = md5(time());  
	$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";  
		
	$headers .= "\nMIME-Version: 1.0\n" .  
				"Content-Type: multipart/mixed;\n" .  
				" boundary=\"{$mime_boundary}\"";  
	
	$email_message .= "This is a multi-part message in MIME format.\n\n"
.  
					"--{$mime_boundary}\n" .  
					"Content-Type:text/html;
charset=\"iso-8859-1\"\n" .  
				   "Content-Transfer-Encoding: 7bit\n\n" .  
	$email_txt . "\n\n";  
	
	$data = chunk_split(base64_encode($data));  
	
	$email_message .= "--{$mime_boundary}\n" .  
					  "Content-Type:
application/octet-stream;\n" .  
					  " name=\"{$backupFileName}\"\n" .

					  "Content-Transfer-Encoding:
base64\n\n" .  
					 $data . "\n\n" .  
					  "--{$mime_boundary}--\n";  
	
	$ok = @mail($email_to, $email_subject, $email_message, $headers);  
	
	if($ok) {  
	echo date("Y-m-d H:i:s") . "  Operation completed.\n";  
	} else {  
	die(date("Y-m-d H:i:s") . "  Operation failed - The e-mail could not
be sent.\n");  
	}  
?>

> -----Original Message-----
> From: Edward Kay [mailto:edward@xxxxxxxxxx]
> Sent: Wednesday, November 22, 2006 5:25 AM
> To: PHP General List
> Subject: RE:  backing up a database
> 
> If you're using MySQL, set up a cron job/scheduled task to call mysqldump
> and save the output somewhere. Other DBMSs will likely have similar tools.
> 
> Edward
> 
> > -----Original Message-----
> > From: Ross [mailto:ross@xxxxxxxxxxxxx]
> > Sent: 22 November 2006 09:54
> > To: php-general@xxxxxxxxxxxxx
> > Subject:  backing up a database
> >
> >
> > I have a database and it needs to get backed up on a daily basis.
> > Is there a
> > class that allows me to create a backup and then save it as a
> > .sql or excel
> > or both to a folder of her choice?
> >
> >
> > R.
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
> >
> >
> 
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux