Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

mysql backup via php?

0 views
Skip to first unread message

Mosher

unread,
May 3, 2005, 8:00:54 PM5/3/05
to
Hi all, I am wanting to automatically do dump of a mysql database nightly
using cron and some kind of script that will email the whole db structure to
me (as an attachment or as plain text in my email) - including the data in
the db. So, if the db was ever lost, I could go to my email, find the dump
and copy and paste it into the mysql db again.

Does anyone know how to do this in PHP or of a premade (and free) script out
there, preferably written in PHP?

Thanks,

Mosher


Chris Hope

unread,
May 3, 2005, 8:06:11 PM5/3/05
to
Mosher wrote:

Dump it using mysqldump into a text file using one of the system() or
exec() type function calls, then either attach the resulting text file
to your email or make it the body of the message.

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com

Good Man

unread,
May 3, 2005, 8:29:17 PM5/3/05
to
Chris Hope <blac...@electrictoolbox.com> wrote in news:d593li$i6f$1
@lust.ihug.co.nz:

.... though if your data is really important, perhaps regular e-mail should
be avoided, lest anyone snoop in on it...

Chuck Anderson

unread,
May 3, 2005, 8:38:09 PM5/3/05
to
Chris Hope wrote:

I'm no great Php expert, probably not perfect Php, but here's the
function I use in my script:

////////////////////////////////////////////
function create_db_backup ($path, $tables = "")
{
$filename=$DBNAME . "_backup" . "_" . date("n-j-y_H-i-s") .
".sql";

system("mysqldump --add-drop-table -u $USRNAME -p $PASSWRD $DBNAME
$tables > $path/$filename", $result);

system ("gzip $path/$filename", $result);

$gzname = $filename . ".gz";

return $gzname;
}

$path is the directory I save backups to.

$tables is optional. If empty, mysqldump will backup all tables,
otherwise it's a list of tables to backup (separated by a space, e.g."
table1 table2 table3" - no quotes).

I use --add-drop-table because it seems like the simplest sql file to
use for a restore.

Finally, I gzip it to make it smaller.

Now all you have to do is mail it to yourself. I use Phpmailer.

--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************

Chris Hope

unread,
May 3, 2005, 8:40:55 PM5/3/05
to
Good Man wrote:

Especially if it contains sensitive data. If you have ssh or sftp access
to the server you can always download the dump file via sftp which
makes it much safer. I manage my own servers and all the databases are
dumped to text files on a daily basis and rsync'd to my local machine
on a daily basis via ssh.

Message has been deleted

Mosher

unread,
May 7, 2005, 9:16:59 AM5/7/05
to
Thanks for the help all - I'll try what you mentioned.

Mosher

"Chuck Anderson" <website...@seemy.sig> wrote in message
news:xtCdncHaEu3...@comcast.com...

0 new messages