1、备份命令:

# mysqldump -u'root'  -p'12345' -h 127.0.0.1 \

     -q --single-transaction -R -E \

     --lock-all-tables --flush-logs \

     --triggers --default-character-set=utf8 \

     -B test | gzip > test.`date +%F_%T`.sql

2、备份脚本:

# cat /dbback.sh

#!/bin/bash


datetime=`date +%F_%T`

dbname=(mysql test )

echo $datetime  > /backdb.log


for db in ${dbname[*]}

    do

echo "############################## db back $db  begin time is $(date +%F_%T) ##############################" >> /backdb.log



mysqldump -u'root'  -p'12345' -h 127.0.0.1 --lock-all-tables --flush-logs -q --single-transaction -R -E --triggers --default-character-set=utf8 -B ${db} | gzip  > /${db}.$(date +%F_%T).sql.gz



echo "############################## db back $db  end time is   $(date +%F_%T) ##############################" >> /backdb.log

sleep 2;

        echo "" >> /backdb.log

        echo "" >> /backdb.log

    done


sleep 5


find / -name "*.sql.gz" -type f -mtime +45  | xargs rm -rf

[root@ ~ ]#


3、命令说明:

--lock-all-tables 锁住全局表

--flush-logs      刷新产生一个新的binlog

-q, --quick   Don't buffer query, dump directly to stdout.不要缓存查询,直接转储到stdout。

--single-transaction 

                      Creates a consistent snapshot by dumping all tables in a

                      single transaction. Works ONLY for tables stored in

                      storage engines which support multiversioning (currently

                      only InnoDB does); the dump is NOT guaranteed to be

                      consistent for other storage engines. While a

                      --single-transaction dump is in process, to ensure a

                      valid dump file (correct table contents and binary log

                      position), no other connection should use the following

                      statements: ALTER TABLE, DROP TABLE, RENAME TABLE,

                      TRUNCATE TABLE, as consistent snapshot is not isolated

                      from them. Option automatically turns off --lock-tables.

                      通过将所有表倾销到一个统一的快照中,创建一个一致的快照

                      单笔交易。只用于存储在

                      存储引擎支持多版本(目前

                      只有InnoDB也);垃圾不能保证

                      其他存储引擎的一致性。而

                      -单交易转储在过程中,以确保

                      有效的转储文件(正确的表内容和二进制日志

                      位置),没有其他连接应使用以下

                      语句:更改表、下拉表、重命名表,

                      TRUNCATETABLE,一致的快照不是孤立的

                      从他们。选项自动关闭-锁定表。

-R, --routines      Dump stored routines (functions and procedures).

                    转储存储的例程(功能和程序)。

-E, --events        Dump events. 倾倒事件。

--triggers          Dump triggers for each dumped table.转储表的转储触发器。

--default-character-set=name Set the default character set.设置默认字符集。

-B, --databases     Dump several databases. Note the difference in usage; in

                      this case no tables are given. All name arguments are

                      regarded as database names. 'USE db_name;' will be

                      included in the output.

                    转储几个数据库。注意使用中的差异;在

                    这种情况下没有表。所有名称参数

                    视为数据库名称。”使用db_name;将

                    包括在输出

--master-data[=#]   This causes the binary log position and filename to be

                      appended to the output. If equal to 1, will print it as a

                      CHANGE MASTER command; if equal to 2, that command will

                      be prefixed with a comment symbol. This option will turn

                      --lock-all-tables on, unless --single-transaction is

                      specified too (in which case a global read lock is only

                      taken a short time at the beginning of the dump; don't

                      forget to read about --single-transaction below). In all

                      cases, any action on logs will happen at the exact moment

                      of the dump. Option automatically turns --lock-tables

                      off.

                    这将导致二进制日志位置和文件名

                    追加到输出。如果等于1,将打印它作为一个

                    更改主命令;如果等于2,该命令将

                    带有一个注释符号。此选项将打开

                    -锁定所有表,除非-单交易

                    指定的(在这种情况下,一个全局读锁是唯一的

                    在转储开始的时间很短;不要

                    忘记阅读-下面的单笔交易。总共

                    案件,任何对日志的行动将发生在精确的时刻

                    的转储。选项自动打开锁定表

                    关闭。