Mysql full backup and Incremental Backup implementation method, mysql incremental

Source: Internet
Author: User

Mysql full backup and Incremental Backup implementation method, mysql incremental

Mysql full backup and Incremental backup. Enable the logbin log function of mysql. Add the following code to the/etc/my. cnf file:

[mysqld]log-bin = "/home/mysql/logbin.log"binlog-format = ROWlog-bin-index = "/home/mysql/logindex"binlog_cache_size=32mmax_binlog_cache_size=512mmax_binlog_size=512m

Restart mysql. The users and groups in the path/home/mysql should be changed to mysql.

2. Incremental Backup
Create the following directory in the/home/mysql/directory:

mkdir -p /home/mysql/backup/daily

Incremental backup script

Cd/home/mysqlvi binlogbak. sh #! /Bin/bashexport LANG = en_US.UTF-8BakDir =/home/mysql/backup/dailyBinDir =/home/mysqlLogFile =/home/mysql/backup/binlog. logBinFile =/home/mysql/logindex. indexmysqladmin-uroot-proot123 flush-logs # This is used to generate new mysql-bin.00000 * file Counter = 'wc-l $ BinFile | awk '{print $1} ''NextNum = 0 # this for loop is used to compare $ Counter, $ NextNum is used to determine whether the file exists or is up-to-date. For file in 'cat $ binfile' do base = 'basename $ file' # basename is used to intercept the mysql-bin.00000 * file name, remove. /mysql-bin.000005 front. /NextNum = 'expr $ NextNum + 1' if [$ NextNum-eq $ Counter] then echo $ base skip! >>$ LogFile else dest = $ BakDir/$ base if (test-e $ dest) # test-e is used to check whether the target file exists. if yes, write exist! Go to $ LogFile. Then echo $ base exist! >>$ LogFile else cp $ BinDir/$ base $ BakDir echo $ base copying >>$ LogFile fi fidoneecho 'date + "% Y % m % d % H: % M: % S "'bakup succ! >>$ LogFile

Grant binlogbak. sh execution permission

chmod a+x /home/mysql/binlogbak.sh

3. Full backup

Vi databak. sh #! /Bin/bashexport LANG = en_US.UTF-8BakDir =/home/mysql/backupLogFile =/home/mysql/backup/bak. logDate = 'date + % Y % m % d' Begin = 'date + "% Y % m month % dday % H: % M: % S "'CD $ BakDirDumpFile = $ Date. sqlGZDumpFile = $ Date. SQL. tgzmysqldump-uroot-proot123 -- all-databases -- flush-logs -- delete-master-logs -- single-transaction> $ DumpFiletar-czvf $ GZDumpFile $ DumpFilerm $ DumpFilecount = $ (ls-l *. tgz | wc-l) if [$ count-ge 5] thenfile = $ (ls-l *. tgz | awk '{print $9}' | awk 'nr = 1 ') rm-f $ filefi # retain only the database content in the past four weeks Last = 'date + "% Y % m month % d % H: % M: % S" 'echo start: $ Begin end: $ Last $ GZDumpFile succ> $ LogFilecd $ BakDir/dailyrm-f *

Grant databak. sh execution permission

chmod a+x /home/mysql/databak.sh

4. Enable scheduled tasks

Vi/etc/crontab # execute the full backup script 0 3 ** 0/home/mysql/databak at every Sunday. sh>/dev/null 2> & 1 # perform Incremental backup at from Monday to Saturday 0 3 ** 1-6/home/mysql/binlogbak. sh>/dev/null 2> & 1

Make the preceding scheduled task take effect

crontab /etc/crontab

View scheduled tasks

crontab -l

Complete.

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.