MySQL backup and recovery strategy (b)

Backup strategy Third, use mysqldump database backup

mysqldump  is the use of SQL-level backup mechanism, which will lead into a data table SQL script file, relatively appropriate upgrades between different versions of MySQL, which is the most commonly used backup method. mysqldump  than a direct copy is slower. About mysqldump of a more detailed explanation, see the appendix.

For moderate levels of traffic systems, backup strategy can be so determined: the first full backup, incremental backup once a day, a week do a full backup, so repeated. As for the important and busy systems, you may need daily full backup, incremental backup once an hour, or even more often. In order not to affect the online business and achieve online backups, incremental backups and can be the best way is to use master-slave replication (replication), the slave machine to do backup.

Backup strategy layout:

 

(1) , create a backup directory

Shell> mkdir /tmp/mysqlbackup

Shell> mkdir /tmp/mysqlbackup/daily

(2) , enabled the binary log

The method using binlog relatively more flexible, worry and effort, but also supports incremental backups.

You must restart mysqld enabled binlog. First, close the mysqld, open  / etc / my.cnf , adding the following lines:

[mysqld]

log-bin

Then start mysqld on it. During operation will produce  HOSTNAME-bin .000001  and  HOSTNAME-bin .index , in front of the file is mysqld record all updates to the data operations, later in the file is all binlog index, can not be easily removed. About binlog  more detailed information, please see the manual.

(3), configure SSH Key, used to transfer MySQL backup to the backup server (if the backup server for Windows, skip this section).

1 ) to generate SSH keys where the MySQL server (192.168.0.20)

[root@lab ~]# ssh-keygen -t rsa

Generating public/private rsa key pair.

Which to File in the Save the Enter at The Key (/root/.ssh/id_rsa):  // Direct Enter 

Passphrase the Enter (empty for NO passphrase):  // Enter directly, without using a password        

Same, passphrase Again the Enter:  // Enter directly, without using a password                    

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 ), created on the backup server (192.168.0.200) directory, modify the permissions, and transmitting public key.

[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)? yes

Warning: Permanently added ’192.168.0.200′ (RSA) to the list of known hosts.

password [email protected]'s:  // Enter the backup server's root password    

[root@lab ~]# scp .ssh/id_rsa.pub 192.168.0.200:.ssh/authorized_keys2

[email protected]’s password: 

id_rsa.pub                                             100%  218     0.2KB/s   00:00    

3 ) Test SSH login

[root @ Lab ~] #  SSH 192.168.0.200        // test SSH login

Last login: Fri Nov 16 10:34:02 2007 from 192.168.0.20

[root@lib ~]# 

 

(4), set up crontab tasks, perform a daily backup script

shell> crontab -e

# Every Sunday at 3:00 perform a full backup script

0 3 * * 0 /root/MySQLBackup/mysqlFullBackup.sh >/dev/null 2>&1

# Monday to Saturday 3:00 am to do incremental backups

0 3 * * 1-6 /root/MySQLBackup/mysqlDailyBackup.sh >/dev/null 2>&1

 

mysqlFullBackup.sh Notes:

#!/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.

#

# Define variables, tailored to the specific circumstances

# Define scripts directory

scriptsDir=`pwd`

# Define the database directory

mysqlDir=/usr/local/mysql

# Define user name and password for the database backup

user=root

userPWD=111111

# Define a backup directory

dataBackupDir=/tmp/mysqlbackup

# 定义邮件正文文件

eMailFile=$dataBackupDir/email.txt

# 定义邮件地址

eMail=[email protected]

# 定义备份日志文件

logFile=$dataBackupDir/mysqlbackup.log

DATE=`date -I`


echo “” > $eMailFile

echo $(date +”%y-%m-%d %H:%M:%S”) >> $eMailFile

cd $dataBackupDir

# 定义备份文件名

dumpFile=mysql_$DATE.sql

GZDumpFile=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!” >> $emailFile

fi

# 写日志文件

echo “——————————————————–” >> $logFile

cat $eMailFile >> $logFile

# 发送邮件通知

cat $eMailFile | mail -s “MySQL Backup” $eMail

 

本文转自:http://blog.thematice.com  作者:稀饭的国度

转载于:https://www.cnblogs.com/lvsong/archive/2010/07/30/1788899.html

Guess you like

Origin blog.csdn.net/weixin_33766805/article/details/92959470