当前位置: 代码迷 >> 综合 >> MySQL备份脚本--mysqldump
  详细解决方案

MySQL备份脚本--mysqldump

热度:26   发布时间:2023-11-18 06:12:51.0
#!/bin/bash
# 设置mysql的登录用户名和密码(根据实际情况填写)
mysql_user="root"
mysql_password="123456"
mysql_host="192.1.1.1"
mysql_port="3306"
# 备份文件存放地址(根据实际情况填写)
backup_location=/opt/mysql/thunisoft/backup
#备份保留天数
expire_days=7# 是否删除过期数据
expire_backup_delete="ON"
backup_time=`date +%Y%m%d%H%M`
backup_dir=${backup_location}/${backup_time}
welcome_msg="Welcome to use MySQL backup tools!"
if [ ! -d "$backup_dir" ] ;thenmkdir -p $backup_dir
fi# 判断mysql实例是否正常运行
mysql_ps=`ps -ef |grep mysql |wc -l`
mysql_listen=`netstat -an |grep LISTEN |grep $mysql_port|wc -l`
if [ [$mysql_ps == 0] -o [$mysql_listen == 0] ]; thenecho "ERROR:MySQL is not running! backup stop!"exit
elseecho $welcome_msg
fi# 备份全部数据库
mkdir -p /opt/mysql/thunisoft/backup/$backup_time
mysqldump -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password --flush-logs --all-databases  > $backup_dir/mysql_backup_all-$backup_time.sql
flag=`echo $?`
if [ $flag == "0" ];thenecho "database mysql_backup_test success backup to $backup_dir/mysql_backup_all-$backup_time.sql.gz"
elseecho "database mysql_backup_all backup fail(失败)!"
fi# 删除历史备份数据
if [ "$expire_backup_delete" == "ON" -a  "$backup_location" != "" ];then`find $backup_location/ -type f -mtime +$expire_days | xargs rm -rf`echo "Expired backup data delete complete!"
fi
  相关解决方案