mysql_backup.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/bin/bash
  2. # mysql_backup.sh: backup mysql databases and keep newest 5 days backup.
  3. #
  4. # ${db_user} is mysql username
  5. # ${db_password} is mysql password
  6. # ${db_host} is mysql host
  7. # —————————–
  8. #/root/mysql_backup.sh
  9. # everyday 3:00 AM execute database backup
  10. # 0 3 * * * /root/mysql_backup.sh
  11. #/etc/cron.daily
  12. # MySQL 客户端程序路径
  13. mysql="/usr/mysql/mysql-5.7/bin/mysql"
  14. db_user="root"
  15. db_password="PA55W0RD"
  16. db_host="localhost"
  17. # the directory for story your backup file. #
  18. backup_dir="/home/backup/mysql/"
  19. # 要备份的数据库名 #
  20. #all_db="$(${mysql} -u ${db_user} -h ${db_host} -p${db_password} -Bse 'show databases')" #
  21. all_db="$(${mysql} -u ${db_user} -h ${db_host} -p${db_password} -Bse 'SHOW DATABASES' )"
  22. # 要保留的备份天数 #
  23. backup_day=10
  24. #数据库备份日志文件存储的路径
  25. logfile="/var/log/mysql_backup.log"
  26. ###ssh端口号###
  27. ssh_port=22
  28. ###定义ssh auto key的文件###
  29. id_rsa=/root/.ssh/id_rsa
  30. ###定义ssh auto username###
  31. id_rsa_user=root
  32. ###定义要同步的远程服务器的目录路径(必须是绝对路径)###
  33. clientPath="/home/backup/mysql"
  34. ###定义要镜像的本地文件目录路径 源服务器(必须是绝对路径)###
  35. serverPath=${backup_dir}
  36. ###定义生产环境的ip###
  37. web_ip="47.107.28.221"
  38. # date format for backup file (dd-mm-yyyy) #
  39. time="$(date +"%Y-%m-%d")"
  40. # mysql, ${mysqldump} and some other bin's path #
  41. mysqldump="/usr/mysql/mysql-5.7/bin/mysqldump"
  42. # the directory for story the newest backup #
  43. test ! -d ${backup_dir} && mkdir -p ${backup_dir}
  44. #备份数据库函数#
  45. mysql_backup()
  46. {
  47. # 取所有的数据库名 #
  48. for db in ${all_db}
  49. do
  50. backname=${db}.${time}
  51. dumpfile=${backup_dir}${backname}
  52. #将备份的时间、数据库名存入日志
  53. echo "------"$(date +'%Y-%m-%d %T')" Beginning database "${db}" backup--------" >>${logfile}
  54. ${mysqldump} -F -u${db_user} -h${db_host} -p${db_password} ${db} > ${dumpfile}.sql 2>>${logfile} 2>&1
  55. #开始将压缩数据日志写入log
  56. echo $(date +'%Y-%m-%d %T')" Beginning zip ${dumpfile}.sql" >>${logfile}
  57. #将备份数据库文件库压成ZIP文件,并删除先前的SQL文件. #
  58. tar -czvf ${backname}.tar.gz ${backname}.sql 2>&1 && rm ${dumpfile}.sql 2>>${logfile} 2>&1
  59. #将压缩后的文件名存入日志。
  60. echo "backup file name:"${dumpfile}".tar.gz" >>${logfile}
  61. echo -e "-------"$(date +'%Y-%m-%d %T')" Ending database "${db}" backup-------\n" >>${logfile}
  62. done
  63. }
  64. delete_old_backup()
  65. {
  66. echo "delete backup file:" >>${logfile}
  67. # 删除旧的备份 查找出当前目录下七天前生成的文件,并将之删除
  68. find ${backup_dir} -type f -mtime +${backup_day} | tee delete_list.log | xargs rm -rf
  69. cat delete_list.log >>${logfile}
  70. }
  71. rsync_mysql_backup()
  72. {
  73. # rsync 同步到其他Server中 #
  74. for j in ${web_ip}
  75. do
  76. echo "mysql_backup_rsync to ${j} begin at "$(date +'%Y-%m-%d %T') >>${logfile}
  77. ### 同步 ###
  78. rsync -avz --progress --delete $serverPath -e "ssh -p "${ssh_port}" -i "${id_rsa} ${id_rsa_user}@${j}:$clientPath >>${logfile} 2>&1
  79. echo "mysql_backup_rsync to ${j} done at "$(date +'%Y-%m-%d %T') >>${logfile}
  80. done
  81. }
  82. #进入数据库备份文件目录
  83. cd ${backup_dir}
  84. mysql_backup
  85. delete_old_backup
  86. rsync_mysql_backup
  87. echo -e "========================mysql backup && rsync done at "$(date +'%Y-%m-%d %T')"============================\n\n">>${logfile}
  88. cat ${logfile}