mysqlhotcopy + binlog mysql to achieve incremental backup

mysqlhotcopy just the process simple caching write and file copy, and backup resource-lot faster than mysqldump. Especially for large databases, it uses LOCK TABLES, FLUSH TABLES and cp or scp to quickly back up the database. It is the fastest way to back up a single database or table can only be run on the machine where the directory database. [warning] Note: mysqlhotcopy supports only MyISAM engine. [/ warning] 1. install dependencies mysqlhotcopy perl language is written, it is necessary to install the drive connection mysql perl of:
# yum install perl-DBI.x86_64 
# yum install perl-DBD-MySQL.x86_64
2 mysqlhotcopy common parameters:  --allowold exit if the target is not present (plus a suffix _old rename it) --addtodest incremental backup, new backup automatically overwrite the original data with respect allowold --checkpoint = db_name. tbl_name insertion checkpoint entry in the specified database table. --Debug Enable debug output. --Dryrun, -n Report actions without performing them. --Flushlog after locking all the tables refresh the log. Do not delete previous (renamed) after --keepold complete. --Method = command replication methods (cp or scp). --Noindices backup does not include all of the index files. This makes the backup smaller and faster. Can rebuild the index with myisamchk -rq later. --User = user_name, MySQL user name to use when connecting to the server -u user_name. --Password = password, used when connecting to the server -p password password. Note that the password value is not optional for this option, unlike other MySQL programs. --Port = port_num, TCP / IP port number -P port_num when connecting the local server. --Quiet, -q Be silent except for errors. --Regexp = expr database replication regular expression matching all database names given. --Socket = path, -S path connections for Unix socket file. --Suffix = suffix str replicated database name. --Tmpdir = path temporary directory (instead of / tmp). --resetmaster after locking all the tables reset after reset master.info --record_log_pos binary log --resetslave all tables specified record lock = db.table table slave and master information 3. Create a table recording slave and master information
CREATE TABLE `mysqlhotcopy_log_pos` (
 `host` varchar(60) NOT NULL,
 `time_stamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 `log_file` varchar(32) DEFAULT NULL,
 `log_pos` int(11) DEFAULT NULL,
 `master_host` varchar(60) DEFAULT NULL,
 `master_log_file` varchar(32) DEFAULT NULL,
 `master_log_pos` int(11) DEFAULT NULL,
 `relay_log_file` varchar(32) DEFAULT NULL,
 `relog_log_pos` int(11) DEFAULT NULL,
 PRIMARY KEY (`host`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
[warning] Note: mysqlhotcopy script does not record relay_log_file and relog_log_pos value, at the same time, nor will such information be written to the file. Behind the record relay log and pos value information and write to the file because I changed the mysqlhotcopy script content. [/ warning] 4. special user privileges
grant select, reload, lock tables on *.* to 'mysqlbackup'@'localhost' identified by 'www.ttlsa.com'; 
grant select, delete, update, insert on mysql.mysqlhotcopy_log_pos to 'mysqlbackup'@'localhost' identified by 'www.ttlsa.com';
5. Regular use If you want to spare some of the data may be used where there is a regular. 5.1 database names match, such as: back up to the library at the beginning of ttlsa, you can use:
# mysqlhotcopy --flushlog -u='mysqlbackup' -p='www.ttlsa.com' --regexp= ^ttlsa /backup/mysqlback
Backup to [af] library at the beginning, you can use:
# mysqlhotcopy --flushlog -u='mysqlbackup' -p='www.ttlsa.com' --regexp=^[a-f] /backup/mysqlback
Some tables 5.2 to back up a database: backup ttlsa_com library at the beginning of the user table:
# mysqlhotcopy --flushlog -u='mysqlbackup' -p='www.ttlsa.com' ttlsa_com./^user/ /backup/mysqlback
Backup ttlsa_com library at the beginning of user_log table except:
# mysqlhotcopy --flushlog -u='mysqlbackup' -p='www.ttlsa.com' ttlsa_com./~^user_log/ /backup/mysqlback
Backup ttlsa_com library to user_0, user_1, user_2 ......, beginning user_9 table:
# mysqlhotcopy --flushlog -u='mysqlbackup' -p='www.ttlsa.com' ttlsa_com./^\(user_[0-9]\)/ /backup/mysqlback
6. The information recording master and slave
# perl ./mysqlhotcopy -u mysqlbackup -p www.ttlsa.com -S /tmp/mysql.sock --record_log_pos=mysql.mysqlhotcopy_log_pos --keepold --record_log_pos2file --flushlog --regexp="[a-zA-Z0-9_-]" /backup/mysqlback/mysqlhotcopy_20131114_041307
Note: - record_log_pos2file I modified parameter is mysqlhotcopy added. Information is recorded as follows: 7. incremental backup implemented according to the above 6 master, slave recording information, to achieve incremental backup. mysqlhotcopy-1   mysqlhotcopy-2
# mysqlbinlog --start-position=POS BIN_LOG_FILE
8. simple backup script
#!/bin/bash

mysqlhotcopy="/usr/local/mysql/bin/mysqlhotcopy2"
user="mysqlbackup"
password="www.ttlsa.com"
socket="/tmp/mysql.sock"

backupdir="/backup/mysqlback"
datadir="mysqlhotcopy_`date +%Y%m%d_%I%M%S`"
echo $datadir
target="$backupdir/$datadir"
retention_days_local="5"

status=($(mysql -u$user -p${password} -S $socket -e "show slave status\G" --skip-column-names | egrep  "Slave_IO_Running|Slave_SQL_Running" | awk '{print $2}'))

if [ "${status[0]}" == "Yes" ] && [ "${status[1]}" == "Yes" ]; then
   mkdir -p $target
   $mysqlhotcopy -u $user -p $password -S $socket  --record_log_pos=mysql.mysqlhotcopy_log_pos --keepold --record_log_pos2file --flushlog --regexp="[a-zA-Z0-9_-]" $target
   find $backupdir -name "^mysqlhotcopy_*_*" -type d -mtime +${retention_days_local} |  xargs rm -rf
else
  echo "slave error"
fi
Reproduced please specify from time to live operation and maintenance : http://www.ttlsa.com/html/3689.html

Reproduced in: https: //my.oschina.net/766/blog/211112

Guess you like

Origin blog.csdn.net/weixin_34245749/article/details/91493025