备份mysql数据库脚本

#!/bin/bash

# 设置mysql的登录用户名和密码
mysql_user=""
mysql_password=""
mysql_host="127.0.0.1"
mysql_port="3306"
# mysql_charset="utf8mb4"

# 备份文件存放地址
backup_location=/home/nfsfile/mysqlbackup/backup

# 是否删除过期数据
expire_backup_delete="ON"
expire_days=30
backup_time=`date +%Y%m%d%H%M`
backup_dir=$backup_location
welcome_msg="Welcome to use MySQL backup tools!"

# 判断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] ]; then
        echo "ERROR:MySQL is not running! backup stop!"
        exit
else
        echo $welcome_msg
fi

# 备份全部数据库
mysqldump -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password --all-databases | gzip > $backup_dir/all_databases-$backup_time.sql.gz
flag=`echo $?`
if [ $flag == "0" ];then
        echo "all_databases success backup to $backup_dir/mysql_backup-$backup_time.sql.gz"
else
        echo "all_databases 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

你可能感兴趣的:(mysql)