Linux mysql5.7 database full backup and incremental backup shell instance

Source: Internet
Author: User
Tags mkdir

I have previously written a script similar to mysql full and incremental backup, but it is too simple. Last night, I wrote an instance for mysql5.7 for the company's online server, which is relatively more optimized.

Note: This script is only applicable to small databases and is not suitable for large databases. After all, it is a logical backup method. If the database is too large to be imported, it will be slow. innobackupex is recommended for backup, including innobackupex used by my own server.

Full backup script:

#! /Bin/bash
# Bak mysql all
# V1.0
#2016.8.4
TIME = 'date + % Y % m % d _ % H % M % s'
LOGFILE = "/home/mysqlbak/mysql_full_bak_time.log"
MYSQL_BAKDIR = "/home/mysqlbak/full $ TIME"
USER = root
PASSWORD = xxxxxx
[! -D $ MYSQL_BAKDIR] & mkdir-p $ MYSQL_BAKDIR
[! -E $ LOGFILE] & touch $ LOGFILE
DATABASE = 'mysqlshow-u $ USER-p $ PASSWORD | grep-Ev "Data | info | per | sys" | grep-v + | awk '{print $2 }''
For bakmysqlname in $ DATABASE
Do
Mysqldump-u $ USER-p $ PASSWORD $ bakmysqlname -- skip-lock-tables> $ MYSQL_BAKDIR/$ TIME _ $ bakmysqlname. SQL
Tar zcf $ MYSQL_BAKDIR/Secrets $ MYSQL_BAKDIR/$ TIME _ $ bakmysqlname. SQL -- remove-files &>/dev/null
Done
Ret = $?
FINISHTIME = 'date + % Y % m % d _ % H % M % s'
 
[$ Ret-eq 0] & echo "Mysql fullbakup finished, time: $ FINISHTIME" >>$ LOGFILE | echo "Mysql fullbakup failed, please check time: $ FINISHTIME ">>$ LOGFILE

Incremental backup script:

#! /Bin/bash
# Bak incr for mysql
#2016.8.4
TIME = 'date + % Y % m % d _ % H % M % s'
DATADIR =/data/mysql
LOGFILE = "/home/mysqlbak/mysql_incr _ bak_time.log"
USER = root
PASSWORD = xxxxxx
MYSQL_BAKDIR =/home/mysqlbak/incr $ TIME
[! -D $ MYSQL_BAKDIR] & mkdir-p $ MYSQL_BAKDIR
[! -E $ LOGFILE] & touch $ LOGFILE
Mysqladmin-u $ USER-p $ PASSWORD flush-logs
TOTAL = 'ls $ DATADIR/mysql-bin. * | wc-L'
TOTAL = 'expr $ TOTAL-2'
For filetemp in 'ls $ DATADIR/mysql-bin. * | head-n $ total'
Do
Incrfilename = 'basename $ filetemp'
Cp $ filetemp $ MYSQL_BAKDIR/$ incrfilename
Tar zcf $ MYSQL_BAKDIR/javasincrfilename.tar.gz $ MYSQL_BAKDIR/$ incrfilename -- remove-files &>/dev/null
Done
Ret = $?
FINISHTIME = 'date + % Y % m % d _ % H % M % s'
[$ Ret-eq 0] & echo "Mysql incr bak finished, time: $ FINISHTIME" >>$ LOGFILE | echo "Mysql incr bak failed, please check. time: $ FINISHTIME ">>$ LOGFILE

Related Article

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.