Re: MySQL Dump
| From: | Matt Palermo | Date: | Fri, 23 Apr 2004 20:27:45 +0000 |
| Subject: | Re: MySQL Dump | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-184466@lists.php.net to get a copy of this message | ||
Will this work on both a Windows and Unix server?
"Michal Migurski" <mike@teczno.com> wrote in message
news:Pine.BSF.4.58.0404221811080.37146@or.pair.com...
> > Is there an easy way to do a mysql dump routine written in php? I
> > basically want to write a function to backup a database and have it
> > create all the neccessary structures and data in the dump. I know
> > phpMyAdmin will do this for me, but I want to write a function to do
> > this incase the server doesn't have phpMyAdmin installed. Any ideas?
>
> Why not use the existing mysqldump command-line utility?
>
> function mysql_dump($db, $user, $pass)
> {
> return shell_exec("/path/to/mysqldump
> --user={$user} --password={$pass}
> --complete-insert --add-drop-table
> {$db}");
> }
>
> Or, skip PHP entirely and place a call to mysqldump with a suitable
> select-privilege-only username & password in a cron job, if you're on a
> unix machine. I use the following shellscript to do periodic backups to
> RCS repositories in /usr/local/var/backups:
>
> #!/bin/sh
>
> if [ $# -ne 3 ]; then
> echo 'Usage:';
> echo ' mysql-backup.sh <db-user> <db-password> <db-name>';
> exit;
> fi;
>
> USER=$1;
> PASS=$2;
> DB=$3;
> MYSQLDUMP=/usr/local/bin/mysqldump;
>
> BACKUP_DIR=/usr/local/var/backups;
> SQL_FILE=$DB.mysql;
> RCS_FILE=$SQL_FILE,v;
>
> cd $BACKUP_DIR;
>
$MYSQLDUMP --user=$USER --password=$PASS --complete-insert --add-drop-table
$DB > $SQL_FILE;
>
> #### the remaining lines can be ommitted if RCS isn't needed ####
>
> if [ ! -f $RCS_FILE ]; then
> echo "import" | rcs -q -i $SQL_FILE;
> else
> rcs -q -l $RCS_FILE;
> fi;
>
> echo "mysql_backup.sh" | ci -q $SQL_FILE;
>
> ---------------------------------------------------------------------
> michal migurski- contact info and pgp key:
> sf/ca http://mike.teczno.com/contact.html