MySQL remote Incremental Backup

Source: Internet
Author: User

MySQL remote Incremental Backup:

  1. #! /Bin/sh 
  2.  
  3. # MySQL Info
  4. Sqluser = root
  5. Sqlpsw = root
  6. /Usr/bin/mysqladmin-u $ sqluser-p $ sqlpsw flush-logs --Default-Character-set = GBK
  7. DATADIR =/data/mysql
  8. BAKDIR =/home/backup/mysql/daily
  9. TEMPDIR =/home/backup/mysql/temp
  10. BINLOGNAME ='Mysql-bin'
  11. Cd $ DATADIR
  12. FILELIST = $ (cat $ BINLOGNAME. index | cut-c3 -) 
  13.  
  14. # COUNTER number
  15. COUNTER = 0
  16. ForFileIn$ FILELIST
  17. Do
  18. COUNTER = $ (expr $ COUNTER + 1)
  19. Done
  20. NextNum = 0
  21. ForFileIn$ FILELIST
  22. Do
  23. NextNum = $ (expr $ NextNum + 1)
  24. IfTest"$ NextNum"="$ COUNTER"
  25. Then
  26. Echo"Skip lastest"
  27. Else
  28. Dest = $ BAKDIR/$ file
  29. IfTest-e $ dest
  30. Then
  31. Echo"Skip exist $ dest"
  32. Else
  33. Cp $ file $ TEMPDIR
  34. Fi
  35. Fi
  36. Done
  37. Echo"Backup mysql binlog OK"
  38. Sleep 2 s
  39. Cd $ TEMPDIR
  40. Tarname = binlog $
  41. Tar czf $ tarname./mysql-bin *
  42. Sleep 1 s 
  43.  
  44. # Ftp Info
  45. Ftphost = 111.111.11.11
  46. Ftpname = xxx
  47. Ftppsw = psw
  48. Cd $ TEMPDIR
  49. Ftp-I-n <!
  50. Open $ ftphost
  51. User $ ftpname $ ftppsw
  52. Type binary
  53. Put $ tarname
  54. Close
  55. Bye
  56. !
  57. Sleep 10 s
  58. Echo"End upload"
  59. Cd $ TEMPDIR
  60. Mv $ BINLOGNAME * $ BAKDIR
  61. Rm-r $ tarname
  62. Echo"End"

Note:

Mysqladmin-u $ sqluser-p $ sqlpsw flush-logs: mysql automatically stores the logs in memory in the last file of the binlog log file and generates an empty new log file, after that, you only need to back up the first few items. The last one is generated and empty, so no backup is required.

For details about binlog, refer:

General idea:

Compare the binlog in the datadir and bakdir directories. For example, if there is no binlog in the bakdir directory and the binlog is not the last one, copy the binlog to the tempdir directory. In this way, the binlog in the tempdir directory is a new binlog. Then, compress the binlog in tempdir and ftp it to another server.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.