MySQL备份bash脚本
时间:2019-08-20 17:58:27 来源:igfitidea点击:
这是一个简单的mysql备份脚本。那个脚本可以手动使用,也可以在crontab中使用。
脚本以.sql格式进行备份,压缩sql文件,最后发送包含mysql备份状态的邮件。
安装mail命令
yum install mailx ( Red Hat 和 CentOS) 或者 apt-get install mail-utils ( Debian 和 Ubuntu)
创建备份目录
mkdir -p /root/database-backup/
创建MySQL备份脚本
vi db-backupscript.sh
内容如下
#!/bin/bash # mysql backup script # Author: Hyman # MYSQLUSER=Mysql-user PASSWORD=Mysql-user-Passw0rd DBNAME=Database-Name ### Give MYSQL Server IP address or FQDN, For eg. 127.0.0.1 or localhost MYSQLHOST=127.0.0.1 BACKUPPATH=/root/database-backup/$DBNAME-`date +%F-%H%M%S` #You can give multiple email id in MAILTO variable by using comma (,) for eg. [email protected],[email protected] [email protected] mysqldump -h$MYSQLHOST -u$MYSQLUSER -p$PASSWORD $DBNAME > $BACKUPPATH ### compressing the file gzip $BACKUPPATH FILESIZE=$( du -sh $BACKUPPATH.gz ) if [ ! -f $BACKUPPATH.gz ]; then echo "$BACKUPPATH.gz File not found!, Database Name: $DBNAME" | mail -s "$DBNAME backup failed" $MAILTO else echo "$BACKUPPATH.gz File found,Database Name: $DBNAME, Actual size after compression is $FILESIZE " | mail -s "DBNAME database backup is done" $MAILTO fi
添加可执行权限
chmod 700 db-backupscript.sh