MySQL 备份和恢复策略(二)
备份策略三、使用mysqldump备份数据库mysqldump 是采用SQL级别的备份机制,它将数据表导成 SQL 脚本文件,在不同的 MySQL 版本之间升级时相对比较合适,这也是最常用的备份方法。mysqldump 比直接拷贝要慢些。关于mysqldump的更详细解释见最后的附录。对于中等级别业务量的系统来说,备份策略可以这么定:第一次完全备份,每天一次增量备份,每周再做一次完全备份,如此一直重复。而对于重要的且繁忙的系统来说,则可能需要每天一次全量备份,每小时一次增量备份,甚至更频繁。为了不影响线上业务,实现在线备份,并且能增量备份,最好的办法就是采用主从复制机制(replication),在 slave 机器上做备份。备份策略布置:(1)、创建备份目录Shell> mkdir /tmp/mysqlbackupShell> mkdir /tmp/mysqlbackup/daily(2)、启用二进制日志采用 binlog 的方法相对来说更灵活,省心省力,而且还可以支持增量备份。启用 binlog 时必须要重启 mysqld。首先,关闭 mysqld,打开 /etc/my.cnf,加入以下几行:[mysqld]log-bin然后启动 mysqld 就可以了。运行过程中会产生 HOSTNAME-bin.000001 以及 HOSTNAME-bin.index,前面的文件是 mysqld 记录所有对数据的更新操作,后面的文件则是所有 binlog 的索引,都不能轻易删除。关于 binlog 的更详细信息请查看手册。(3)、配置SSH密钥登录,用于将MySQL备份传送到备份服务器(如果备份服务器为Windows,请跳过此部)。1)、在MySQL所在服务器(192.168.0.20)生成SSH密钥[root@lab ~]# ssh-keygen -t rsaGenerating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa): //直接回车Enter passphrase (empty for no passphrase): //直接回车,不使用密码Enter same passphrase again: //直接回车,不使用密码Your identification has been saved in /root/.ssh/id_rsa.Your public key has been saved in /root/.ssh/id_rsa.pub.The key fingerprint is:c2:96:9f:2d:5a:8e:08:42:43:35:2f:85:5e:72:f8:1c root@lab 2)、在备份服务器(192.168.0.200)上创建目录,修改权限,并传送公钥。[root@lab ~]# ssh 192.168.0.200 "mkdir .ssh;chmod 0700 .ssh"The authenticity of host '192.168.0.200 (192.168.0.200)' can't be established.RSA key fingerprint is 37:57:55:c1:32:f1:dd:bb:1b:8a:13:6f:89:fb:b8:9d.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '192.168.0.200' (RSA) to the list of known hosts.root@192.168.0.200's password: //输入备份服务器的root密码[root@lab ~]# scp .ssh/id_rsa.pub 192.168.0.200:.ssh/authorized_keys2root@192.168.0.200's password: id_rsa.pub 100% 218 0.2KB/s 00:00 3)、测试SSH登录[root@lab ~]# ssh 192.168.0.200 //测试SSH登录Last login: Fri Nov 16 10:34:02 2007 from 192.168.0.20[root@lib ~]# (4)、设置crontab任务,每天执行备份脚本shell> crontab -e#每个星期日凌晨3:00执行完全备份脚本0 3 * * 0 /root/MySQLBackup/mysqlFullBackup.sh >/dev/null 2>&1#周一到周六凌晨3:00做增量备份0 3 * * 1-6 /root/MySQLBackup/mysqlDailyBackup.sh >/dev/null 2>&1 mysqlFullBackup.sh注释:#!/bin/sh# Name:mysqlFullBackup.sh# PS:MySQL DataBase Full Backup.# Write by:i.Stone# Last Modify:2007-11-17## Use mysqldump --help get more detail.## 定义变量,请根据具体情况修改# 定义脚本目录scriptsDir=`pwd`# 定义数据库目录mysqlDir=/usr/local/mysql# 定义用于备份数据库的用户名和密码user=rootuserPWD=111111# 定义备份目录dataBackupDir=/tmp/mysqlbackup# 定义邮件正文文件eMailFile=$dataBackupDir/email.txt# 定义邮件地址eMail=alter@somode.com# 定义备份日志文件logFile=$dataBackupDir/mysqlbackup.logDATE=`date -I` echo "" > $eMailFileecho $(date +"%y-%m-%d %H:%M:%S") >> $eMailFilecd $dataBackupDir# 定义备份文件名dumpFile=mysql_$DATE.sqlGZDumpFile=mysql_$DATE.sql.tar.gz # 使用mysqldump备份数据库,请根据具体情况设置参数$mysqlDir/bin/mysqldump -u$user -p$userPWD \--opt --default-character-set=utf8 --extended-insert=false \--triggers -R --hex-blob --all-databases \--flush-logs --delete-master-logs \--delete-master-logs \-x > $dumpFile # 压缩备份文件if [[ $? == 0 ]]; then tar czf $GZDumpFile $dumpFile >> $eMailFile 2>&1 echo "BackupFileName:$GZDumpFile" >> $eMailFile echo "DataBase Backup Success!" >> $eMailFile rm -f $dumpFile # Delete daily backup files. cd $dataBackupDir/daily rm -f * # Delete old backup files(mtime>2). $scriptsDir/rmBackup.sh # 如果不需要将备份传送到备份服务器或备份服务器为Windows,请将标绿的行注释掉# Move Backup Files To Backup Server.#适合Linux(MySQL服务器)到Linux(备份服务器) $scriptsDir/rsyncBackup.sh if (( !$? )); then echo "Move Backup Files To Backup Server Success!" >> $eMailFile else echo "Move Backup Files To Backup Server Fail!" >> $eMailFile fi else echo "DataBase Backup Fail!" >> $emailFilefi# 写日志文件echo "--------------------------------------------------------" >> $logFilecat $eMailFile >> $logFile# 发送邮件通知cat $eMailFile | mail -s "MySQL Backup" $eMail本文出自 51CTO.COM技术博客 |