結合Git實現Mysql差異備份,可用於生產環境

來源:互聯網
上載者:User

埋頭苦幹多年一直沒寫過文章,今天突發狂想,為LNMP陣營貢獻一些力量。就從平時工作過程中的心得和一些技巧分享出來。今天就猿們最熟悉的Mysql開始宅鳥的開篇部落格文章。歡迎猿們拍磚、轉載。

注意:宅鳥的測試環境和生產環境為ubuntu


Mysql是程式猿和營運猿最關心的開發利器之一,今天就來談談Mysql的日常備份之宅鳥見.

常見的Mysql備份方法很多在此不在贅述。直上乾貨!


本shell指令碼運行需要安裝

git  

ssh  

要求讀者對mysql,git,ssh,shell有一定瞭解


本指令碼功能:把遠程mysqlserver通過指令碼配置的白名單和黑名單把指定資料庫sql檔案利用git版本控製備份到localserver指定目錄下,

通過版本控制git查看資料庫表資料的變化


預先配置好從localserver->mysqlserver的免密碼登入(配置過程)

localserver(192.168.1.110)


db_backup_local.sh



#!/bin/bash#created by lihuibin#date 2013-8-30#desc backup mysqlsql file from mysqlserver to localservertar_path="/root/mysql_backup"ssh root@192.168.1.120 /root/shell/db_backup.sh[ -d $tar_path ] && cd $tar_path; git pull  || git clone root@192.168.1.20:/root/shell/mysql_backup.git


mysqlserver(192.168.1.120)

假設mysqlserver伺服器上有testdb1,testdb2,exdb1,exdb2四個資料庫


指令碼位置:/root/shell/db_backup.sh


#!/bin/bash#created by lihuibin#date 2013-8-30#desc  exec mysqldump mysqlsql file from mysqlserver to gittime=`date '+%F %R'`mysql_user="backup_user"mysql_password="backup_passwd"mysql_host="127.0.0.1"mysql_port="3306"tar_path=/root/shell/mysql_backupbackup_db_arr=("testdb1" "testdb2")  #需要備份的資料庫列表#backup_db_arr=()exclude_db_arr=("exdb1" "exdb2")     #排除備份的資料庫列表  當backup_db_arr為空白的時候,exclude_db_arr生效,不為空白時僅backup_db_arr有效[ -d $tar_path ]is_first_backup=$?in_array() {    local hay needle=$1    shift    for hay; do        [[ $hay == $needle ]] && return 0    done    return 1}backup_length=${#backup_db_arr[@]}#echo $backup_lengthexclude_length=${#exclude_db_arr[@]}#echo $exclude_length;#in_array "dopool_blog" ${backup_db_arr[@]} && echo hit || echo missfor dbname in `mysql -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password -e "show databases" |sed '1,2d'`do     if [ $backup_length -gt 0 ]; then          in_array $dbname ${backup_db_arr[@]} || continue     elif [ $backup_length -eq 0 -a $exclude_length -gt 0 ]; then          in_array $dbname ${exclude_db_arr[@]} && continue     fi     for tablename in `mysql -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password -e "show tables from $dbname" |sed '1d'`     do         mkdir -p $tar_path/$dbname/         /usr/bin/mysqldump --lock-tables=TRUE  --extended-insert=FALSE --complete-insert=TRUE -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password $dbname $tablename > $tar_path/$dbname/$tablename.sql     donedoneinit_git(){tar_path=`dirname $1`dir_name=`basename $1` cd $1git initgit add .git commit -a -m "init $time"cd $tar_pathgit clone --bare $dir_name/rm -rf $dir_namegit clone $dir_name.git}[ $is_first_backup -eq 1 ] && init_git $tar_path || {                                                     cd $tar_path;                                                     git add .;                                                     git commit -a -m " back up $time";                                                     git push;                                                      }


指令碼執行後,就可以結合git的tig、diff命令查看每次備份的中資料庫表中版本變化

幹活吐槽結束 有不足之處,歡迎拍磚!!!

本文出自 “宅鳥” 部落格,請務必保留此出處http://birdinroom.blog.51cto.com/7740375/1341884

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.