通过LVM备份mysql数据库脚本
- #!/bin/bash
- #********************************************************************
- #encoding -*-utf8-*-
- #Author: zhangshang
- #URL: http://blog.vservices.top/myblog
- #Description: To backup mysql databases
- #QQ Numbers: 765030447
- #********************************************************************
- ARGV=`getopt -o d:b:l:v:h -l dbpath:,backpath:,dblvm:,dbvg:,help,binlog: -n test.sh -- "$@"`
- eval set --"$ARGV"
- while true
- do
- case "$1" in
- -d|--dbpath)
- dbpath="$2"
- shift
- ;;
- -b|--backpath)
- backpath="$2"
- shift
- ;;
- -l|--dblvm)
- dblvm="$2"
- shift
- ;;
- -v|--dbvg)
- dbvg="$2"
- shift
- ;;
- --binlog)
- binlog="$2"
- shift
- ;;
- -h|--help)
- echo "
- This tool can helps you to backup your databases which located on the LVM
- -d|--dbpath : The path of databases;
- -b|--backpath : The backup directory
- -l|--dblvm : The lvm name of which your databases's locate
- -v|--dbvg : Filling out your vg's name which your database located
- --binlog : Tar a packege of binlog
- -h|--help : It helps you to use this tool!
- "
- exit 0
- # shift
- ;;
- --)
- shift
- break
- ;;
- *)
- echo "Internal error!" ;
- echo "
- This tool can helps you to backup your databases which located on the LVM
- -d|--dbpath : The path of databases;
- -b|--backpath : The backup directory
- -l|--dblvm : The lvm name of which your databases's locate
- -v|--dbvg : Filling out your vg's name which your database located
- --binlog : Tar a packege of binlog
- -h|--help : It helps you to use this tool!
- "
- exit 0
- ;;
- esac
- shift
- done
- #Mysql's configuration
- dbport='3306'
- dbpasswd='123123'
- #dbsock='/var/lib/mysql.sock'
- dbuser='root'
- #db_run_cmd="mysql -u$dbuser -p$dbpasswd -S $dbsock -e "
- db_run_cmd="mysql -u$dbuser -p$dbpasswd -P $dbport -e"
- current_data=`date +%F`
- #Test the given options's values
- function test_argments(){
- [ -z $1 ]
- }
- test_argments $dbpath && dbpath='/var/lib/mysql'
- test_argments $backpath && backpath='/backup'
- test_argments $dblvm && dblvm='dblvm'
- test_argments $dbvg && dbvg='dbvg'
- #Test the dbpath
- function test_dbpath(){
- local i=''
- local count=0
- for i in `ls -l $dbpath | grep '^d' | awk '{print $9}'`
- do
- [ "$i" == 'mysql' ] && let count+=1
- [ "$i" == 'performance_schema' ] && let count+=1
- done
- [ "$count" -lt 2 ] && echo 'There not a dbpath!' && exit 1
- }
- test_dbpath
- #Create a snapshot
- function mk_snapshot(){
- eval $db_run_cmd '"FLUSH TABLES WITH READ LOCK;"'
- [ $? -ne 0 ] && { echo 'Lock tables failed!' ; exit 1; }
- eval $db_run_cmd '"show binary logs" | tail -n 1 > /$backpath/binlog_position'
- lvcreate -n sql_data_snapshot -L 2G -s -p r /dev/$dbvg/$dblvm &>/dev/null
- [ $? -ne 0 ] && echo -e "\033[31msql_data_snapshot already exists! It's meaning that previous copy wasn't completed\033[0m" && exit 1
- eval $db_run_cmd '"flush logs;"'
- eval $db_run_cmd '"unlock tables;"'
- }
- #Backup the dbdate
- function bk_db(){
- mkdir -p $backpath &>/dev/null
- mkdir -p /snapshot_tmp &>/dev/null
- mount -o nouuid,norecovery /dev/$dbvg/$dblvm /snapshot_tmp/ &>/dev/null
- [ $? -ne 0 ] && echo 'mount error!' && exit 1
- /usr/bin/cp -ra /snapshot_tmp /backup/mariadb_backup.$current_data
- [ $? -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
- umount /snapshot_tmp
- [ $? -ne 0 ] && echo 'Umount error !'
- cd / && rm -rf /snapshot_tmp
- }
- #Remove the snapshot
- function rm_snapshot(){
- [ -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
- lvremove -y /dev/$dbvg/sql_data_snapshot 1>/dev/null
- }
- #Copy the position-file to backup path
- function cp_positon_file(){
- /usr/bin/cp $backpath/binlog_position $backpath/mariadb_backup.$current_data/
- }
- #Backup the binlog
- function tar_binlog(){
- cd $binlog
- echo -e "\033[31mAs you need ,you can modify the regex!\033[0m"
- ls | grep '.*bin\.[[:digit:]]\{6\}$' | xargs tar cvzf $backpath/mariadb_backup.$current_data/binlog.$current_data.tar.gz 1>/dev/null
- [ $? -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
- }
- #Begin to starting backup
- mk_snapshot
- bk_db
- rm_snapshot
- cp_positon_file
- if [ -n "$binlog" ]
- then
- tar_binlog
- fi
通过LVM备份mysql数据库脚本的更多相关文章
- Linux自动备份MySQL数据库脚本代码
下面这段Linux的Shell脚本用于每日自动备份MySQL数据库,可通过Linux的crontab每天定时执行 在脚本中可设置需要备份的数据库表清单,并且会将备份文件通过gzip压缩.需要注意的是, ...
- Linux- 自动备份MySQL数据库脚本
数据安全很重要,所以日常中需要对数据库进行备份.
- 自动备份Mysql数据库脚本
[root@bogon ~]# cat auto_backup_mysql.sh #!/bin/bash #auto backup mysql db #by authors wugk #define ...
- 备份MySQL数据库
备份MySQL数据库脚本: #!/bin/bash # description: MySQL buckup shell script # author: lmj # web site: http:// ...
- 一个备份MySQL数据库的简单Shell脚本(转)
Shell脚本是我们写不同类型命令的一种脚本,这些命令在这一个文件中就可以执行.我们也可以逐一敲入命令手动执行.如果我们要使用shell脚本就必须在一开始把这些命令写到一个文本文件中,以后就可以随意反 ...
- [转]一个备份MySQL数据库的简单Shell脚本
本文翻译自 iSystemAdmin 的 <A Simple Shell Script to Backup MySQL Database> Shell脚本是我们写不同类型命令的一种脚本,这 ...
- Linux下定时备份MySQL数据库的Shell脚本
Linux下定时备份MySQL数据库的Shell脚本 对任何一个已经上线的网站站点来说,数据备份都是必须的.无论版本更新还是服务器迁移,备份数据的重要性不言而喻.人工备份数据的方式不单耗费大量时间 ...
- Linux下备份MySQL数据库的Shell脚本
数据库每天都想备份,手动备份太麻烦而又容易忘记,所以写了一个自动备份MySQL数据库的脚本,加入定时计划中,每天自运运行. 创建Shell脚本代码如下,命名为mysql_dump.sh #!/bin/ ...
- 使用shell脚本定时执行备份mysql数据库
使用shell脚本定时执行备份mysql数据库 #!/bin/bash ############### common file ################ #本机备份文件存放目录 MYSQLBA ...
随机推荐
- fcntl和flock两个系统调用的区别
总的来说,flock函数只能锁定整个文件,无法锁定文件的某一区域.而fcntl可以利用struct flock结构体,来实现文件里部分区域锁定的操作. 附:fcntl(文件描述词操作) 相关函数 op ...
- 关于spring 事务 和 AOP 管理事务和打印日志问题
关于spring 事务 和 AOP 管理事务和打印日志问题 1. 就是支持事务注解的(@Transactional) . 可以在server层总使用@Transactional,进行方法内的事务管 ...
- where用法
where 子句用于指定类型约束. 1.接口约束 public class MyGenericClass<T> where T:IComparable { } 2.基类约束: 指出某个类 ...
- Windows 编程 键盘
键盘对于大家来说可能再也熟悉不过了,它和鼠标是现在最常用的电脑输入设备.虽然在现在的图形界面操作系统下使用鼠标比使用键盘更方便.更广泛,但是鼠标还是一时半会儿取代不了它的老前辈——键盘的地位,尤其是在 ...
- VBA学习资料分享-3
VBA创建/发送OUTLOOK邮件时怎么加上默认签名呢?用过OUTLOOK写邮件的人都知道,如果你设置了默认签名,那么在创建空白邮件的时候就会自动加上你设置的签名.根据这一特性,我们可以在用VBA创建 ...
- ActiveMQ入门系列一:认识并安装ActiveMQ(Windows下)
一.什么是ActiveMQ 度娘给出的定义: Apache ActiveMQ是Apache软件基金会所研发的开放源代码消息中间件:由于ActiveMQ是一个纯Java程序,因此只需要操作系统支持Jav ...
- Js-带进度条的轮播图
带进度条的轮播图--原生JS实现 实现了图片自动轮播,左右按钮实现图片左右转换,下方原点或者缩小图点击选择其中的某一张图片,然后有红条实现图片的进度. <div class="cont ...
- Oracle 如何查看当前的实例及切换实例
一.Oracle查看当前实例 1.打开终端,输入命令sqlpuls / as sysdba连接到数据库 2.输入命令show parameter name便可以查看当前登录数据库的参数配置,如下可以看 ...
- flutter问题集锦
现在的多平台开发很多啦,目前主流的救赎react native 和weex了,其他的旧的就不说了,新出来的gomobile目前基本无人使用,底层的很多原生对接还没有完善,flutter发布了第一版正式 ...
- Browser Security-超文本标记语言(HTML)
Browser Security-超文本标记语言(HTML) 瞌睡龙 · 2013/06/19 18:55 重要的4个规则: 1 &符号不应该出现在HTML的大部分节点中. 2 尖括号< ...