用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]
唉 这是何必呢,
#!/bin/sh
STARTTIME=`date +%Y-%m-%d_%H:%M:%S`
BACKUPDIR=/root/backup/
LOGFILE=${BACKUPDIR}/mysql.backup.log
MYSQLDUMP=mysqldump
MYSQLUSER=root
MYSQLPASS=密码
DATABASENAME=数据库名
DATE=`date +%Y%m%d`
DATETIME=`date +%Y-%m-%d_%H:%M:%S`
BACKUPDIR=${BACKUPDIR}/${DATE}/
BACKUPFILE=${DATETIME}
[ -d ${BACKUPDIR} ] || mkdir -p ${BACKUPDIR}
${MYSQLDUMP} –opt -u ${MYSQLUSER} -p${MYSQLPASS} ${DATABASENAME} > ${BACKUPDIR}/${BACKUPFILE}
ENDTIME=`date +%Y-%m-%d\ %H:%M:%S`
echo “${STARTTIME}-${ENDTIME}” >> ${LOGFILE}
echo “${STARTTIME}-${ENDTIME}”