(Transfer) MySQL data backup using mysqldump

Reprinted from http://www.cnblogs.com/feichexia/p/MysqlDataBackup.html

 

mysqldump is often used for logical backups of MySQL databases.

 

1. Various usage instructions

     A. The simplest usage:

mysqldump -uroot -pPassword [database name] 
> [dump file]

     The above command will back up the specified database to a dump file (dump file), for example:

mysqldump -uroot -p123 test > test.dump

     The generated test.dump file contains the table building statement (generating the database structure) and the insert statement for inserting data.

 

     B. --opt

     If the --opt parameter is added, the generated dump file is slightly different:

     . The table creation statement includes drop table if exists tableName

     . Before insert contains a lock table statement lock tables tableName write, after insert contains unlock tables


     C. Cross-host backup

     Use the following command to copy the sourceDb on host1 to the targetDb on host2, provided that the targetDb database has been created on the host2 host:

mysqldump --host=host1 --opt sourceDb| mysql --host=host2 -C targetDb

     -C instructs data transfer between hosts to use data compression

 

     D. Back up only the table structure

mysqldump --no-data --databases mydatabase1 mydatabase2 mydatabase3 > test.dump

     Only the table structure will be backed up. --databases indicates the databases on the host to back up. If you want to back up all databases on a MySQL host, you can use the --all-databases option, as follows:

mysqldump --all-databases
> test.dump

 

     E. Restore the database from the backup file

mysql [database name] < [backup file name]

 

2. Combined with Linux cron command to achieve regular backup

    For example, if you need to back up all databases on a host at 1:30 am every day and compress the dump file into gz format, you can add the following line of code to the /etc/crontab configuration file:

30 1 * * * root mysqldump -u root -pPASSWORD --all-databases | gzip > /mnt/disk2/database_`date '+%m-%d-%Y'`.sql.gz

    The first five parameters represent minutes, hours, days, months, and years, respectively, and asterisks represent any. date '+%m-%d-%Y' gets the current date in MM-DD-YYYY format.

 

3, a complete shell script backup MySQL database example

复制代码
# vi / backup / backup. sh

#!bin/bash
cd /backup
echo "You are in backup dir"
mv backup* /oldbackup
echo "Old dbs are moved to oldbackup folder"
File = backup-$Now.sql
mysqldump -u user -p password database-name > $File
echo "Your database backup successfully completed"
复制代码

     上面脚本文件保存为backup.sh,并且系统中已经创建两个目录/olcbackup和/backup。每次执行backup.sh时都会先将/backup目录下所有名称为backup开头的文件移到/oldbackup目录。

      为上述脚本制定执行计划如下:

#crontab -e
30 1 * * * /backup.sh

 

4、mysqldump全量备份+mysqlbinlog二进制日志增量备份

    从mysqldump备份文件恢复数据会丢失掉从备份点开始的更新数据,所以还需要结合mysqlbinlog二进制日志增量备份。确保my.ini或者my.cnf中包含下面的配置以启用二进制日志,或者mysqld ---log-bin:

[mysqld]
log-bin=mysql-bin

    mysqldump命令必须带上--flush-logs选项以生成新的二进制日志文件:

mysqldump --single-transaction --flush-logs --master-data=2 > backup.sql

    这样生成的增量二进制日志文件比如为mysql-bin.000003,那么恢复数据时如下:

1
2
shell> mysql -uroot -pPwd < backup_sunday_1_PM.sql
shell> mysqlbinlog mysql-bin.000003 | mysql -uroot -pPwd

   此外mysqlbinlog还可以指定--start-date、--stop-date、--start-position和--stop-position参数,用于精确恢复数据到某个时刻之前或者跳过中间某个出问题时间段恢复数据,直接摘录MySQL文档说明中相关内容如下:

复制代码
5.9.3.1. 指定恢复时间
对于MySQL 4.1.4,可以在mysqlbinlog语句中通过--start-date和--stop-date选项指定DATETIME格式的起止时间。举例说明,假设在今天上午10:00(今天是2005年4月20日),执行SQL语句来删除一个大表。要想恢复表和数据,你可以恢复前晚上的备份,并输入:
mysqlbinlog --stop-date="2005-04-20 9:59:59" /var/log/mysql/bin.123456 \
     | mysql -u root -pmypwd
该命令将恢复截止到在--stop-date选项中以DATETIME格式给出的日期和时间的所有数据。如果你没有检测到几个小时后输入的错误的SQL语句,可能你想要恢复后面发生的活动。根据这些,你可以用起使日期和时间再次运行mysqlbinlog:

mysqlbinlog --start-date="2005-04-20 10:01:00" /var/log/mysql/bin.123456 \
     | mysql -u root -pmypwd \
在该行中,从上午10:01登录的SQL语句将运行。组合执行前夜的转储文件和mysqlbinlog的两行可以将所有数据恢复到上午10:00前一秒钟。你应检查日志以确保时间确切。下一节介绍如何实现。

5.9.3.2. 指定恢复位置
也可以不指定日期和时间,而使用mysqlbinlog的选项--start-position和--stop-position来指定日志位置。它们的作用与起止日选项相同,不同的是给出了从日志起的位置号。使用日志位置是更准确的恢复方法,特别是当由于破坏性SQL语句同时发生许多事务的时候。要想确定位置号,可以运行mysqlbinlog寻找执行了不期望的事务的时间范围,但应将结果重新指向文本文件以便进行检查。操作方法为:
mysqlbinlog --start-date="2005-04-20 9:55:00" --stop-date="2005-04-20 10:05:00" \
      /var/log/mysql/bin.123456 > /tmp/mysql_restore.sql
该命令将在/tmp目录创建小的文本文件,将显示执行了错误的SQL语句时的SQL语句。你可以用文本编辑器打开该文件,寻找你不要想重复的语句。如果二进制日志中的位置号用于停止和继续恢复操作,应进行注释。用log_pos加一个数字来标记位置。使用位置号恢复了以前的备份文件后,你应从命令行输入下面内容:

mysqlbinlog --stop-position="368312" /var/log/mysql/bin.123456 \
    | mysql -u root -pmypwd 
 
mysqlbinlog --start-position="368315" /var/log/mysql/bin.123456 \
    | mysql -u root -pmypwd \ 
上面的第1行将恢复到停止位置为止的所有事务。下一行将恢复从给定的起始位置直到二进制日志结束的所有事务。因为mysqlbinlog的输出包括每个SQL语句记录之前的SET TIMESTAMP语句,恢复的数据和相关MySQL日志将反应事务执行的原时间。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326282381&siteId=291194637