To perform incremental backup of the MySQL database, you just need to modify the database configuration file /etc/my.cnf to enable the bin-log function. Next, I referred to some methods on the Internet and wrote it myself. The main thing was to learn some of his ideas and methods. #function: MYSQL incremental backup #version: 1.0.0 #author:wangyanlin #date: 2017/08/02 #----------------------------------------------------------------------------------------------- #!/bin/sh export LANG=en_US.UTF-8 #Set time DATE=`date +%Y%m%d` #Set information USER=root PASSWORD=withub #Set the path cd / /usr/bin/mkdir -p mysql_bak/daily /usr/bin/mkdir -p mysql_bak/logs BakDir=/mysql_bak/daily BinDir=/var/lib/mysql LogFile=/mysql_bak/logs/Daily_$DATE.log BinlogFile=/var/lib/mysql/mysql-bin.index /usr/bin/mysqladmin -u$USER -p$PASSWORD flush-logs #Flush log Counter=`wc -l $BinlogFile | awk '{print $1}'` NextNum=0 start_time=`date +'%Y-%m-%d %H:%M:%S'` echo `date +"%Y year%m month%d day%H:%M:%S"` $Next1 Bakup start! >> $LogFile #This for loop is used to compare the two values of $Counter and $NextNum to determine whether the file exists or is the latest. for file in `cat $BinlogFile` do base=`basename $file` #basename is used to intercept the mysql-bin.00000* file name and remove the ./ in front of ./mysql-bin.000005 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 detect whether the target file exists. If it exists, write exist! to $LogFile. then echo $base exist! >> $LogFile else cp $BinDir/$base $BakDir echo $base copying >> $LogFile fi fi done echo `date +"%Y year%m month%d day%H:%M:%S"` $Next2 Bakup succ! >> $LogFile end_time=`date +'%Y-%m-%d %H:%M:%S'` start_seconds=$(date --date="$start_time" +%s); end_seconds=$(date --date="$end_time" +%s); echo "This backup running time: "$((end_seconds-start_seconds))"s" >> $LogFile Add a scheduled task: crontab -e 00 03 * * * /root/MySQL_incrementbak.sh #Start the incremental backup at 3:00 a.m. every day The effect of logs printing is as follows: PS: Let's take a look at the mysql full and incremental backup scripts Full amount: [root@master leo]# cat DBfullBak.sh #!/bin/bash #use mysqldump to fully backup mysql data BakDir=/root/leo/full LogFile=/root/leo/full/bak.log Date=`date +%Y%m%d` Begin=`date +"%Y year%m month%d day%H:%M:%S"` cd $BakDir DumpFile=$Date.sql GZDumpFile=$Date.sql.tgz mysqldump -uroot -p123456 --all-databases --lock-all-tables --routines --triggers --events --master-data=2 --flush-logs > $DumpFile tar zcvf $GZDumpFile $DumpFile rm $DumpFile Last=`date +"%Y year%m month%d day%H:%M:%S"` echo Start: $Begin End: $Last $GZDumpFile succ >> $LogFile Parameter Notes: --all-databases #Backup all databases --lock-all-tables #Add read locks to all tables --routinge #Stored procedures and functions --triggers #Trigger --events #Record events --master-data=2 #Record the current binary log position in the backup file and annotate it. 1 is not commented out and is meaningful in master-slave replication --flush-logs #Roll the log once The results are as follows: [root@master full]# ls 20140728.sql.tgz bak.log [root@master full]# cat bak.log Start: 2014-07-28 19:02:59 End: 2014-07-28 19:02:59 20140728.sql.tgz succ Start: 2014-07-28 19:12:01 End: 2014-07-28 19:12:01 20140728.sql.tgz succ [root@master full]# Incremental backup: [root@master leo]# cat DBDailyBak.sh #!/bin/bash BakDir=/root/leo/binlog/ BinDir=/var/lib/mysql LogFile=/root/leo/binlog/bak.log BinFile=/var/lib/mysql/mysql-bin.index mysqladmin -uroot -p123456 flush-logs Counter=`wc -l $BinFile|awk '{print $1}'` NextNum=0 for file in `cat $BinFile` do base=`basename $file` NextNum=`expr $NextNum + 1` if [ $NextNum -eq $Counter ] then echo $base skip! >> $LogFile else dest=$BakDir/$base if(test -e $dest) then echo $base exist! >> $LogFile else cp $BinDir/$base $BakDir/ echo $base copying >> $LogFile fi fi done echo `date +"%Y year%m month%d day%H:%M:%S"` $Next Bakup succ~ >> $LogFile Summarize The above is the idea and method of incremental backup of MySQL database introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! You may also be interested in:
|
<<: Troubleshooting process for Docker container suddenly failing to connect after port mapping
>>: Vue-CLI multi-page directory packaging steps record
<template> <div class="app-containe...
What is the function of this key attribute? Let’s...
Table of contents Preface 1. What is a lock? 2. L...
Docker download address: http://get.daocloud.io/#...
Maybe some people have not come across this issue ...
for loop Basic syntax format: for(initialize vari...
Table of contents 1. Overview 2. Parameters for c...
Preface: When using MySQL, you may encounter time...
The Linux operating system has revolutionized the...
I knew before that to synchronously obtain the re...
1. How MySQL uses indexes Indexes are used to qui...
Table of contents Preface first step: Step 2: Mod...
Table of contents 1. Current limiting algorithm 2...
1. Select Edit → Virtual Network Editor in the me...
Introduction Part 1: Written at the beginning One...