话不多说,直接上码
dbuser='用户名' dbpassword='密码' dbname='库名' backtime=`date +%Y%m%d%H%M%S` logpath='日志路径' datapath='数据存放路径' echo '开始导出数据库' echo '备份时间为'+${backtime}+',备份数据库表'+${dbname}+'开始'>>${logpath}/mysqllog.log for table in $dbname;do source = `mysqldump -u${dbuser} -p${dbpassword} --single-transaction ${table}> ${datapath}/${table}${backtime}.sql` 2>> ${logpath}/mysqllog.log; if ["$?" == 0];then echo '导出数据库...' ${table} echo '开始压缩文件...' cd $datapath tar jcf ${table}${backtime}.tar.bz2 ${table}${backtime}.sql > /dev/null echo '压缩完毕...' echo '删除文件...' rm -f ${datapath}/${table}${backtime}.sql echo '数据库表 ${dbname} 备份成功!!' >> ${logpath}/mysqllog.log echo '备份成功...'
#删除29天前的备份文件 find ${datapath} -mtime +29 -name "*.tar.gz" -exec rm -rf {} \; else #备份失败则进行以下操作 echo '数据库表 ${dbname} 备份失败!!' >> ${logpath}/mysqllog.log echo '备份失败...' fi done
创建一个mysqlbackup.sh的文件,将上述代码放入文件中
启动命令:sh mysqlbackup.sh start
如果想定时进行数据的导出可以参考一些linux的定时任务文章
标签:脚本,...,shell,备份,导出,backtime,echo,table,dbname From: https://www.cnblogs.com/javabianhua/p/16627320.html