我们设置了MySQL(版本5.1),具有Master-Slave设置。
我们需要提出备份策略,我们将使用mysqldump
。
我们打算使用flushlogs
,master-date=2
和single-transaction
选项。
我只是想知道从哪个节点运行备份,Master或Slave更好?
答案 0 :(得分:4)
您应该在Slave上运行备份,这样Master不会以任何方式中断,Master可以保持在生产模式。
这是一个备份Slave上的mysql数据的脚本,只保留最后7个备份
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
您无法使用--master-data=2
,因为备份来自Slave而不是Master。如果升级MySQL 5.5,可以使用MYSQLDUMP_OPTIONS
中的--dump-slave
来捕获Master的二进制日志和SHOW SLAVE STATUS\G
中的位置。您将在转储的第23行找到--dump-slave
的输出。
如果您希望继续使用MySQL 5.1并且想要记录Master中的坐标,请使用此
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
如果要在Master上自动旋转二进制日志,只需在Master
上配置它[mysqld]
expire-logs-days=7
答案 1 :(得分:0)
以下是我多年前解决这个问题的方法:
#!/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;"
在从站上:使用show slave status命令验证您是否与主站同步。如果要重新同步到主服务器,请运行:
slave stop;
reset slave;
slave start;
您可能需要停止mysql,删除从属bin日志文件然后重新启动并运行上面的内容(这对我们来说很重要,因为我们有一个流氓应用程序扰乱了奴隶上的mysql配置)。
这一策略非常符合我们的目的。确保并测试以确保它符合您的需求。