用php来备份mysql

[shell]
#将所有备份文件备份到指定的目录,如/backup/mysql_data_backup
mkdir /backup/mysql_data_backup -p
cd /backup/mysql_data_backup
wget http://www.4wei.cn/files/2013/08/backmysql.txt -O backmysql.php
chmod +x backmysql.php
crontab -e
[/shell]

添加一行任务
0 13 * * * /usr/bin/php /backup/mysql_data_backup/backmysql.php

表示在每天晚上0点13分用php执行备份命令

[shell]
#!/usr/bin/php
< ?php

//产生保存目录
$path = dirname(__FILE__) . ‘/’ .date("Ym");
$filename = sprintf("%s/%s.sql.gz", $path, date("YmdHis"));

if(!is_dir($path))
mkdir($path);

//导出并压缩所有数据库
$cmd = sprintf("/usr/bin/mysqldump -uroot -ppassword –all-databases | /bin/gzip > %s", $filename);
echo "backuping…\n";
`$cmd`;
echo "backup done.\n";
[/shell]