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