MySQL中的增量备份

时间:2010-07-15 07:32:30

标签: mysql backup

  

可能重复:
  What is the best way to do Incremental backups in Mysql?

有人可以帮我在MySQL中进行增量备份吗?

1 个答案:

答案 0 :(得分:8)

检查MySQL :: MySQL 5.1 Reference Manual :: 6.2 Database Backup Methods页面并阅读“通过启用二进制日志进行增量备份”部分。我们不能说清楚:)

如果网站出现故障:

  

通过启用二进制日志进行增量备份

     

MySQL支持增量备份:您必须启动服务器   --log-bin选项用于启用二进制日志记录;请参见第5.2.4节“   二进制日志“。二进制日志文件为您提供信息   需要将更改复制到后续生成的数据库   您执行备份的时间点。目前你想要的   进行增量备份(包含自那以后发生的所有更改)   最后一次完整备份或增量备份),您应该旋转二进制日志   通过使用FLUSH LOGS。完成后,您需要复制到备份   定位所有二进制日志,其范围从一个时刻   上次完整或增量备份到最后一个但是一个。这些二进制日志   是增量备份;在恢复时,您将它们应用为   在第7.5节“使用时间点(增量)恢复”中进行了解释   二进制日志“。下次执行完整备份时,您也应该这样做   使用FLUSH LOGS,mysqldump --flush-logs或转动二进制日志   mysqlhotcopy --flushlog。请参见第4.5.4节“mysqldump - 数据库”   备份程序“,以及第4.6.9节”,mysqlhotcopy - 数据库备份   程序”。

另一个选择是使用MySQLDump,并将新的dumpfile版本与旧的(呃)版本区分开,然后只存储diff。请参阅MySQLDump comment section上的实施示例。

对于后代,脚本包含在下面。

#!/bin/sh
# Incremental backup script using rdiff
# Author: Driantsov Alexander
# Requirements:
# * rdiff-backup - http://www.nongnu.org/rdiff-backup/
# * rsync
# * ssh ;)
####

BACKUP_ADMIN_EMAIL="yourmail@yourserver.com"
BACKUP_MAIL_SUBJECT="`hostname`: SQL Backup Synchronization Result"
BACKUP_LOG="/var/log/backup-sql"

BACKUP_PRIO="20" # Priority for the MySQL dump and rdiff-backup Min: 20 Max: -20
BACKUP_TMP_DIR="/var/backup/mysql_tmp" # New dumps will be stored here
BACKUP_DIFF_DIR="/var/backup/hosting/mysql" # Diffs of dumps will be stored there
SYNC_SRV="BAC.KUP.SER.VER" # Remote server for backup storage
SYNC_USER="backup_user" # User at remote storage
SYNC_SPEED="200" # Limit Synchronization Bandwidth to this number of KB/s
SYNC_DIR="/backup/hosting/mysql" #Directory on Remote server to synchronize backups in
MYSQL_USER="admin" # MySQL user
MYSQL_PASSWD=`cat /etc/psa/.psa.shadow` # Password for MySQL. You may obtain password from /etc/psa/.psa.shadow if you are using Plesk on your server.

#Implementaition
RSCONSTR="$SYNC_USER@$SYNC_SRV"
# Dump
echo "Backup Started at `date`" > $BACKUP_LOG
load_average=`uptime|awk {print $10" "$11" "$12}`
echo "Load overage at start: $load_average" >> $BACKUP_LOG
echo "\nBackingUP MySQL:" >> $BACKUP_LOG
for i in `mysql -u$MYSQL_USER -p$MYSQL_PASSWD -Bse "show databases"`; do echo "Backing up database $i..." >> $BACKUP_LOG ; nice -n $BACKUP_PRIO mysqldump --single-transaction --quick --skip-extended-insert -u$MYSQL_USER -p$MYSQL_PASSWD $i > $BACKUP_TMP_DIR/$i.dump; done

echo "MySQL dump completed at `date`" >> $BACKUP_LOG
load_average=`uptime|awk {print $10" "$11" "$12}`
echo "\nLoad overage after MySQL dumps: $load_average\n" >> $BACKUP_LOG
# Diff
echo "Looking for difference in database" >> $BACKUP_LOG
nice -n $BACKUP_PRIO rdiff-backup $BACKUP_TMP_DIR $BACKUP_DIFF_DIR/$HOSTNAME 2>&1 >> $BACKUP_LOG
echo "Done" >> $BACKUP_LOG

echo "Rdiff-backup completed at `date`" >> $BACKUP_LOG
load_average=`uptime|awk {print $10" "$11" "$12}`
echo "\nLoad overage after rdiff-backup: $load_average\n" >> $BACKUP_LOG

# Synchronize

sql_result=`rsync -avz --bwlimit $SYNC_SPEED $BACKUP_DIFF_DIR $RSCONSTR:$SYNC_DIR|tail -n 2`
free_space_info=`ssh $RSCONSTR df -h --sync -t ext3`

echo -en " MySQL backup synchronization:\n $sql_result \nSynchronization completed at `date` \n\nInformation on free space on remote backup server: \n $free_space_info \n\n Backup Log: \n\n`cat $BACKUP_LOG`"| mail -s "$BACKUP_MAIL_SUBJECT" $BACKUP_ADMIN_EMAIL