MySQL remote Incremental Backup:
- #! /Bin/sh
-
- # MySQL Info
- Sqluser = root
- Sqlpsw = root
- /Usr/bin/mysqladmin-u $ sqluser-p $ sqlpsw flush-logs --Default-Character-set = GBK
- DATADIR =/data/mysql
- BAKDIR =/home/backup/mysql/daily
- TEMPDIR =/home/backup/mysql/temp
- BINLOGNAME ='Mysql-bin'
- Cd $ DATADIR
- FILELIST = $ (cat $ BINLOGNAME. index | cut-c3 -)
-
- # COUNTER number
- COUNTER = 0
- ForFileIn$ FILELIST
- Do
- COUNTER = $ (expr $ COUNTER + 1)
- Done
- NextNum = 0
- ForFileIn$ FILELIST
- Do
- NextNum = $ (expr $ NextNum + 1)
- IfTest"$ NextNum"="$ COUNTER"
- Then
- Echo"Skip lastest"
- Else
- Dest = $ BAKDIR/$ file
- IfTest-e $ dest
- Then
- Echo"Skip exist $ dest"
- Else
- Cp $ file $ TEMPDIR
- Fi
- Fi
- Done
- Echo"Backup mysql binlog OK"
- Sleep 2 s
- Cd $ TEMPDIR
- Tarname = binlog $
- Tar czf $ tarname./mysql-bin *
- Sleep 1 s
-
- # Ftp Info
- Ftphost = 111.111.11.11
- Ftpname = xxx
- Ftppsw = psw
- Cd $ TEMPDIR
- Ftp-I-n <!
- Open $ ftphost
- User $ ftpname $ ftppsw
- Type binary
- Put $ tarname
- Close
- Bye
- !
- Sleep 10 s
- Echo"End upload"
- Cd $ TEMPDIR
- Mv $ BINLOGNAME * $ BAKDIR
- Rm-r $ tarname
- 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.