Topic: automated database backups

hopefully someone here will find this useful. this dumps a database and emails it to you.

i created a gmail account. and i have a cronjob that says:

45 0 * * * php bk.php

bk.php:

<?php

 ********************************************************************************
 * Copyright (c) 2007 Jonathan Lucas Reddinger <lucas@wingedleopard.net>        *
 *                                                                              *
 * Permission to use, copy, modify, and distribute this software for any        *
 * purpose with or without fee is hereby granted, provided that the above       *
 * copyright notice and this permission notice appear in all copies.            *
 *                                                                              *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES     *
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF             *
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR      *
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES       *
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN        *
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF      *
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.               *
 ********************************************************************************

$filename = "ttf.sql";
$attachname = "ttf.sql.bz2";

system("mysqldump --skip-opt --add-drop-table --add-locks --create-options --disable-keys --extended-insert --quick --set-charset --user=ttfbackup --password=xxxx ttf > ".$filename);

system("bzip2 ".$filename);

$file = fopen($attachname, 'rb'); 
$data = fread($file, filesize($attachname)); 
fclose($file);

system("rm ".$attachname);

$data = chunk_split(base64_encode($data)); 

$boundary = md5(time());

$to_addr = "ttfbackup@gmail.com";

$subject = "ttf database backup for ".date("Y-m-d H:i:s e");

$headers = "From: violet@thinktankforums.com\n".
           "MIME-Version: 1.0\n".
           "Content-Type: multipart/mixed; boundary=\"{$boundary}\";"; 

$message = "Content-Transfer-Encoding: 7bit\n".
           "This is a multi-part message in MIME format.\n".
           "--".$boundary."\n".
           "Content-Type: text/plain; charset=\"iso-8859-1\"\n".
           "Content-Transfer-Encoding: 7bit\n".
           "hello,\n\nthis is your daily ttf database backup.\n\n".
           "please find the data attached.\n\n".
           "yours,\nviolet\n".
           "--".$boundary."\n".
           "Content-Type: application/octet-stream; name=\"sql.bz2\";\n".
           "Content-Transfer-Encoding: base64\n".
           "Content-Disposition: attachment\n\n".
           $data."\n\n".
           "--".$boundary."\n";

@mail($to_addr, $subject, $message, $headers); 

?>

comments?

p.s. i don't know how google feels about this...

Last edited by lucas (2009-02-28 22:44:25)

Re: automated database backups

You may want to change

$filename = "ttf.sql";
$attachname = "ttf.sql.bz2";

to

$filename = '/tmp/ttf.sql';
$attachname = '/tmp/ttf.sql.bz2';

PHP has a function to get the system's temp dir to (get_system_tmp() or something), which is even better

Trust me, I know what I'm doing.

Re: automated database backups

oh cool.. thanks!