MySQL異地增量備份

來源:互聯網
上載者:User

MySQL異地增量備份:

  1. #!/bin/sh  
  2.  
  3. #MySQL Info   
  4. sqluser=root   
  5. sqlpsw=root   
  6.   
  7. /usr/bin/mysqladmin -u$sqluser -p$sqlpsw flush-logs --default-character-set=GBK   
  8.   
  9. DATADIR=/data/mysql   
  10. BAKDIR=/home/backup/mysql/daily   
  11. TEMPDIR=/home/backup/mysql/temp   
  12.   
  13. BINLOGNAME='mysql-bin'  
  14. cd $DATADIR   
  15. FILELIST=$(cat $BINLOGNAME.index | cut -c3-)  
  16.  
  17. ## COUNTER number   
  18. COUNTER=0   
  19. for file in $FILELIST   
  20. do  
  21. COUNTER=$(expr $COUNTER + 1)   
  22. done   
  23.   
  24. NextNum=0   
  25. for file in $FILELIST   
  26. do  
  27. NextNum=$(expr $NextNum + 1)   
  28. if test "$NextNum" = "$COUNTER"  
  29. then   
  30. echo "skip lastest"  
  31. else  
  32. dest=$BAKDIR/$file   
  33. if test -e $dest   
  34. then   
  35. echo "skip exist $dest"  
  36. else  
  37. cp $file $TEMPDIR   
  38. fi   
  39. fi   
  40. done   
  41. echo "backup mysql binlog ok"  
  42.   
  43. sleep 2s   
  44.   
  45. cd $TEMPDIR   
  46. tarname=binlog$(date +%y%m%d).tar.gz   
  47. tar czf $tarname ./mysql-bin*   
  48.   
  49. sleep 1s  
  50.  
  51. #ftp Info   
  52. ftphost=111.111.11.11   
  53. ftpname=xxx   
  54. ftppsw=psw   
  55.   
  56. cd $TEMPDIR   
  57.   
  58. ftp -i -n <<!   
  59. open $ftphost   
  60. user $ftpname $ftppsw   
  61. type binary   
  62. put $tarname   
  63. close   
  64. bye   
  65. !   
  66. sleep 10s   
  67.   
  68. echo "end upload"  
  69.   
  70. cd $TEMPDIR   
  71. mv $BINLOGNAME* $BAKDIR   
  72. rm -r $tarname   
  73.   
  74. echo "end"  

說明:

mysqladmin -u$sqluser -p$sqlpsw flush-logs:mysql自動把記憶體中的日誌放到記錄檔binlog的最後一個檔案裡,並產生一個空的新記錄檔,之後只需要備份前面的幾個即可,最後一個因為是剛產生的,並且是空的,所以不需備份。

關於binlog可參考:

大概思路:

把datadir和bakdir目錄中的binlog進行對比,如bakdir中沒有,且該binlog不是最後一個,則把該binlog複製到tempdir中;這樣tempdir中的binlog都是新增的binlog。之後把tempdir中的binlog進行打包壓縮,ftp到另一個伺服器上。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.