基本思路是利用mysqldump导出到sql文件,然后利用rar加密压缩利用FTP上传到远程FTP空间。
Linux 下的Rar请到http://www.rarlab.com/ 下载。我的环境是CentOS
- #!/bin/bash
- FILENAME=$(date +%Y%m%d)
- echo time: $FILENAME
- #exit
- echo exporting
- /usr/local/mysql/bin/mysqldump --all-databases --quick --add-drop-table --socket=/tmp/mysql.sock --allow-keywords --complete-insert --compress > mysql.db.backup.sql
- echo compressing
- rar a "./backup/${FILENAME}_db_backup.rar" mysql.db.backup.sql -pYOURPASSWORD
- cd backup
- echo Uploading
- ftp -n<<!
- open FTPADDRESS
- user FTPUSER FTPPASSWORD
- cd backup
- put ${FILENAME}_db_backup.rar ${FILENAME}_backup.bak
- quit
- !
- echo Backup Successful
顺便放一下MySQL的常用参数:
格式 | 可选 | 描述[Chinese] | 描述[English] | Introduction |
---|---|---|---|---|
--add-drop-database | add-drop-database | 每个CREATE DATABASE语句前添加DROP DATABASE语句 | Add a DROP DATABASE statement before each CREATE DATABASE statement | |
--add-drop-table | add-drop-table | 每个CREATE TABLE语句前添加DROP TABLE语句 | Add a DROP TABLE statement before each CREATE TABLE statement | |
--add-drop-trigger | add-drop-trigger | 每个CREATE TRIGGER语句前添加DROP TRIGGER语句 | Add a DROP TRIGGER statement before each CREATE TRIGGER statement | 5.1.47-ndb-7.1.8 |
--add-locks | add-locks | 在每个表导出之前增加LOCK TABLES并且之后UNLOCK TABLE。 | Surround each table dump with LOCK TABLES and UNLOCK TABLES statements | |
--all-databases | all-databases | 导出所有数据库中的所有表 | Dump all tables in all databases | |
--all-tablespaces | all-tablespaces | Adds to a table dump all SQL statements needed to create any tablespaces used by an NDB Cluster table | 5.1.6 | |
--allow-keywords | allow-keywords | 允许创建关键字列名。应在每个列名前面加上表名前缀。 | Allow creation of column names that are keywords | |
--bind-address=ip_address | bind-address | 在具有多个网络接口的计算机,这个选项可以用来选择哪个接口连接到MySQL服务器时采用。 | Use the specified network interface to connect to the MySQL Server | 5.1.22-ndb-6.3.4 |
--comments | comments | 如果设置为 0,禁止转储文件中的其它信息,例如程序版本、服务器版本和主机。--skip—comments与---comments=0的结果相同。 默认值为1,即包括额外信息。 | Add comments to the dump file | |
--compact | compact | 产生少量输出。该选项禁用注释并启用--skip-add-drop-tables、--no-set-names、--skip-disable-keys和--skip-add-locking选项。 | Produce more compact output | |
--compatible=name[,name,...] | compatible | 产生与其它数据库系统或旧的MySQL服务器更兼容的输出。值可以为ansi、mysql323、mysql40、postgresql、oracle、mssql、db2、maxdb、no_key_options、no_tables_options或者no_field_options。要使用几个值,用逗号将它们隔开。 | Produce output that is more compatible with other database systems or with older MySQL servers | |
--complete-insert | complete-insert | 使用包括列名的完整的INSERT语句。 | Use complete INSERT statements that include column names | |
--create-options | create-options | 在CREATE TABLE语句中包括所有MySQL表选项。 | Include all MySQL-specific table options in CREATE TABLE statements | |
--databases | databases | 转储几个数据库。通常情况,mysqldump将命令行中的第1个名字参量看作数据库名,后面的名看作表名。使用该选项,它将所有名字参量看作数据库名。CREATE DATABASE IF NOT EXISTS db_name和USE db_name语句包含在每个新数据库前的输出中。 | Dump several databases | |
--debug[=debug_options] | debug | 写调试日志。debug_options字符串通常为'd:t:o,file_name'。 | Write a debugging log | |
--debug-check | debug-check | Print debugging information when the program exits | 5.1.21 | |
--debug-info | debug-info | Print debugging information, memory and CPU statistics when the program exits | 5.1.14 | |
--default-character-set=charset_name | default-character-set | 使用charsetas默认字符集。如果没有指定,mysqldump使用utf8。 | Use charset_name as the default character set | |
--delayed-insert | delayed-insert | 使用INSERT DELAYED语句插入行。 | Write INSERT DELAYED statements rather than INSERT statements | |
--delete-master-logs | delete-master-logs | 在主复制服务器上,完成转储操作后删除二进制日志。该选项自动启用--master-data。 | On a master replication server, delete the binary logs after performing the dump operation | |
--disable-keys | disable-keys | 对于每个表,用/*!40000 ALTER TABLE tbl_name DISABLE KEYS */;和/*!40000 ALTER TABLE tbl_name ENABLE KEYS */;语句引用INSERT语句。这样可以更快地装载转储文件,因为在插入所有行后创建索引。该选项只适合MyISAM表。 | For each table, surround the INSERT statements with statements to disable and enable keys | |
--dump-date | dump-date | Include dump date as "Dump completed on" comment if --comments is given | 5.1.23 | |
--events | events | Dump events from the dumped databases | 5.1.8 | |
--extended-insert | extended-insert | Use multiple-row INSERT syntax that include several VALUES lists | ||
--fields-enclosed-by=string | fields-enclosed-by | This option is used with the --tab option and has the same meaning as the corresponding clause for LOAD DATA INFILE | ||
--fields-escaped-by | fields-escaped-by | This option is used with the --tab option and has the same meaning as the corresponding clause for LOAD DATA INFILE | ||
--fields-optionally-enclosed-by=string | fields-optionally-enclosed-by | This option is used with the --tab option and has the same meaning as the corresponding clause for LOAD DATA INFILE | ||
--fields-terminated-by=string | fields-terminated-by | This option is used with the --tab option and has the same meaning as the corresponding clause for LOAD DATA INFILE | ||
--first-slave | first-slave | 不赞成使用,现在重新命名为--lock-all-tables。 | Deprecated; use --lock-all-tables instead | |
--flush-logs | flush-logs | 开始转储前刷新MySQL服务器日志文件。该选项要求RELOAD权限。请注意如果结合--all--database(或-A)选项使用该选项,根据每个转储的数据库刷新日志。例外情况是当使用--lock-all-tables或--master-data的时候:在这种情况下,日志只刷新一次,在所有 表被锁定后刷新。如果你想要同时转储和刷新日志,应使用--flush-logs连同--lock-all-tables或--master-data。 | Flush the MySQL server log files before starting the dump | |
--flush-privileges | flush-privileges | Emit a FLUSH PRIVILEGES statement after dumping the mysql database | ||
--help | Display help message and exit | |||
--hex-blob | hex-blob | 使用十六进制符号转储二进制字符串列(例如,'abc' 变为0x616263)。影响到的列有BINARY、VARBINARY、BLOB。 | Dump binary columns using hexadecimal notation (for example, 'abc' becomes 0x616263) | |
--host | host | 从给定主机的MySQL服务器转储数据。默认主机是localhost。 | Host to connect to (IP address or hostname) | |
--ignore-table=db_name.tbl_name | ignore-table | Do not dump the given table | ||
--insert-ignore | insert-ignore | Write INSERT IGNORE statements rather than INSERT statements | ||
--lines-terminated-by=string | lines-terminated-by | This option is used with the --tab option and has the same meaning as the corresponding clause for LOAD DATA INFILE | ||
--lock-all-tables | lock-all-tables | 所有数据库中的所有表加锁。在整体转储过程中通过全局读锁定来实现。该选项自动关闭--single-transaction和--lock-tables。 | Lock all tables across all databases | |
--lock-tables | lock-tables | 开始转储前锁定所有表。用READ LOCAL锁定表以允许并行插入MyISAM表。对于事务表例如InnoDB和BDB,--single-transaction是一个更好的选项,因为它不根本需要锁定表。 请注意当转储多个数据库时,--lock-tables分别为每个数据库锁定表。因此,该选项不能保证转储文件中的表在数据库之间的逻辑一致性。不同数据库表的转储状态可以完全不同。 | Lock all tables before dumping them | |
--log-error=file_name | log-error | Append warnings and errors to the named file | 5.1.18 | |
--master-data[=value] | master-data | 该选项将二进制日志的位置和文件名写入到输出中。该选项要求有RELOAD权限,并且必须启用二进制日志。如果该选项值等于1,位置和文件名被写入CHANGE MASTER语句形式的转储输出,如果你使用该SQL转储主服务器以设置从服务器,从服务器从主服务器二进制日志的正确位置开始。如果选项值等于2,CHANGE MASTER语句被写成SQL注释。如果value被省略,这是默认动作。 --master-data选项启用--lock-all-tables,除非还指定--single-transaction(在这种情况下,只在刚开始转储时短时间获得全局读锁定。又见--single-transaction。在任何一种情况下,日志相关动作发生在转储时。该选项自动关闭--lock-tables。 | Write the binary log file name and position to the output | |
--max_allowed_packet=value | max_allowed_packet | 客户端/服务器之间通信的缓存区的最大大小。最大为1GB。 | The maximum packet length to send to or receive from the server | |
--net_buffer_length=value | net_buffer_length | 客户端/服务器之间通信的缓存区的初始大小。当创建多行插入语句时(如同使用选项--extended-insert或--opt),mysqldump创建长度达net_buffer_length的行。如果增加该变量,还应确保在MySQL服务器中的net_buffer_length变量至少这么大。 | The buffer size for TCP/IP and socket communication | |
--no-autocommit | no-autocommit | Enclose the INSERT statements for each dumped table within SET autocommit = 0 and COMMIT statements | ||
--no-create-db | no-create-db | 该选项禁用CREATE DATABASE /*!32312 IF NOT EXISTS*/ db_name语句,如果给出---database或--all--database选项,则包含到输出中。 | This option suppresses the CREATE DATABASE statements | |
--no-create-info | no-create-info | 不写重新创建每个转储表的CREATE TABLE语句。 | Do not write CREATE TABLE statements that re-create each dumped table | |
--no-data | no-data | 不写表的任何行信息。如果你只想转储表的结构这很有用。 | Do not dump table contents | |
--no-set-names | no-set-names | Same as --skip-set-charset | ||
--no-tablespaces | no-tablespaces | Do not write any CREATE LOGFILE GROUP or CREATE TABLESPACE statements in output | 5.1.14 | |
--opt | opt | 该选项是速记;等同于指定 --add-drop-tables--add-locking --create-option --disable-keys--extended-insert --lock-tables --quick --set-charset。它可以给出很快的转储操作并产生一个可以很快装入MySQL服务器的转储文件。该选项默认开启,但可以用--skip-opt禁用。要想只禁用确信用-opt启用的选项,使用--skip形式;例如,--skip-add-drop-tables或--skip-quick。 | Shorthand for --add-drop-table --add-locks --create-options --disable-keys --extended-insert --lock-tables --quick --set-charset. | |
--order-by-primary | order-by-primary | Dump each table's rows sorted by its primary key, or by its first unique index | ||
--password[=password] | password | 连接服务器时使用的密码。如果你使用短选项形式(-p),不能在选项和密码之间有一个空格。如果在命令行中,忽略了--password或-p选项后面的 密码值,将提示你输入一个。 | The password to use when connecting to the server | |
--pipe | On Windows, connect to server using a named pipe | |||
--port=port_num | port | 用于连接的TCP/IP端口号。 | The TCP/IP port number to use for the connection | |
--quick | quick | 该选项用于转储大的表。它强制mysqldump从服务器一次一行地检索表中的行而不是检索所有行并在输出前将它缓存到内存中。 | Retrieve rows for a table from the server a row at a time | |
--quote-names | quote-names | 用‘`’字符引用数据库、表和列名。如果服务器SQL模式包括ANSI_QUOTES选项,用‘"’字符引用名。默认启用该选项。可以用--skip-quote-names禁用,但该选项应跟在其它选项后面,例如可以启用--quote-names的--compatible。 | Quote identifiers within backtick characters | |
--replace | replace | Write REPLACE statements rather than INSERT statements | 5.1.3 | |
--result-file=file | result-file | 将输出转向给定的文件。该选项应用在Windows中,因为它禁止将新行‘\n’字符转换为‘\r\n’回车、返回/新行序列。 | Direct output to a given file | |
--routines | routines | 在转储的数据库中转储存储程序(函数和程序)。使用---routines产生的输出包含CREATE PROCEDURE和CREATE FUNCTION语句以重新创建子程序。但是,这些语句不包括属性,例如子程序定义者或创建和修改时间戳。这说明当重载子程序时,对它们进行创建时定义者应设置为重载用户,时间戳等于重载时间。 如果你需要创建的子程序使用原来的定义者和时间戳属性,不使用--routines。相反,使用一个具有mysql数据库相应权限的MySQL账户直接转储和重载mysql.proc表的内容。 该选项在MySQL 5.1.2中添加进来。在此之前,存储程序不转储。 | Dump stored routines (procedures and functions) from the dumped databases | 5.1.2 |
--set-charset | set-charset | 将SET NAMES default_character_set加到输出中。该选项默认启用。要想禁用SET NAMES语句,使用--skip-set-charset。 | Add SET NAMES default_character_set to the output | |
--single-transaction | single-transaction | 该选项从服务器转储数据之前发出一个BEGIN SQL语句。它只适用于事务表,例如InnoDB和BDB,因为然后它将在发出BEGIN而没有阻塞任何应用程序时转储一致的数据库状态。 当使用该选项时,应记住只有InnoDB表能以一致的状态被转储。例如,使用该选项时任何转储的MyISAM或HEAP表仍然可以更改状态。 --single-transaction选项和--lock-tables选项是互斥的,因为LOCK TABLES会使任何挂起的事务隐含提交。 要想转储大的表,应结合--quick使用该选项。 | This option issues a BEGIN SQL statement before dumping data from the server | |
--skip-add-drop-table | skip-add-drop-table | Do not add a DROP TABLE statement before each CREATE TABLE statement | ||
--skip-add-locks | skip-add-locks | Do not add locks | ||
--skip-comments | skip-comments | Do not add comments to the dump file | ||
--skip-compact | skip-compact | Do not produce more compact output | ||
--skip-disable-keys | skip-disable-keys | Do not disable keys | ||
--skip-extended-insert | skip-extended-insert | Turn off extended-insert | ||
--skip-opt | skip-opt | Turn off the options set by --opt | ||
--skip-quick | skip-quick | Do not retrieve rows for a table from the server a row at a time | ||
--skip-quote-names | skip-quote-names | Do not quote identifiers | ||
--skip-set-charset | skip-set-charset | Suppress the SET NAMES statement | ||
--skip-triggers | skip-triggers | Do not dump triggers | ||
--skip-tz-utc | skip-tz-utc | Turn off tz-utc | 5.1.2 | |
--ssl-ca=file_name | ssl-ca | The path to a file that contains a list of trusted SSL CAs | ||
--ssl-capath=dir_name | ssl-capath | The path to a directory that contains trusted SSL CA certificates in PEM format | ||
--ssl-cert=file_name | ssl-cert | The name of the SSL certificate file to use for establishing a secure connection | ||
--ssl-cipher=cipher_list | ssl-cipher | A list of allowable ciphers to use for SSL encryption | ||
--ssl-key=file_name | ssl-key | The name of the SSL key file to use for establishing a secure connection | ||
--ssl-verify-server-cert | ssl-verify-server-cert | The server's Common Name value in its certificate is verified against the host name used when connecting to the server | ||
--tab=path | tab | 产生tab分割的数据文件。对于每个转储的表,mysqldump创建一个包含创建表的CREATE TABLE语句的tbl_name.sql文件,和一个包含其数据的tbl_name.txt文件。选项值为写入文件的目录。 默认情况,.txt数据文件的格式是在列值和每行后面的新行之间使用tab字符。可以使用--fields-xxx和--行--xxx选项明显指定格式。 注释:该选项只适用于mysqldump与mysqld服务器在同一台机器上运行时。你必须具有FILE权限,并且服务器必须有在你指定的目录中有写文件的许可。 | Produce tab-separated data files | |
--tables | tables | 覆盖---database或-B选项。选项后面的所有参量被看作表名。 | Override the --databases or -B option | |
--triggers | triggers | 为每个转储的表转储触发器。该选项默认启用;用--skip-triggers禁用它。 | Dump triggers for each dumped table | |
--tz-utc | tz-utc | Add SET TIME_ZONE='+00:00' to the dump file | 5.1.2 | |
--user=user_name | user | 连接服务器时使用的MySQL用户名。 | The MySQL user name to use when connecting to the server | |
--verbose | 冗长模式。打印出程序操作的详细信息。 | Verbose mode | ||
--version | 显示版本信息并退出。 | Display version information and exit | ||
--where='where_condition' | where | 只转储给定的WHERE条件选择的记录。请注意如果条件包含命令解释符专用空格或字符,一定要将条件引用起来。 | Dump only rows selected by the given WHERE condition | |
--xml | xml | Produce XML output |