Ideas and methods for incremental backup of MySQL database

Ideas and methods for incremental backup of MySQL database

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:
  • How to implement scheduled backup of MySQL database
  • How to backup and restore the mysql database if it is too large
  • 8 ways to manually and automatically backup your MySQL database
  • Example of MySQL database backup function implemented by ThinkPHP framework
  • Navicat for MySQL scheduled database backup and data recovery details
  • Linux implements automatic and scheduled backup of MySQL database every day
  • Automatic backup of MySQL database using shell script
  • How to import and export MySQL database (backup and restore)
  • Mysql backup multiple database code examples

<<:  Troubleshooting process for Docker container suddenly failing to connect after port mapping

>>:  Vue-CLI multi-page directory packaging steps record

Recommend

MySQL 5.7 mysql command line client usage command details

MySQL 5.7 MySQL command line client using command...

Vue two-choice tab bar switching new approach

Problem Description When we are working on a proj...

Analysis of two implementation methods for adding static routing in Linux

Command to add a route: 1.Route add route add -ne...

How to call the browser sharing function in Vue

Preface Vue (pronounced /vjuː/, similar to view) ...

Pure CSS to achieve click to expand and read the full text function

Note When developing an article display list inte...

jQuery implements all shopping cart functions

Table of contents 1. Select All 2. Increase or de...

A brief discussion on CSS blocking merging and other effects

Non-orthogonal margins When margin is used, it wi...

Vue multi-page configuration details

Table of contents 1. The difference between multi...

Detailed tutorial on installing Protobuf 3 on Ubuntu

When to install If you use the protoc command and...

MySQL 5.7.23 installation and configuration graphic tutorial

This article records the detailed installation pr...