- #!/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
相关文章
- 07-27如何利用zabbix+bat脚本对windows的数据库备份任务进行监控?
- 07-27Transact-SQL 示例 - 使用脚本备份数据库的示例
- 07-27SQL备份所有数据库脚本
- 07-27通过Load table命令将数据文件加载到Sybase IQ数据库里面的Python脚本
- 07-27常用脚本--查看数据库最近备份
- 07-27刚刚完成了在vs2013中通过 ef连接mysql数据库的工作。感觉没有想象中的简单。试了n次终于成功。故记录成功的方法,希望可以帮到大家
- 07-27如何通过Mysql的二进制日志恢复数据库数据
- 07-27通过xtrabackup工具对mysql数据库做全备
- 07-27Linux中使用mysqldump对MySQL数据库进行定时备份
- 07-27Windows下Oracle数据库自动备份批处理脚本