简体   繁体   中英

MySQL: Backup strategy - Master or Slave?

We have a set up of MySQL (version 5.1) with Master-Slave setup. We need to propose a backup strategy and we are going to use mysqldump .

We intend to use flushlogs , master-date=2 and single-transaction options.

I just want to know from which node its better to run the backup from, Master or Slave?

You should be running your backups on the Slave so that the Master is not interrupted in any way and the Master can stay in Production mode.

Here is a script to backup mysql data on the Slave and keep only the last 7 backups

MYSQL_USER=root
MYSQL_PASS=rootpass
MYSQL_CONN="-u${MYSQL_USER} -p${MYSQL_PASS}"
BACKUP_HOME=/some/path/to/your/backups
BACKUP_TIME=`date +"%Y%m%d_%H%M%S"`
BACKUP_FILE=${BACKUP_HOME}/MySQLData_${BACKUP_TIME}.sql
BACKUP_GZIP=${BACKUP_FILE}.gz

MYSQLDUMP_OPTIONS="--single--transaction --routines --triggers"
#
# Stop Replication
# Dump the Data
# Resume Replication
#
mysql ${MYSQL_CONN} -ANe"STOP SLAVE"
mysqldump ${MYSQL_CONN} ${MYSQLDUMP_OPTIONS} -A | gzip > ${BACKUP_GZIP}
mysql ${MYSQL_CONN} -ANe"START SLAVE"

#
# Rotate Out Old Backups based on BACKUPS_TO_KEEP
#
cd ${BACKUP_HOME}
FILES_TO_DELETE=/tmp/BackupFilesToDelete.txt
BACKUPS_TO_KEEP=7
BACKUPS_ON_HAND=`ls MySQLData_*.gz | wc -l`
if [ ${BACKUPS_ON_HAND} -gt ${BACKUPS_TO_KEEP} ]
then
    (( DIFF = BACKUPS_ON_HAND - BACKUPS_TO_KEEP ))
    ls -l MySQLData_*.gz | head -${DIFF} > ${FILES_TO_DELETE}
    for FIL in `cat ${FILES_TO_DELETE}` ; do rm -f ${FIL} ; done
fi

You cannot use --master-data=2 because the backup is from the Slave and not the Master. If you upgrade MySQL 5.5, you could use --dump-slave in the MYSQLDUMP_OPTIONS to capture the Master's binary log and position from the SHOW SLAVE STATUS\\G . You will find the output of --dump-slave on line 23 of the dump.

If you wish to remain with MySQL 5.1 and you want to record the coordinates from the Master, use this

MYSQL_USER=root
MYSQL_PASS=rootpass
MYSQL_CONN="-u${MYSQL_USER} -p${MYSQL_PASS}"
BACKUP_HOME=/some/path/to/your/backups
BACKUP_TIME=`date +"%Y%m%d_%H%M%S"`
BACKUP_COOR=${BACKUP_HOME}/MySQLCoor_${BACKUP_TIME}.txt
BACKUP_FILE=${BACKUP_HOME}/MySQLData_${BACKUP_TIME}.sql
BACKUP_GZIP=${BACKUP_FILE}.gz

MYSQLDUMP_OPTIONS="--single--transaction --routines --triggers"
#
# Stop Replication
# Dump the Data
# Capture Master Coordinates
# Resume Replication
#
mysql ${MYSQL_CONN} -ANe"STOP SLAVE"
mysqldump ${MYSQL_CONN} ${MYSQLDUMP_OPTIONS} -A | gzip > ${BACKUP_GZIP}
mysql ${MYSQL_CONN} -Ae"SHOW SLAVE STATUS\G" > /tmp/SSS.txt
grep "Relay_Master_Log_File:" /tmp/SSS.txt  > ${BACKUP_COOR}
grep "Exec_Master_Log_Pos:"   /tmp/SSS.txt >> ${BACKUP_COOR}
mysql ${MYSQL_CONN} -ANe"START SLAVE"
#
# Rotate Out Old Backups based on BACKUPS_TO_KEEP
#
cd ${BACKUP_HOME}
FILES_TO_DELETE=/tmp/BackupFilesToDelete.txt
BACKUPS_TO_KEEP=7
BACKUPS_ON_HAND=`ls MySQLData_*.gz | wc -l`
if [ ${BACKUPS_ON_HAND} -gt ${BACKUPS_TO_KEEP} ]
then
    (( DIFF = BACKUPS_ON_HAND - BACKUPS_TO_KEEP ))
    ls -l MySQLData_*.gz | head -${DIFF} > ${FILES_TO_DELETE}
    for FIL in `cat ${FILES_TO_DELETE}` ; do rm -f ${FIL} ; done
fi

If you want to autorotate binary logs on the Master, just configure this on the Master

[mysqld]
expire-logs-days=7

Give it a Try !!!

Following is how I tackled this problem many moons ago:

#!/usr/bin/ksh
date=`date +%y%m%d`
mysql -u root db_name -e "flush tables with read lock;"
mysqldump -u root -pYrPass --add-drop-table --add-locks natl_inv > /path/to/backup/db_name$date
mysql -u root -e "reset master;"
mysql -u root db_name -e "unlock tables;"
mysql -u root –pYrPass db_name < /path/to/backup/db_name$date
mysql -u root -e "flush logs;"

On the slave(s): use show slave status command to verify you are in sync with the master. If you want to resync to the master, run:

slave stop;
reset slave;
slave start;

You may need to stop mysql, delete the slave bin log files then restart and run the above (this was important for us because we had a rogue app upsetting mysql configurations on the slaves).

This strategy served our purposes extremely well. Be sure and test out to make sure it fits your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM