自动备份mysql数据库脚本,适用小量数据

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
dbuser='root'
dbpasswd='xx'
dbname='db1 db2'
backtime=`date +%Y%m%d%H%M%S`
logpath='/root/mysql_backup/log'
datapath='/root/mysql_backup'
echo "backup time:${backtime},database:${dbname} touching..." >> ${logpath}/mysqllog.log
for table in $dbname; do
source=`/usr/local/mysql/bin/mysqldump -u${dbuser} -p${dbpasswd} ${table}> ${datapath}/${table}${backtime}.sql` 2>> ${logpath}/mysqllog.log;
if [ "$?" == 0 ];then
        cd ${datapath}
        tar czf ${table}${backtime}.tar.gz ${table}${backtime}.sql > /dev/null
        mv ${table}${backtime}.tar.gz backup/
        cd ${datapath}/backup
        svn add ${table}${backtime}.tar.gz
        echo "database ${dbname} backup success!!" >> ${logpath}/mysqllog.log
else
        echo "database ${dbname} backup error!!" >> ${logpath}/mysqllog.log
fi
done
cd ${datapath}/backup
svn commit -m "mysql auto backup"


你可能感兴趣的:(自动备份mysql数据库脚本,适用小量数据)