1.备份脚本

  1. #!/bin/bash
  2. source /etc/profile
  3. # 以下配置信息请自己修改
  4. mysql_user="user" # MySQL备份用户
  5. mysql_password="password@" # MySQL备份用户的密码
  6. mysql_port="3306" # MySQL端口
  7. mysql_charset="utf8" # MySQL编码
  8. backup_db_arr=("bakbase") # 要备份的数据库名称,多个用空格分开隔开 如("db1" "db2" "db3")
  9. backup_location=/data/backup # 备份数据存放位置,末尾请不要带"/",此项可以保持默认,程序会自动创建文件夹
  10. expire_backup_delete="ON" # 是否开启过期备份删除 ON为开启 OFF为关闭
  11. expire_days=7 # 过期时间天数 默认为三天,此项只有在expire_backup_delete开启时有效
  12. # 本行开始以下不需要修改
  13. backup_time=`date +%Y%m%d%H%M` #定义备份详细时间
  14. backup_Ymd=`date +%Y-%m-%d` #定义备份目录中的年月日时间
  15. backup_3ago=`date -d '3 days ago' +%Y-%m-%d` #3天之前的日期
  16. backup_dir=$backup_location/$backup_Ymd #备份文件夹全路径
  17. welcome_msg="Welcome to use MySQL backup tools!" #欢迎语
  18. # 判断MYSQL是否启动,mysql没有启动则备份退出
  19. mysql_ps=`ps -ef |grep mysql |wc -l`
  20. mysql_listen=`netstat -an |grep LISTEN |grep $mysql_port|wc -l`
  21. if [ [$mysql_ps == 0] -o [$mysql_listen == 0] ]; then
  22. echo "ERROR:MySQL is not running! backup stop!"
  23. exit
  24. else
  25. echo $welcome_msg
  26. fi
  27. # 连接到mysql数据库,无法连接则备份退出
  28. mysql -P$mysql_port -u$mysql_user -p$mysql_password <<end
  29. use mysql;
  30. select host,user from user where user='root' and host='localhost';
  31. exit
  32. end
  33. flag=`echo $?`
  34. if [ $flag != "0" ]; then
  35. echo "ERROR:Can't connect mysql server! backup stop!"
  36. exit
  37. else
  38. echo "MySQL connect ok! Please wait......"
  39. # 判断有没有定义备份的数据库,如果定义则开始备份,否则退出备份
  40. if [ "$backup_db_arr" != "" ];then
  41. #dbnames=$(cut -d ',' -f1-5 $backup_database)
  42. #echo "arr is (${backup_db_arr[@]})"
  43. for dbname in ${backup_db_arr[@]}
  44. do
  45. echo "database [ $dbname ] backup start..."
  46. `mkdir -p $backup_dir`
  47. `mysqldump -P$mysql_port -u$mysql_user -p$mysql_password $dbname --default-character-set=$mysql_charset | gzip > $backup_dir/$dbname-$backup_time.sql.gz`
  48. flag=`echo $?`
  49. if [ $flag == "0" ];then
  50. echo "database $dbname success backup to $backup_dir/$dbname-$backup_time.sql.gz"
  51. else
  52. echo "database $dbname backup fail!"
  53. fi
  54. done
  55. else
  56. echo "ERROR:No database to backup! backup stop"
  57. exit
  58. fi
  59. # 如果开启了删除过期备份,则进行删除操作
  60. if [ "$expire_backup_delete" == "ON" -a "$backup_location" != "" ];then
  61. #`find $backup_location/ -type d -o -type f -ctime +$expire_days -exec rm -rf {} \;`
  62. `find $backup_location/ -type d -mtime +$expire_days | xargs rm -rf`
  63. echo "Expired backup data delete complete!"
  64. fi
  65. echo "All database backup success! Thank you!"
  66. exit
  67. fi

2.上传脚本

上传脚本到/data/backup目录下(如果没有创建之)

3.给脚本赋予执行权限

先确保环境变量正常配置:

  1. #MYSQL_HOME
  2. export MYSQL_HOME=/data/mysql
  3. export PATH=$PATH:$MYSQL_HOME/bin
  4. chmod +x mysql_backup.sh

4.定时执行脚本

  1. crontab -e
  2. # 输入以下内容,设置每天凌晨3:00定时自动备份
  3. 00 03 * * * /data/backup/mysql_backup.sh

5.MySQL恢复

  1. # 解压备份文件
  2. gunzip db-xxxxx.sql.gz
  3. mysql -u 用户名 p密码 数据库名 < db-xxxxx.sql