MySQL full backup and Incremental Backup

Source: Internet
Author: User

Reference blog: http://lhflinux.blog.51cto.com/1961662/506467

Thank you very much for sharing this information!

The following is the script I modified based on the above blog!

1. Complete backup
VI full_mysqldump.sh
#! /Bin/sh
# Test by XXXXX
# Time info
Time = 'date + % Y % m % d % H'
Time1 = 'date + % Y % m % d \ % H: % m: % s'
# Dir_info
Back_dir = "/backup_db/MySQL"
File_dir = "/backup_db/MySQL"
# Mysql_back
Log =/backup_db/mysqlback. Log
Echo "$ time1 start to full backup"> $ log
Mysqldump-uroot -- flush-logs -- master-Data = 2 zabbix | gzip> $ back_dir/mysqlfull1_time. SQL .gz
Time2 = 'date + % Y % m % d \ % H: % m: % s'
Echo "$ time2 full backup successfully" >>$ log
Spendtime = $ (date + % s-d "$ time2")-$ (date + % s-d "$ time1 ")))
Echo "backup spend total time: $ spendtime seconds" >>$ log
# Get postion
Sleep 5S
Gunzip <$ back_dir/mysqlfull1_time. SQL .gz | sed-n "22 p" | awk-f '= |, |; ''{print $2, $4}'> $ file_dir/position
# Remove 7 days ago backup
Find $ back_dir-name "MySQL *. SQL .gz"-mtime + 7-exec RM {}\;
Echo "remove 7 days ago backup! ">>> $ Log

2. Perform Incremental backup. Perform Incremental Backup Based on the position obtained from the complete backup and the current position and binlog of the binlog server,
After the backup, save the current position and BINLOG to the position file, and retain the starting point of the next Incremental backup.
 
VI zl_backup.sh
#! /Bin/bash
#
# Test by xxxxxx
#
Date = 'date + % Y % m % d'
Olddate = 'date -- date = '7 Days ago '+ % Y % m % d'
Data_dir =/var/lib/MySQL
File_dir =/backup_db/MySQL
Backup_dir =/backup_db/mysqlincr
Backupsql = $ backup_dir/mysql_zlback _ $ date. SQL
Log =/backup_db/mysqlback. Log
Startbinlog = 'awk' {print $1} '$ file_dir/position'
Startposition = 'awk' {print $2} '$ file_dir/position'
Echo "Start BINLOG: $ startbinlog, start position: $ startposition" >>$ log
CD $ file_dir
Mysql-uroot-e "show Master Status \ G;" | awk '{print $2}'> nowposition
Stopbinlog = 'sed-n' 2P '$ file_dir/nowposition'
Stopposition = 'sed-n' 3p '$ file_dir/nowposition'
Echo "Stop BINLOG: $ stopbinlog, stop position: $ stopposition" >>$ log
Time1 = 'date + % Y % m % d \ % H: % m: % s'
Echo "$ time1 start to Incremental Backup"> $ log
If ["$ startbinlog" = "$ stopbinlog"]; then
Mysqlbinlog -- start-position = $ startposition -- stop-position = $ stopposition $ data_dir/$ startbinlog> $ backupsql
Else
Startline = 'awk "/$ startbinlog/{print Nr}" $ data_dir/mysqld-bin.index'
Stopline = 'wc-L $ data_dir/mysqld-bin.index | awk '{print $1 }''
For I in 'seq $ Startline $ stopline'
Do
BINLOG = 'sed-n "$ I" p $ data_dir/mysqld-bin.index | SED's/. \ // g''
Case "$ BINLOG" in
"$ Startbinlog ")
Mysqlbinlog -- start-position = $ startposition $ data_dir/$ BINLOG >>$ backupsql
;;
"$ Stopbinlog ")
Mysqlbinlog -- stop-position = $ stopposition $ data_dir/$ BINLOG> $ backupsql
;;
*)
Mysqlbinlog $ data_dir/$ BINLOG >>$ backupsql
Esac
Done
Fi

If [-F $ backupsql]; then
Gzip $ backupsql
Echo "$ stopbinlog $ stopposition"> $ file_dir/position
Rm-RF $ backup_dir/mysql_zlback_1_olddate.tar.gz
Time2 = 'date + % Y % m % d \ % H: % m: % s'
Spendtime = $ (date + % s-d "$ time2")-$ (date + % s-d "$ time1 ")))
Echo "$ time2 Incremental Backup finished! ">>> $ Log
Echo "Incremental Backup spend total time: $ spendtime seconds" >>$ log
Else
Echo "$ date Incremental Backup fail !!!! ">>> $ Log

3. Plan tasks well
Write the Incremental backup script and full backup script in the scheduled task according to the desired time, and perform regular Incremental backup.
Of course, you need to restore the data in sequence during restoration.

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.