背景:需要对Mysql做数据库备份,
解决方案方案:使用到Linux自带的crontab进行排成设定,通过自行编写的一个备份数据库的脚本把数据库进行备份,同时加入到crontab中就可以进行自动备份啦
mysql_local_backup.sh
#!/bin/bash USERNAME=root PASSWORD=Foxconn123$ DATE=`date +%Y-%m-%d` OLDDATE=`date +%Y-%m-%d -d '-30 days'` H=127.0.0.1 P=3307 #MYSQL=/usr/local/mysql/bin/mysql #MYSQLDUMP=/usr/local/mysql/bin/mysqldump #MYSQLADMIN=/usr/local/mysql/bin/mysqladmin BACKDIR=/data/mysql_local_backup [ -d ${BACKDIR} ] || mkdir -p ${BACKDIR} [ -d ${BACKDIR}/${DATE} ] || mkdir ${BACKDIR}/${DATE} [ ! -d ${BACKDIR}/${OLDDATE} ] || rm -rf ${BACKDIR}/${OLDDATE} #MYSQLDUMP_LIST=`/usr/local/mysql/bin/mysql -u root -p'Foxconn123$' -P 3307 -h 127.0.0.1 -e "show databases"| grep -Evi "database|infor|perfor|sys|mysql"` MYSQLDUMP_LIST=`mysql -u root -u${USERNAME} -P${P} -h${H} -p${PASSWORD} -e "show databases"| grep -Evi "database|infor|perfor|sys|mysql"` for DBNAME in ${MYSQLDUMP_LIST} do mysqldump -B -F --master-data=2 --flush-privileges --flush-logs --triggers --routines --events --single-transaction -u${USERNAME} -P${P} -h${H} -p${PASSWORD} ${DBNAME} | gzip > ${BACKDIR}/${DATE}/${DBNAME}-full_backup-${DATE}.sql.gz /bin/sleep 2 logger "${DBNAME} has been backup successful - $DATE" done