1. #!/bin/bash
  2. #********************************************************************
  3. #encoding -*-utf8-*-
  4. #Author: zhangshang
  5. #URL: http://blog.vservices.top/myblog
  6. #Description: To backup mysql databases
  7. #QQ Numbers: 765030447
  8. #********************************************************************
  9. ARGV=`getopt -o d:b:l:v:h -l dbpath:,backpath:,dblvm:,dbvg:,help,binlog: -n test.sh -- "$@"`
  10. eval set --"$ARGV"
  11. while true
  12. do
  13. case "$1" in
  14. -d|--dbpath)
  15. dbpath="$2"
  16. shift
  17. ;;
  18. -b|--backpath)
  19. backpath="$2"
  20. shift
  21. ;;
  22. -l|--dblvm)
  23. dblvm="$2"
  24. shift
  25. ;;
  26. -v|--dbvg)
  27. dbvg="$2"
  28. shift
  29. ;;
  30. --binlog)
  31. binlog="$2"
  32. shift
  33. ;;
  34. -h|--help)
  35. echo "
  36. This tool can helps you to backup your databases which located on the LVM
  37. -d|--dbpath : The path of databases;
  38. -b|--backpath : The backup directory
  39. -l|--dblvm : The lvm name of which your databases's locate
  40. -v|--dbvg : Filling out your vg's name which your database located
  41. --binlog : Tar a packege of binlog
  42. -h|--help : It helps you to use this tool!
  43. "
  44. exit 0
  45. # shift
  46. ;;
  47. --)
  48. shift
  49. break
  50. ;;
  51. *)
  52. echo "Internal error!" ;
  53. echo "
  54. This tool can helps you to backup your databases which located on the LVM
  55. -d|--dbpath : The path of databases;
  56. -b|--backpath : The backup directory
  57. -l|--dblvm : The lvm name of which your databases's locate
  58. -v|--dbvg : Filling out your vg's name which your database located
  59. --binlog : Tar a packege of binlog
  60. -h|--help : It helps you to use this tool!
  61. "
  62. exit 0
  63. ;;
  64. esac
  65. shift
  66. done
  67. #Mysql's configuration
  68. dbport='3306'
  69. dbpasswd='123123'
  70. #dbsock='/var/lib/mysql.sock'
  71. dbuser='root'
  72. #db_run_cmd="mysql -u$dbuser -p$dbpasswd -S $dbsock -e "
  73. db_run_cmd="mysql -u$dbuser -p$dbpasswd -P $dbport -e"
  74. current_data=`date +%F`
  75. #Test the given options's values
  76. function test_argments(){
  77. [ -z $1 ]
  78. }
  79. test_argments $dbpath && dbpath='/var/lib/mysql'
  80. test_argments $backpath && backpath='/backup'
  81. test_argments $dblvm && dblvm='dblvm'
  82. test_argments $dbvg && dbvg='dbvg'
  83. #Test the dbpath
  84. function test_dbpath(){
  85. local i=''
  86. local count=0
  87. for i in `ls -l $dbpath | grep '^d' | awk '{print $9}'`
  88. do
  89. [ "$i" == 'mysql' ] && let count+=1
  90. [ "$i" == 'performance_schema' ] && let count+=1
  91. done
  92. [ "$count" -lt 2 ] && echo 'There not a dbpath!' && exit 1
  93. }
  94. test_dbpath
  95. #Create a snapshot
  96. function mk_snapshot(){
  97. eval $db_run_cmd '"FLUSH TABLES WITH READ LOCK;"'
  98. [ $? -ne 0 ] && { echo 'Lock tables failed!' ; exit 1; }
  99. eval $db_run_cmd '"show binary logs" | tail -n 1 > /$backpath/binlog_position'
  100. lvcreate -n sql_data_snapshot -L 2G -s -p r /dev/$dbvg/$dblvm &>/dev/null
  101. [ $? -ne 0 ] && echo -e "\033[31msql_data_snapshot already exists! It's meaning that previous copy wasn't completed\033[0m" && exit 1
  102. eval $db_run_cmd '"flush logs;"'
  103. eval $db_run_cmd '"unlock tables;"'
  104. }
  105. #Backup the dbdate
  106. function bk_db(){
  107. mkdir -p $backpath &>/dev/null
  108. mkdir -p /snapshot_tmp &>/dev/null
  109. mount -o nouuid,norecovery /dev/$dbvg/$dblvm /snapshot_tmp/ &>/dev/null
  110. [ $? -ne 0 ] && echo 'mount error!' && exit 1
  111. /usr/bin/cp -ra /snapshot_tmp /backup/mariadb_backup.$current_data
  112. [ $? -ne 0 ] && echo "\033[31mCopying failed! Please check the harddisk-room. Cleaning the copy-data!\033[0m" && rm -rf /backup/mariadb_backup.$current_data && state=1
  113. umount /snapshot_tmp
  114. [ $? -ne 0 ] && echo 'Umount error !'
  115. cd / && rm -rf /snapshot_tmp
  116. }
  117. #Remove the snapshot
  118. function rm_snapshot(){
  119. [ -n "$state" ] && echo -e "\033[31mThe databases's snapshot has't been remove! Don't forget to use this command \033[0m\033[32m\`lvremove -y /dev/$dbvg/sql_data_snapshot \`\033[0m\033[31m to remove it! \033[0m" && exit 1
  120. lvremove -y /dev/$dbvg/sql_data_snapshot 1>/dev/null
  121. }
  122. #Copy the position-file to backup path
  123. function cp_positon_file(){
  124. /usr/bin/cp $backpath/binlog_position $backpath/mariadb_backup.$current_data/
  125. }
  126. #Backup the binlog
  127. function tar_binlog(){
  128. cd $binlog
  129. echo -e "\033[31mAs you need ,you can modify the regex!\033[0m"
  130. ls | grep '.*bin\.[[:digit:]]\{6\}$' | xargs tar cvzf $backpath/mariadb_backup.$current_data/binlog.$current_data.tar.gz 1>/dev/null
  131. [ $? -ne 0 ] && echo '\033[31mCopying failed! Please check the harddisk-room. Cleaning the copy-binlog-data!\033[0m' && rm -rf $backpath/mariadb_backup.$current_data/binlog.$current_data.tar.gz && exit 1
  132. }
  133. #Begin to starting backup
  134. mk_snapshot
  135. bk_db
  136. rm_snapshot
  137. cp_positon_file
  138. if [ -n "$binlog" ]
  139. then
  140. tar_binlog
  141. fi

通过LVM备份mysql数据库脚本的更多相关文章

  1. Linux自动备份MySQL数据库脚本代码

    下面这段Linux的Shell脚本用于每日自动备份MySQL数据库,可通过Linux的crontab每天定时执行 在脚本中可设置需要备份的数据库表清单,并且会将备份文件通过gzip压缩.需要注意的是, ...

  2. Linux- 自动备份MySQL数据库脚本

    数据安全很重要,所以日常中需要对数据库进行备份.

  3. 自动备份Mysql数据库脚本

    [root@bogon ~]# cat auto_backup_mysql.sh #!/bin/bash #auto backup mysql db #by authors wugk #define ...

  4. 备份MySQL数据库

    备份MySQL数据库脚本: #!/bin/bash # description: MySQL buckup shell script # author: lmj # web site: http:// ...

  5. 一个备份MySQL数据库的简单Shell脚本(转)

    Shell脚本是我们写不同类型命令的一种脚本,这些命令在这一个文件中就可以执行.我们也可以逐一敲入命令手动执行.如果我们要使用shell脚本就必须在一开始把这些命令写到一个文本文件中,以后就可以随意反 ...

  6. [转]一个备份MySQL数据库的简单Shell脚本

    本文翻译自 iSystemAdmin 的 <A Simple Shell Script to Backup MySQL Database> Shell脚本是我们写不同类型命令的一种脚本,这 ...

  7. Linux下定时备份MySQL数据库的Shell脚本

    Linux下定时备份MySQL数据库的Shell脚本   对任何一个已经上线的网站站点来说,数据备份都是必须的.无论版本更新还是服务器迁移,备份数据的重要性不言而喻.人工备份数据的方式不单耗费大量时间 ...

  8. Linux下备份MySQL数据库的Shell脚本

    数据库每天都想备份,手动备份太麻烦而又容易忘记,所以写了一个自动备份MySQL数据库的脚本,加入定时计划中,每天自运运行. 创建Shell脚本代码如下,命名为mysql_dump.sh #!/bin/ ...

  9. 使用shell脚本定时执行备份mysql数据库

    使用shell脚本定时执行备份mysql数据库 #!/bin/bash ############### common file ################ #本机备份文件存放目录 MYSQLBA ...

随机推荐

  1. jdbc配置及使用测试

    源码:https://github.com/xiaostudy/jdbc_test1 这是没有使用连接池的 目录 创建的sql语句create.sql DROP TABLE IF EXISTS t_u ...

  2. [转帖]POWER ISA开源 浪潮商用机器加速POWER技术生态建设步伐

    POWER ISA开源 浪潮商用机器加速POWER技术生态建设步伐 [原创]   2019-08-26 18:51:04 关键字: 开源 Power 浪潮商用机器 http://server.zhid ...

  3. ThreadLocal父子线程之间的数据传递问题

    一.问题的提出 在系统开发过程中常使用ThreadLocal进行传递日志的RequestId,由此来获取整条请求链路.然而当线程中开启了其他的线程,此时ThreadLocal里面的数据将会出现无法获取 ...

  4. 关于spring 事务 和 AOP 管理事务和打印日志问题

    关于spring 事务 和 AOP 管理事务和打印日志问题 1. 就是支持事务注解的(@Transactional) . ​ 可以在server层总使用@Transactional,进行方法内的事务管 ...

  5. Android Studio彻底删除Module

    在"Project"视图中选择需要删除的module名,此处删除"app",点击右键,选择"Open Module Setting",然后选 ...

  6. 20190728-Python爬取视频&切割视频&视频加水印

    1.视频爬取 1.下载视频的源码如下: import os import requests from bs4 import BeautifulSoup import threading from bj ...

  7. LeetCode 61——旋转链表(JAVA)

    给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2 输出: 4-& ...

  8. 【转载】STM32 IAP 在线升级详解

      (扩展-IAP主要用于产品出厂后应用程序的更新作用,考虑到出厂时要先烧写IAP  再烧写APP应用程序要烧写2次增加工人劳动力基础上写了“STM32 IAP+APP ==>双剑合一”链接稍后 ...

  9. 关于泛型擦除的知识(来源于csdn地址:https://blog.csdn.net/briblue/article/details/76736356)

    泛型,一个孤独的守门者. 大家可能会有疑问,我为什么叫做泛型是一个守门者.这其实是我个人的看法而已,我的意思是说泛型没有其看起来那么深不可测,它并不神秘与神奇.泛型是 Java 中一个很小巧的概念,但 ...

  10. Google开源项目风格指南

    Google开源项目风格指南 来源 https://github.com/zh-google-styleguide/zh-google-styleguide Google 开源项目风格指南 (中文版) ...