MySQL backup and recovery strategy (a)

In the case of a database table is missing or damaged, back up your database is very important. If a system crash occurs, you definitely want to be able to your table with minimal loss of data recovery to the state when the crash occurred as much as possible. This paper focuses on MyISAM tables do backup and recovery.

 

A backup strategy: directly copy the database file (not recommended)

Backup Strategy Two: Use mysqlhotcopy backup of the database (full backup for small database backup)

Backup Strategy Three: Using mysqldump database backup (Full + incremental backup, database backup for medium)

Backup Strategy 4: use master-slave replication (Replication) (achieve real-time database backup)

 

Script Download: Download script

 

 

A backup strategy, directly copy the database file

A direct copy of the data file is the most direct, fast, convenient, but the disadvantage is basically incremental backups can not be achieved. In order to ensure data consistency, you need to back up before the file, execute the following SQL statement:

FLUSH TABLES WITH READ LOCK;

That is, the data in memory is flushed to disk, and lock the data table, to ensure that the copy process, there will be no new data is written. This approach backed up data recovery is very simple, you can directly copy the lower back to the original database directory.

 

Backup Strategy II, using mysqlhotcopy backup database

mysqlhotcopy  is a PERL program, originally written by Tim Bunce. It uses LOCK TABLES, FLUSH TABLES and cp or scp to quickly back up the database. It is the fastest way to back up the database or single tables, but it can only run on the machine database files (including data table definition files, data files, index files) is located , and mysqlhotcopy  only be used for backup MyISAM tables.

This backup strategy is suitable for small database backup, the amount of data, you can use mysqlhotcopy program to conduct a full backup every day.

Backup strategy layout:

(1), mounted DBD-mysql perl module, connected to the support mysqlhotcopy script MySQL database.

shell> tar -xzvf  DBD-mysql-4.005.tar.gz

shell> cd DBD-mysql-4.005

shell> unset LANG

shell> perl Makefile.PL -mysql_config=/usr/local/mysql/bin/mysql_config -testuser=root -testpassword=UserPWD

shell> make

shell> make test

shell> make install

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

shell> crontab -e

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

3:00 am every day to perform the backup script.

 

mysqlbackup.sh Notes:

#!/bin/sh

# Name:mysqlbackup.sh

# PS:MySQL DataBase Backup,Use mysqlhotcopy script.

# Write by:i.Stone

# Last Modify:2007-11-15

#

# Define variables, tailored to the specific circumstances

# Custom Scripts directory

scriptsDir=`pwd`

Database data directory #

dataDir=/usr/local/mysql/data/

Data backup directory #

tmpBackupDir=/tmp/tmpbackup/

backupDir=/tmp/mysqlbackup/

# Used to back up the database user name and password

mysqlUser=root

mysqlPWD=111111

#Define eMail address

[email protected]


# If the temporary backup directory exists, emptied it, if it does not exist, create

if [[ -e $tmpBackupDir ]]; then

  rm -rf $tmpBackupDir/*

else

  mkdir $tmpBackupDir

be

# If the backup directory does not exist, create it

if [[ ! -e $backupDir ]];then

  mkdir $backupDir

fi
# Empty MySQLBackup.log

if [[ -s MySQLBackup.log ]]; then

  cat /dev/null >MySQLBackup.log

fi
# get a list of database backups, where you can filter the database do not want to back up

for databases in `find $dataDir -type d | \

  sed -e “s/\/usr\/local\/mysql\/data\///” | \

  sed -e “s/test//”`; do
  if [[ $databases == "" ]]; then

    continue

  else

# backup database

    /usr/local/mysql/bin/mysqlhotcopy –user=$mysqlUser –password=$mysqlPWD -q “$databases” $tmpBackupDir

    dateTime=`date “+%Y.%m.%d %H:%M:%S”`

    echo “$dateTime Database:$databases backup success!” >>MySQLBackup.log

  be

DONE
# compressed backup file

date=`date -I`

cd $tmpBackupDir

CZF $ backupDir tar / mysql- $ date.tar.gz ./
# Send e-mail notification

if [[ -s MySQLBackup.log ]]; then

  cat MySQLBackup.log | mail -s “MySQL Backup” $eMail

fi
# Use smbclientmv.sh upload script database backup to the backup server

# $scriptsDir/smbclientmv.sh

smbclientmv.sh comments

#!/bin/sh

# Name:smbclientmv.sh

# PS:Move the data to Backup Server.

# Write by:i.Stone

# Last Modify:2007-11-15

#

# Define the variable

# Backup server name

BackupServer=”BackupServerName”

# Shared folder name

BackupShare=”ShareName”

User name and password to access the backup server #

BackupUser=”SMBUser”

BackupPW=”SMBPassword”

# Define a backup directory

BackupDir=/tmp/mysqlbackup

date=`date -I`
# Move the data to BackupServer

smbclient //$BackupServer/$BackupShare \

$BackupPW -d0 -W WORKGROUP -U $BackupUser \

-c “put $BackupDir/mysql-$date.tar.gz \

mysql-$date.tar.gz”
# Delete temp files

rm -f $BackupDir/mysql-$date.tar.gz

(3), to restore the state of the database backup
mysqlhotcopy back out of the entire database directory, can be copied directly to the use mysqld DATADIR specified (in this case, / usr / local / mysql / data /) directory can, at the same time permission to pay attention to the problem in the following example:
the shell> CP db_name -rf / usr / local / MySQL / Data /
the shell> chown -R & lt MySQL: MySQL / usr / local / MySQL / Data / (the owner of the directory into db_name mysqld run user)
this set of backup strategy can only be restored to the state when the last backup of the database, to be lost in the time of the crash data as little as possible should be backed up more frequently, in order to restore the data to the collapse of the state from using the master replication (replication).

 

This switched: http://blog.thematice.com  Author: porridge country

Reproduced in: https: //www.cnblogs.com/lvsong/archive/2010/07/30/1788895.html

Guess you like

Origin blog.csdn.net/weixin_34037977/article/details/93774410