This article mainly introduces mysqlhotcopy for MySQL backup and its description of precautions in actual operations. We all know that there are three common methods to implement MySQL database backup, however, what we will introduce to you today is a relatively useful solution. The following is a detailed description.
Mysqlhotcopy backup
Mysqlhotcopy is a Perl script originally written and provided by Tim Bunce. It uses lock tables, flush tables, and cp or scp to quickly back up databases. It is the fastest way for MySQL to back up a database or a single table, but it can only run on the machine where the database directory is located. Mysqlhotcopy is only used to back up MyISAM. It runs in Unix and NetWare
For usage instructions, see the following script. Add it to crotab.
#! /Bin/sh
# Name: mysqlbackup. sh
# PS: MySQL DataBase Backup, Use mysqlhotcopy script.
# Last Modify: 2008-06-12
# Define variables. Modify the variables as needed
# Define the directory where the script is located
ScriptsDir = 'pwd'
# Database Data Directory
DataDir =/var/lib/mysql
# Data MySQL Backup Directory
TmpBackupDir =/tmp/mysqlblackup
BackupDir =/backup/mysql
# Username and password used to back up the database
MysqlUser = root
MysqlPWD = 'you password'
# If the temporary Backup directory exists, clear it. If it does not exist, create it.
If [[-e $ tmpBackupDir]; then
Rm-rf $ tmpBackupDir /*
Else
Mkdir $ tmpBackupDir
Fi
# Create a backup directory if it does not exist
If [[! -E $ backupDir]; then
Mkdir $ backupDir
Fi
# Obtain the database backup list, where you can filter databases that do not want to be backed up
For databases in 'Find $ dataDir-type d | \
Sed-e "s/\/var \/lib \/mysql \ //" | \
Sed-e "s/test //" '; do
If [[$ databases = ""]; then
Continue
Else
# MySQL backup database
/Usr/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
Fi
Done
# Compressing backup files
Date = 'date-I'
Cd $ tmpBackupDir
Tar czf $ backupDir/mysql-‑date.tar.gz ./
# End completed
Add to crontab to set to run 5 times a week
0 0 ** 5/backup/blackup. sh
Note: Restore the database to the backup status
Mysqlhotcopy backs up the entire database Directory, which can be copied directly to the datadir (/var/lib/mysql/) directory specified by mysqld, pay attention to permission issues as follows:
Shell> cp-rf db_name/var/lib/mysql/
Shell> chown-R mysql: mysql/var/lib/mysql/(change the owner of the db_name directory to the mysqld running user)
This backup policy can only restore the database to the state of the last backup. If you want to lose as little data as possible during the crash, MySQL backup should be performed more frequently, to restore data to the state of the crash, use the master-slave replication mechanism (replication ).
TIPS:
If you do not want to write the password in shell, you can create a. my. cnf file in the root home directory so that mysqlhotcopy can read the username/password from it.
[Mysqlhotcopy]
User = root
Password = YourPassword
Then, for security reasons, chmod.
Chmod 600 ~ /. My. cnf
Appendix: mysqlhotcopy common parameters:
Allowold does not give up if the target exists (add a _ old suffix to rename it ).
Checkpoint = db_name.tbl_name Insert the checkpoint entry in the specified db_name.tbl_name.
-Debug enables debugging output.
Dryrun,-n reports actions without executing them.
Flushlog refresh the log after all the tables are locked.
After keepold is completed, the previous (renamed) targets are not deleted.
Method = command COPY method (cp or scp ).
The noindices backup does not include all index files. In this way, the backup is smaller and faster. You can use myisamchk-rq to re-build the index later.
Password = password,-p password the password used to connect to the server. Note that the password value of this option is optional, unlike other MySQL programs.
Port = port_num and-P port_num are the TCP/IP port numbers used to connect to the local server.
Quiet and-q remain silent except when an error occurs.
Regexp = expr: copy the database that matches the regular expression given by all database names.
Socket = path,-S path is used to connect Unix socket files.
Suffix = suffix of the database name copied by str.
Tmpdir = path temporary directory (instead of/tmp ).
User = user_name,-u user_name is the MySQL user name used to connect to the server.
Mysqlhotcopy reads the [client] and [mysqlhotcopy] consumer groups from the option files. To execute mysqlhotcopy, you must be able to access the table files backed up by MySQL, and have SELECT and RELOAD permissions for those TABLES (so that you can execute flush tables ).