服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Mysql - 分享一个自动编写MySQL数据库备份脚本

分享一个自动编写MySQL数据库备份脚本

2022-02-20 21:35波波说运维 Mysql

其实主要是为了偷懒,所以就搞了一个自动编写MySQL数据库备份脚本,每次写备份脚本传参就可以了,仅供参考。

其实主要是为了偷懒,所以就搞了一个自动编写MySQL数据库备份脚本,每次写备份脚本传参就可以了,仅供参考。

1. MySQL备份模板(上传到下载平台)

  1. #!/bin/bash
  2. #################################
  3. # copyright by hwb
  4. # DATE:2020-12-03
  5. # 用途:MYSQL备份模板
  6. #################################
  7.  
  8. #定义
  9. db_host=localhost
  10. db_port=3306
  11. db_name=mysql_prod
  12. db_user=root
  13. db_pwd=password
  14. backup_path="/data/backup"
  15.  
  16. # view,function,procedure,event,trigger
  17. output_type='view,function,procedure,event,trigger'
  18. today=`date +"%Y%m%d-%H%M%S"`
  19. data_file=$backup_path/$db_name$today.sql
  20. object_file="${backup_path}/obj_${db_name}$today.sql"
  21. log_file="/home/scripts/mysql_backup.log"
  22. mysql_cmd="mysql -u${db_user} -p${db_pwd} -h${db_host} -P${db_port} "
  23. mysqldump_cmd="mysqldump -u${db_user} -p${db_pwd} -h${db_host} -P${db_port} $db_name "
  24.  
  25.  
  26. #调用函数库
  27. [ -f /etc/init.d/functions ] && source /etc/init.d/functions
  28. export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
  29. source /etc/profile
  30.  
  31. #Require root to run this script.
  32. [ $(id -u) -gt 0 ] && echo "请用root用户执行此脚本!" && exit 1
  33.  
  34.  
  35. [ -d $backup_path ] || mkdir -p $backup_path
  36.  
  37.  
  38. #[ ! -n "$5" ] && echo -e " Usage: $0 IP 端口 实例名 用户名 '密码' " && exit 1
  39.  
  40.  
  41. function mysql_backup()
  42. {
  43. echo ""
  44. echo -e "***********************************************mysql数据库备份****************************************************"
  45.  
  46. echo -e "**************备份数据库数据到$data_file**************"
  47. #A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events
  48. $mysqldump_cmd --single_transaction -R -E --flush-logs --master-data=2 --set-gtid-purged=OFF > $data_file
  49.  
  50. if [ $? -eq 0 ];then
  51. action "[$today]>>>完成数据库${db_name}数据备份" /bin/true
  52. echo "[$today]>>>完成数据库${db_name}数据备份" >> ${log_file}
  53. else
  54. action "[$today]>>>数据库${db_name}备份失败,请检查相关配置!" /bin/false
  55. echo "[$today]>>>数据库${db_name}备份失败,请检查相关配置!" >> ${log_file}
  56. exit 1
  57. fi
  58.  
  59.  
  60. echo -e "*******备份${db_name}函数、视图等定义到$object_file***********"
  61. cat > $object_file<<EOF
  62. ouput object‘s definition for database "$db_name"
  63. ouput time: $(date "+%Y-%m-%d %H:%M:%S")
  64. ouput object type: $output_type
  65. EOF
  66. echo "">> $object_file
  67. echo "">> $object_file
  68.  
  69. # 视图
  70. if [[ $output_type == *"view"* ]]
  71. then
  72. echo "-- ------------------------------------------------------------" >> $object_file
  73. echo "-- views" >> $object_file
  74. echo "-- ------------------------------------------------------------" >> $object_file
  75. #让 MySQL不输出列名 可以用-N 或者--skip-column-names参数
  76. $mysql_cmd --skip-column-names
  77. -e "select concat('SHOW CREATE VIEW ',table_schema,'.',table_name,';') from information_schema.views where table_schema='$db_name'" |
  78. sed 's/;/\G/g' | $mysql_cmd $db_name |
  79. sed 's/Create View: /kk_begin /g' | sed 's/[ ]*character_set_client:/; kk_end/g' |
  80. sed -n '/kk_begin/{:a;N;/kk_end/!ba;s/.*kk_begin|kk_end.*//g;p}' >> $object_file
  81. fi
  82.  
  83. # 函数
  84. if [[ $output_type == *"function"* ]]
  85. then
  86. echo "-- ------------------------------------------------------------" >> $object_file
  87. echo "-- function" >> $object_file
  88. echo "-- ------------------------------------------------------------" >> $object_file
  89. $mysql_cmd --skip-column-names
  90. -e "select concat('SHOW CREATE FUNCTION ',routine_schema,'.',routine_name,';') from information_schema.routines where routine_schema='$db_name' and ROUTINE_TYPE='FUNCTION'" |
  91. sed 's/;/\G/g' | $mysql_cmd $db_name |
  92. sed 's/Create Function: /kk_begin delimiter $$ /g' | sed 's/[ ]*character_set_client:/$$ delimiter ; kk_end/g' |
  93. sed -n '/kk_begin/{:a;N;/kk_end/!ba;s/.*kk_begin|kk_end.*//g;p}' >> $object_file
  94. fi
  95.  
  96. # 存储过程
  97. if [[ $output_type == *"procedure"* ]]
  98. then
  99. echo "-- ------------------------------------------------------------" >> $object_file
  100. echo "-- procedure" >> $object_file
  101. echo "-- ------------------------------------------------------------" >> $object_file
  102. $mysql_cmd --skip-column-names
  103. -e "select concat('SHOW CREATE PROCEDURE ',routine_schema,'.',routine_name,';') from information_schema.routines where routine_schema='$db_name' and ROUTINE_TYPE='PROCEDURE'" |
  104. sed 's/;/\G/g' | $mysql_cmd $db_name |
  105. sed 's/Create Procedure: /kk_begin delimiter $$ /g' | sed 's/[ ]*character_set_client:/$$ delimiter ; kk_end/g' |
  106. sed -n '/kk_begin/{:a;N;/kk_end/!ba;s/.*kk_begin|kk_end.*//g;p}' >> $object_file
  107. fi
  108.  
  109. # 事件
  110. if [[ $output_type == *"event"* ]]
  111. then
  112. echo "-- ------------------------------------------------------------" >> $object_file
  113. echo "-- event" >> $object_file
  114. echo "-- ------------------------------------------------------------" >> $object_file
  115. $mysql_cmd --skip-column-names
  116. -e "select concat('SHOW CREATE EVENT ',EVENT_SCHEMA,'.',EVENT_NAME,';') from information_schema.events where EVENT_SCHEMA='$db_name'" |
  117. sed 's/;/\G/g' | $mysql_cmd |
  118. sed 's/Create Event: /kk_begin delimiter $$ /g' | sed 's/[ ]*character_set_client:/$$ delimiter ; kk_end/g' |
  119. sed -n '/kk_begin/{:a;N;/kk_end/!ba;s/.*kk_begin|kk_end.*//g;p}' >> $object_file
  120. fi
  121.  
  122. # 触发器
  123. if [[ $output_type == *"trigger"* ]]
  124. then
  125. echo "-- ------------------------------------------------------------" >> $object_file
  126. echo "-- trigger" >> $object_file
  127. echo "-- ------------------------------------------------------------" >> $object_file
  128. $mysql_cmd --skip-column-names
  129. -e "select concat('SHOW CREATE TRIGGER ',TRIGGER_SCHEMA,'.',TRIGGER_NAME,';') from information_schema.triggers where TRIGGER_SCHEMA='$db_name';" |
  130. sed 's/;/\G/g' | $mysql_cmd $db_name|
  131. sed 's/SQL Original Statement: /kk_begin delimiter $$ /g' | sed 's/[ ]*character_set_client:/$$ delimiter ; kk_end/g' |
  132. sed -n '/kk_begin/{:a;N;/kk_end/!ba;s/.*kk_begin|kk_end.*//g;p}' >> $object_file
  133. fi
  134.  
  135. # ^M, you need to type CTRL-V and then CTRL-M
  136. sed -i "s/^M//g" $object_file
  137.  
  138. #清理过期备份
  139. find ${backup_path} -mtime +10 -type f -name '*.sql' -exec rm -f {} ;
  140.  
  141. if [ $? -eq 0 ];then
  142. action "[$today]>>>完成数据库${db_name}过期备份清理" /bin/true
  143. echo "[$today]>>>完成数据库${db_name}过期备份清理" >> ${log_file}
  144. else
  145. action "[$today]>>>数据库${db_name}过期备份清理失败,请检查相关配置!" /bin/false
  146. echo "[$today]>>>数据库${db_name}过期备份清理失败,请检查相关配置!" >> ${log_file}
  147. exit 1
  148. fi
  149.  
  150. echo -e "**********************************************完成${db_name}数据库备份**********************************************"
  151. cat > /tmp/mysql_backup.log << EOF
  152. mysql地址:${db_host}
  153. mysql端口:${db_port}
  154. mysql实例名:${db_name}
  155. 数据备份文件:${data_file}
  156. 定义备份文件:${object_file}
  157. EOF
  158. cat /tmp/mysql_backup.log
  159. echo -e "e[1;31m 以上信息保存在/tmp/mysql_backup.log文件下 e[0m"
  160. echo -e "*******************************************************************************************************************"
  161. echo ""
  162. }
  163.  
  164.  
  165. mysql_backup

分享一个自动编写MySQL数据库备份脚本

2. 自动编写MySQL数据库备份脚本

  1. #!/bin/bash
  2. ###################################################################
  3. # copyright by hwb
  4. # DATE: 2020-12-04
  5. # 用途:自动编写MySQL备份脚本
  6. ###################################################################
  7.  
  8. #脚本外变量
  9. mysql_path="/home/scripts"
  10. script_name="mysql_backup.sh"
  11. mysql_date=`date +"%Y-%m-%d-%H:%M:%S"`
  12. #脚本内变量
  13. db_host="localhost"
  14. db_port="53306"
  15. db_name="dbname"
  16. db_user="root"
  17. db_pwd="xxxx"
  18. backup_path="/data/backup"
  19. bk_backup_path=`echo $backup_path | sed 's:/:\/:g'`
  20. bk_db_pwd=`echo $db_pwd | sed 's:/:\/:g'`
  21.  
  22. #调用函数库
  23. [ -f /etc/init.d/functions ] && source /etc/init.d/functions
  24. export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
  25. source /etc/profile
  26.  
  27.  
  28. #root用户执行脚本
  29. [ $(id -u) -gt 0 ] && echo "请用root用户执行此脚本!" && exit 1
  30.  
  31.  
  32. #判断目录是否存在
  33. [ -d $mysql_path ] || mkdir -p $mysql_path
  34. [ -d $backup_path ] || echo "mysql数据库备份目录[$backup_path]不存在,请确认参数是否正确!"
  35. [ -d $backup_path ] || exit 1
  36.  
  37.  
  38. function bk_mysqlbackup(){
  39. echo ""
  40. echo -e "**************************自动配置mysql数据库备份脚本[$mysql_path/$script_name]**************************"
  41. echo ""
  42. echo -e " Usage: $0 "
  43. echo ""
  44.  
  45. [ -f $mysql_path/$script_name ] && echo "${mysql_path}已存在脚本[${script_name}],请检查相关配置!" && exit 1
  46.  
  47. wget https://app.fslgz.com/portal/api/public/fs/association/file/downLoad?uploadId=784001405093478400 -O /opt/mysql_backup_template.sh
  48. mv /opt/mysql_backup_template.sh $mysql_path/$script_name
  49.  
  50. #处理windows传linux的脚本格式问题(注意空格位置不能多不能少)
  51. vi +':w ++ff=unix' +':q' ${mysql_path}/${script_name}
  52.  
  53. sed -i "s/localhost/${db_host}/g" $mysql_path/$script_name
  54. sed -i "s/3306/${db_port}/g" $mysql_path/$script_name
  55. sed -i "s/mysql_prod/${db_name}/g" $mysql_path/$script_name
  56. sed -i "s/root/${db_user}/g" $mysql_path/$script_name
  57. sed -i "s/password/${bk_db_pwd}/g" $mysql_path/$script_name
  58. sed -i "s#/data/backup#${bk_backup_path}#g" $mysql_path/$script_name
  59.  
  60. action "完成mysql备份脚本[$mysql_path/$script_name]编写!" /bin/true
  61. chmod u+x $mysql_path/$script_name
  62.  
  63. #配置定时任务
  64. echo "30 11 * * * /bin/bash ${mysql_path}/${script_name} " >> /var/spool/cron/root
  65.  
  66. if [ $? -eq 0 ];then
  67. echo ""
  68. action "[$mysql_date]>>>完成数据库备份定时任务配置" /bin/true
  69. else
  70. echo ""
  71. action "[$mysql_date]>>>定时任务配置失败,请检查相关配置!" /bin/false
  72. fi
  73.  
  74. echo ""
  75. echo "|------------------------------------定时任务内容------------------------------------|"
  76. crontab -l
  77. echo ""
  78. echo "|---------------mysql备份脚本[$mysql_path/$script_name]内容如下---------------|"
  79. cat $mysql_path/$script_name
  80. echo -e "************************************完成mysql数据库备份脚本配置*****************************************"
  81. echo ""
  82. }
  83.  
  84.  
  85. bk_mysqlbackup

分享一个自动编写MySQL数据库备份脚本

3. 基于蓝鲸平台测试自动编写备份脚本

测试成功..

分享一个自动编写MySQL数据库备份脚本

4. 测试备份的脚本是否有效

成功完成备份..

分享一个自动编写MySQL数据库备份脚本

原文地址:https://www.toutiao.com/i6902767275703845379/

延伸 · 阅读

精彩推荐