linux定時備份mysql並同步到其它伺服器

來源:互聯網
上載者:User

標籤:rest   同步工具   系統事件   move   this   error   remote   lda   --   

資料在任何一家公司裡面都是最核心的資產,定期備份則是為了保證資料庫出現問題的時候能夠及時復原到最近的備份點,將損失縮小到最小

這篇文章將會兩部分來說明:1、mysql的定期備份;2、同步到其它伺服器

 

mysql 備份

 

備份還原某個資料庫

備份還原

# 匯出資料庫/usr/bin/mysqldump -u root -ppwd database > database20160929.sql# 匯入資料庫mysql -u root -p database < database20160929.sql

備份到壓縮檔從壓縮檔匯入

#備份到壓縮檔/usr/bin/mysqldump -u root -ppwd database  | gzip > database20160929.sql.gz#從壓縮檔匯入gzip < database20160929.sql.gz | mysql -u root -p database

 

crontab定時備份

1、建立備份目錄

# root 使用者,建立備份目錄mkdir -p /bak/mysqlbakcd /bak/mysqldata

2、編寫運行指令碼

vi  /usr/sbin/bakmysql.sh

指令碼代碼:

#!/bin/bash# Name:bakmysql.sh# This is a ShellScript For Auto DB Backup and Delete old Backup#backupdir=/bak/mysqlbaktime=` date +%Y%m%d%H `mysql_bin_dir/mysqldump -u root -ppwd database | gzip > $backupdir/database$time.sql.gz#find $backupdir -name "name_*.sql.gz" -type f -mtime +7 -exec rm {} ; > /dev/null 2>&1#

指令碼說明:

  • backupdir mysql備份地址
  • root mysql使用者名稱
  • pwd mysql密碼
  • database 資料庫名
  • mysql_bin_dir mysql的bin路徑;
  • time=` date +%Y%m%d%H `也可以寫為time="$(date +"%Y%m%d$H")"其中`符號是TAB鍵上面的符號,不是ENTER左邊的‘符號,還有date後要有一個空格。
  • type f 表示尋找普通類型的檔案,f表示普通檔案。
  • mtime +7 按照檔案的更改時間來尋找檔案,+5表示檔案更改時間距現在7天以前;如果是 -mmin +5 表示檔案更改時間距現在5分鐘以前。
  • exec rm {} \ 表示執行一段shell命令,exec選項後面跟隨著所要執行的命令或指令碼,然後是一對兒{},一個空格和一個,最後是一個分號。
  • /dev/null 2>&1 把標準出錯重新導向到標準輸出,然後扔到/DEV/NULL下面去。通俗的說,就是把所有標準輸出和標準出錯都扔到垃圾桶裡面;其中的& 表示讓該命令在後台執行。

3、為指令碼添加執行許可權

# chmod +x /usr/sbin/bakmysql.sh

4、設定crontab定時執行

vi /etc/crontab #在最後一行中加入:  00 3 * * * root /usr/sbin/bakmysql.sh#表示每天3點00分執行備份

註:crontab設定檔格式如下:
分 時 日 月 周  命令

5、重啟crontab

/etc/rc.d/init.d/crond restart  

這樣就完了定時備份並清理前7天的備份資料

 

同步到其它伺服器

這裡使用Linux同步檔案工具rsync+inotify來進行檔案的同步

 

rsync

rsync是類unix系統下的資料鏡像備份工具——remote sync。一款快速增量備份工具 Remote Sync,遠程同步 支援本地複製,或者與其他SSH、rsync主機同步

用法

rsync src dest

這是最簡單的用法,表示同步src,dest檔案。(即,執行之後,dest的檔案與src的相同,以src的為準)

常用選項

  • -a: 等價於-rlptgoD,歸檔式
  • -r: 遞迴
  • -l: 複製軟體連結
  • -p: 保留許可權資訊
  • -t: 將src的修改時間,同步到dest
  • -g: 同步群組資訊(group)
  • -o: 同步擁有者資訊(own)
  • -D: 保持字元與塊裝置檔案
  • -z: 啟用壓縮傳輸
  • -–delete:如果src沒有此檔案,那麼dest也不能有,即在dest刪除src裡沒有的檔案。(如果你使用這個選項,就必須搭配-r選項一起)
## 將本地/bak/mysqlbak/檔案同步到 遠程伺服器 /bak/mysql/bak 目錄下面 排除 mysqlbak/index目錄 通過ssh連接埠rsync -vzacu  /bak/mysqlbak/  [email protected]168.53.86:/bak/mysqlbak   --exclude  "mysqlbak/index"   -e "ssh -p 22"# 將遠程目錄 /bak/mysqlbak下的檔案同步到本地 /bak/mysqlbak/目錄下rsync -vzrtopg --progress --delete [email protected]168.53.85:/bak/mysqlbak  /bak

 

啟用rsync伺服器端同步處理遠程檔案

rsycn的服務端為伺服器的檔案接收端,rsycn的用戶端為伺服器的檔案推動端。

 

rsycn的服務端/檔案接收端配置

服務端需要開啟rsyncd服務

添加設定檔rsyncd.conf

vi /etc/rsyncd.conf#以下是全域配置log file = /var/log/rsyncd.logpid file = /var/run/rsyncd.pidlock file = /var/lock/rsyncd[mysqlbak]     #模組名,在原始伺服器指定這個名字   comment = sync rsync/home      #描述資訊   path = /bak/mysqlbak      #備份目錄   use chroot=no           #不使用chroot,不用root許可權   read only = no          #設定本地備份目錄為讀寫權限   uid=root             gid=root   max connections=10       #用戶端最大串連數   auth users = root      #指定資料同步使用者   secrets file = /etc/rsyncd.pass          #指定資料同步使用者資訊檔   hosts allow=192.168.53.0/85     #允許串連的用戶端   ignore errors = yes     #忽略出現I/O錯誤   timeout = 600

建立認證檔案

  vi /etc/rsyncd.pass  ##代碼  root:root      #格式是使用者名稱:密碼  #屬主要有許可權讀這個檔案,否則會報沒許可權  chmod 600 /etc/rsyncd.pass  

修改/etc/xinetd.d/rsync檔案,disable 改為 no

service rsync{        disable = no        socket_type     = stream        wait            = no        user            = root        server          = /usr/bin/rsync        server_args     = --daemon        log_on_failure  += USERID}

啟動服務端

rsync --daemon --config=/etc/rsyncd.conf

 

rsycn的用戶端/檔案發送端配置

用戶端配置簡單 只需要配置密碼既可

  vi /etc/rsync_client.pwd  ##代碼  root    #只需要填寫rsync服務的密碼  #屬主要有許可權讀這個檔案,否則會報沒許可權  chmod 600 /etc/rsync_client.pwd 

用戶端同步測試

/usr/bin/rsync -auvrtzopgP --progress --password-file=/etc/rsync_client.pwd /bak/mysqlbak/ [email protected]192.168.53.86::mysqlbak

rsync只是一次性同步,如果需要即時同步就需要引入另一個工具了

 

inotify

Inotify 是一種強大的、細粒度的、非同步檔案系統事件監控機制,linux核心從2.6.13起,加入了Inotify支援,通過Inotify可以監控檔案系統中添加、刪除,修改、移動等各種細微事件,利用這個核心介面,第三方軟體就可以監控檔案系統下檔案的各種變化情況,而inotify-tools就是這樣的一個第三方軟體。

Inotify只需要要按照部署在同步的用戶端,當監控的檔案有變化觸動 rsync指令碼來同步

安裝

yum install inotify-tools

配置監控的檔案路徑

vi /etc/inotify_exclude.lst#代碼/bak/mysqlbak #監控目錄@/bak/log #排除監控目錄

rsync排除監控檔案目錄

vi /etc/rsyncd.d/rsync_exclude.lst#代碼src/*.html*src/js/src/2014/20140[1-9]/

用戶端同步到遠端指令碼rsync.sh

#rsync auto sync script with inotify#variablescurrent_date=$(date +%Y%m%d_%H%M%S)source_path=/bak/mysqlbak/log_file=/var/log/rsync_client.log#rsyncrsync_server=192.168.53.86rsync_user=rootrsync_pwd=/etc/rsync_client.pwdrsync_module=mysqlbakINOTIFY_EXCLUDE=‘(.*/*\.log|.*/*\.swp)$|^/tmp/src/mail/(2014|20.*/.*che.*)‘RSYNC_EXCLUDE=‘/bak/rsync_exclude.lst‘#rsync client pwd checkif [ ! -e ${rsync_pwd} ];then    echo -e "rsync client passwod file ${rsync_pwd} does not exist!"    exit 0fi#inotify_functioninotify_fun(){    /usr/bin/inotifywait -mrq --timefmt ‘%Y/%m/%d-%H:%M:%S‘ --format ‘%T %w %f‘           --exclude ${INOTIFY_EXCLUDE}  -e modify,delete,create,move,attrib ${source_path}           | while read file      do          /usr/bin/rsync -auvrtzopgP --exclude-from=${RSYNC_EXCLUDE} --progress --bwlimit=200 --password-file=${rsync_pwd} ${source_path} ${rsync_user}@${rsync_server}::${rsync_module}       done}#inotify loginotify_fun >> ${log_file} 2>&1 &

給指令碼執行許可權,執行後就可以了

chmod 777 rsync.sh./rsync.sh 

 

參考:

Linux下同步工具inotify+rsync使用詳解

http://www.cnblogs.com/ityouknow/p/5923489.html

 

linux定時備份mysql並同步到其它伺服器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.