Example of using Mysqlhotcopy backup data script in Linux

Source: Internet
Author: User
Tags create directory datetime mkdir mysql host install perl mysql backup

Mysqlhotcopy is simply a process of caching writes and file copying, consuming resources and backing up much faster than mysqldump. Especially suitable for large databases, but note that mysqlhotcopy only supports the MyISAM engine

1. Installing Perl

The code is as follows Copy Code
#yum-y Install perl perl-dbi

Perl support needs to be installed using mysqlhotcopy because Mysqlhotcopy is written in Perl

2. Install Dbd-mysql

The code is as follows Copy Code
#wget http://www.fcbu.com/upimages/soft/DBD-mysql-4.020.tar.gz
The code is as follows Copy Code
#tar XZVF dbd-mysql-4.020.tar.gz
The code is as follows Copy Code
#cd dbd-mysql-4.020
The code is as follows Copy Code
#perl makefile.pl--mysql_config=mysql_config
The code is as follows Copy Code
#make
The code is as follows Copy Code
#make Install
3. Backup sample
The code is as follows Copy Code
#mysqlhotcopy-u root-p password--addtodest database name 1 database name 2 backup directory

4. Recovery

Stop the MySQL service, copy the backup database directory to the appropriate MySQL data directory, use Chown to change the directory owner for the MySQL server running users, start the MySQL service

5. If the following error occurs when performing Perl makefile.pl--mysql_config=mysql_config compilation:

Can ' t locate extutils/makemaker.pm in @ INC (@INC contains:/usr/local/lib64/perl5/usr/local/share/perl5/usr/lib64/perl5/vendor_perl/usr/share/perl5/ Vendor_perl/usr/lib64/perl5/usr/share/perl5.) At makefile.pl Line 20.
BEGIN failed--compilation aborted at makefile.pl.

Workaround:

The code is as follows Copy Code

#yum-y Install Perl-extutils-makemaker
If you still can't, use the following two:
#yum-y Install perl-extutils-makemaker*
#yum-y Install perl-time-hires*

6.mysqldump and Mysqlhotcopy Backup shell script example, mysqlbackup.sh

The code is as follows Copy Code

#!/bin/bash

# enviroment
Path=/usr/local/sbin:/usr/bin:/bin

# Configure the Directory of Backup
Backupdir=/data/backup/mysql # mysql Backup dir
Save=4 #how many days to save lastest
host=192.168.10.32 # MySQL Host
Root=root # mysql Account
Rootpass= ' XXXXXX ' # mysql root password
dumplist= ' mysql database1 database2 database3 ' # Backup with mysqldump
Hotcopylist= ' Database4 ' # Backup with Mysqlhotcopy

# define
sqldir= $BACKUPDIR/sql/# mysqldump to SQL save path
hotcopydir= $BACKUPDIR/hotcopy/# mysqlhotcopy Copy files path
tmpdir= $BACKUPDIR/tmp # temporary Dir
Datetime= ' date-d now +%y-%m-%d_%h-%m ' # create date dir

# Create Directory
if [!-D $SQLDIR]; Then
Mkdir-p $SQLDIR
Fi

if [!-D $HOTCOPYDIR]; Then
Mkdir-p $HOTCOPYDIR
Fi

RM-RF $TMPDIR
Mkdir-p $TMPDIR

#-----Backup MySQL-----

# Backup with Database
For dbname in $DUMPLIST
Todo
Mysqldump-h$host-u$root-p$rootpass $dbname > $TMPDIR/$dbname. sql
Done

For dbname in $HOTCOPYLIST
Todo
Mysqlhotcopy-u $ROOT-P $ROOTPASS $dbname $TMPDIR
Done

# move Hotcopy dir
CD $HOTCOPYDIR
Mkdir-p $DATETIME
CD $DATETIME
For dbname in $HOTCOPYLIST
Todo
Mv.. /.. /tmp/$dbname./
Done

# Create MySQL Tar
CD $SQLDIR
Tar-czf $SQLDIR/mysql_backup. $DATETIME. tar.gz. /tmp
RM-FR $TMPDIR
Find $SQLDIR-type f-mtime + $SAVE | Xargs rm-f
Find $HOTCOPYDIR-type d-mtime + $SAVE | Xargs RM-RF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.