#!/bin/bashpath=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbinexport path# Database user name dbuser= ' root ' # Database with password dbpasswd= ' ******** ' need to back up the database, multiple databases with spaces separated dbname= ' Xiaohuai xiaohuai2 ' #备份时间backtime = ' Date +%y%m%d%h%m%s ' # Log backup path logpath= '/home/mysqlbak ' #数据备份路径datapath = '/home/mysqlbak ' #日志记录头部echo ' "Backup Time is ${backtime}, backup database table ${dbname} start" >> ${logpath}/log.log# official Backup database for table in $dbname; Dosource= ' Mysqldump-u ${dbuser}-p${dbpasswd} ${table}> ${logpath}/${backtime}.sql ' 2>> ${logpath}/ Mysqllog.log; #备份成功以下操作if ["$?" = = 0];thencd $datapath # to conserve hard disk space, compress the database into tar jcf ${table}${backtime}.tar.bz2 ${backtime}. SQL >/dev/null# Delete the original file, leaving only the compressed file rm-f ${datapath}/${backtime}.sql# deleted seven days ago backup, that is, save only 7 days backup find $datapath-name "*. tar.bz2 "-type f-mtime +7-exec rm-rf {} \; >/dev/null 2>&1echo "database table ${dbname} backup succeeded!!" >> ${logpath}/mysqllog.logelse# Backup fails the following Operation Echo database table ${ DBNAME} Backup Failed! ">> ${logpath}/mysqllog.logfidone
CentOS automatically backs up MySQL database every day