码迷,mamicode.com
首页 > 数据库 > 详细

MySql定时备份脚本

时间:2019-12-26 13:11:16      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:指定   mysql   rgs   ack   har   bash   jin   echo   sql   

shell脚本mysql_backup_test_backup.sh

#!/bin/bash
# 设置mysql的登录用户名和密码(根据实际情况填写)
mysql_user="***"
mysql_password="****"
mysql_host="localhost"
mysql_port="3306"
mysql_charset="utf8mb4"

# 备份文件存放地址(根据实际情况填写)
backup_location=/home/app/***/sql_backup

# 是否删除过期数据
expire_backup_delete="ON"
expire_days=7
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

# 备份指定数据库中数据(此处假设数据库是mysql_backup_test)
mysqldump -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password -B mysql_backup_test> $backup_dir/mysql_backup_test-$backup_time.sql
flag=`echo $?`
if [ $flag == "0" ];then
        echo "database mysql_backup_test success backup to $backup_dir/mysql_backup_test-$backup_time.sql.gz"
else
        echo "database mysql_backup_test 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

 

 

编辑定时任务:

crontab -e
# 添加定时任务(每天12:50以及23:50执行备份操作)
50 12,23 * * * cd /home/app/jinxd/;sh mysql_backup_test_backup.sh >> log.txt 2>>log.txt

  

MySql定时备份脚本

标签:指定   mysql   rgs   ack   har   bash   jin   echo   sql   

原文地址:https://www.cnblogs.com/418836844qqcom/p/12101306.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!