基本思路是利用mysqldump导出到sql文件,然后利用rar加密压缩利用FTP上传到远程FTP空间。

Linux 下的Rar请到http://www.rarlab.com/ 下载。我的环境是CentOS

  
  
  1. #!/bin/bash
  2. FILENAME=$(date +%Y%m%d)
  3. echo time: $FILENAME
  4. #exit
  5. echo exporting
  6. /usr/local/mysql/bin/mysqldump --all-databases --quick --add-drop-table --socket=/tmp/mysql.sock --allow-keywords --complete-insert --compress > mysql.db.backup.sql
  7. echo compressing
  8. rar a "./backup/${FILENAME}_db_backup.rar" mysql.db.backup.sql -pYOURPASSWORD
  9. cd backup
  10. echo Uploading
  11. ftp -n<<!
  12. open FTPADDRESS
  13. user FTPUSER FTPPASSWORD
  14. cd backup
  15. put ${FILENAME}_db_backup.rar ${FILENAME}_backup.bak
  16. quit
  17. !
  18. echo Backup Successful

1.png

2.png

顺便放一下MySQL的常用参数:

格式可选描述[Chinese]描述[English]Introduction
--add-drop-databaseadd-drop-database每个CREATE DATABASE语句前添加DROP DATABASE语句Add a DROP DATABASE statement before each CREATE DATABASE statement 
--add-drop-tableadd-drop-table每个CREATE TABLE语句前添加DROP TABLE语句Add a DROP TABLE statement before each CREATE TABLE statement 
--add-drop-triggeradd-drop-trigger每个CREATE TRIGGER语句前添加DROP TRIGGER语句Add a DROP TRIGGER statement before each CREATE TRIGGER statement5.1.47-ndb-7.1.8
--add-locksadd-locks在每个表导出之前增加LOCK TABLES并且之后UNLOCK TABLE。Surround each table dump with LOCK TABLES and UNLOCK TABLES statements 
--all-databasesall-databases导出所有数据库中的所有表Dump all tables in all databases 
--all-tablespacesall-tablespaces Adds to a table dump all SQL statements needed to create any tablespaces used by an NDB Cluster table5.1.6
--allow-keywordsallow-keywords允许创建关键字列名。应在每个列名前面加上表名前缀。Allow creation of column names that are keywords 
--bind-address=ip_addressbind-address在具有多个网络接口的计算机,这个选项可以用来选择哪个接口连接到MySQL服务器时采用。Use the specified network interface to connect to the MySQL Server5.1.22-ndb-6.3.4
--commentscomments如果设置为 0,禁止转储文件中的其它信息,例如程序版本、服务器版本和主机。--skip—comments与---comments=0的结果相同。 默认值为1,即包括额外信息。Add comments to the dump file 
--compactcompact产生少量输出。该选项禁用注释并启用--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-insertcomplete-insert使用包括列名的完整的INSERT语句。Use complete INSERT statements that include column names 
--create-optionscreate-options在CREATE TABLE语句中包括所有MySQL表选项。Include all MySQL-specific table options in CREATE TABLE statements 
--databasesdatabases转储几个数据库。通常情况,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-checkdebug-check Print debugging information when the program exits5.1.21
--debug-infodebug-info Print debugging information, memory and CPU statistics when the program exits5.1.14
--default-character-set=charset_namedefault-character-set使用charsetas默认字符集。如果没有指定,mysqldump使用utf8。Use charset_name as the default character set 
--delayed-insertdelayed-insert使用INSERT DELAYED语句插入行。Write INSERT DELAYED statements rather than INSERT statements 
--delete-master-logsdelete-master-logs在主复制服务器上,完成转储操作后删除二进制日志。该选项自动启用--master-data。On a master replication server, delete the binary logs after performing the dump operation 
--disable-keysdisable-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-datedump-date Include dump date as "Dump completed on" comment if --comments is given5.1.23
--eventsevents Dump events from the dumped databases5.1.8
--extended-insertextended-insert Use multiple-row INSERT syntax that include several VALUES lists 
--fields-enclosed-by=stringfields-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-byfields-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=stringfields-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=stringfields-terminated-by This option is used with the --tab option and has the same meaning as the corresponding clause for LOAD DATA INFILE 
--first-slavefirst-slave不赞成使用,现在重新命名为--lock-all-tables。Deprecated; use --lock-all-tables instead 
--flush-logsflush-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-privilegesflush-privileges Emit a FLUSH PRIVILEGES statement after dumping the mysql database 
--help  Display help message and exit 
--hex-blobhex-blob使用十六进制符号转储二进制字符串列(例如,'abc' 变为0x616263)。影响到的列有BINARY、VARBINARY、BLOB。Dump binary columns using hexadecimal notation (for example, 'abc' becomes 0x616263) 
--hosthost从给定主机的MySQL服务器转储数据。默认主机是localhost。Host to connect to (IP address or hostname) 
--ignore-table=db_name.tbl_nameignore-table Do not dump the given table 
--insert-ignoreinsert-ignore Write INSERT IGNORE statements rather than INSERT statements 
--lines-terminated-by=stringlines-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-tableslock-all-tables所有数据库中的所有表加锁。在整体转储过程中通过全局读锁定来实现。该选项自动关闭--single-transaction和--lock-tables。Lock all tables across all databases 
--lock-tableslock-tables开始转储前锁定所有表。用READ LOCAL锁定表以允许并行插入MyISAM表。对于事务表例如InnoDB和BDB,--single-transaction是一个更好的选项,因为它不根本需要锁定表。 请注意当转储多个数据库时,--lock-tables分别为每个数据库锁定表。因此,该选项不能保证转储文件中的表在数据库之间的逻辑一致性。不同数据库表的转储状态可以完全不同。Lock all tables before dumping them 
--log-error=file_namelog-error Append warnings and errors to the named file5.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=valuemax_allowed_packet客户端/服务器之间通信的缓存区的最大大小。最大为1GB。The maximum packet length to send to or receive from the server 
--net_buffer_length=valuenet_buffer_length客户端/服务器之间通信的缓存区的初始大小。当创建多行插入语句时(如同使用选项--extended-insert或--opt),mysqldump创建长度达net_buffer_length的行。如果增加该变量,还应确保在MySQL服务器中的net_buffer_length变量至少这么大。The buffer size for TCP/IP and socket communication 
--no-autocommitno-autocommit Enclose the INSERT statements for each dumped table within SET autocommit = 0 and COMMIT statements 
--no-create-dbno-create-db该选项禁用CREATE DATABASE /*!32312 IF NOT EXISTS*/ db_name语句,如果给出---database或--all--database选项,则包含到输出中。This option suppresses the CREATE DATABASE statements 
--no-create-infono-create-info不写重新创建每个转储表的CREATE TABLE语句。Do not write CREATE TABLE statements that re-create each dumped table 
--no-datano-data不写表的任何行信息。如果你只想转储表的结构这很有用。Do not dump table contents 
--no-set-namesno-set-names Same as --skip-set-charset 
--no-tablespacesno-tablespaces Do not write any CREATE LOGFILE GROUP or CREATE TABLESPACE statements in output5.1.14
--optopt该选项是速记;等同于指定 --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-primaryorder-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_numport用于连接的TCP/IP端口号。The TCP/IP port number to use for the connection 
--quickquick该选项用于转储大的表。它强制mysqldump从服务器一次一行地检索表中的行而不是检索所有行并在输出前将它缓存到内存中。Retrieve rows for a table from the server a row at a time 
--quote-namesquote-names用‘`’字符引用数据库、表和列名。如果服务器SQL模式包括ANSI_QUOTES选项,用‘"’字符引用名。默认启用该选项。可以用--skip-quote-names禁用,但该选项应跟在其它选项后面,例如可以启用--quote-names的--compatible。Quote identifiers within backtick characters 
--replacereplace Write REPLACE statements rather than INSERT statements5.1.3
--result-file=fileresult-file将输出转向给定的文件。该选项应用在Windows中,因为它禁止将新行‘\n’字符转换为‘\r\n’回车、返回/新行序列。Direct output to a given file 
--routinesroutines在转储的数据库中转储存储程序(函数和程序)。使用---routines产生的输出包含CREATE PROCEDURE和CREATE FUNCTION语句以重新创建子程序。但是,这些语句不包括属性,例如子程序定义者或创建和修改时间戳。这说明当重载子程序时,对它们进行创建时定义者应设置为重载用户,时间戳等于重载时间。 如果你需要创建的子程序使用原来的定义者和时间戳属性,不使用--routines。相反,使用一个具有mysql数据库相应权限的MySQL账户直接转储和重载mysql.proc表的内容。 该选项在MySQL 5.1.2中添加进来。在此之前,存储程序不转储。Dump stored routines (procedures and functions) from the dumped databases5.1.2
--set-charsetset-charset将SET NAMES default_character_set加到输出中。该选项默认启用。要想禁用SET NAMES语句,使用--skip-set-charset。Add SET NAMES default_character_set to the output 
--single-transactionsingle-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-tableskip-add-drop-table Do not add a DROP TABLE statement before each CREATE TABLE statement 
--skip-add-locksskip-add-locks Do not add locks 
--skip-commentsskip-comments Do not add comments to the dump file 
--skip-compactskip-compact Do not produce more compact output 
--skip-disable-keysskip-disable-keys Do not disable keys 
--skip-extended-insertskip-extended-insert Turn off extended-insert 
--skip-optskip-opt Turn off the options set by --opt 
--skip-quickskip-quick Do not retrieve rows for a table from the server a row at a time 
--skip-quote-namesskip-quote-names Do not quote identifiers 
--skip-set-charsetskip-set-charset Suppress the SET NAMES statement 
--skip-triggersskip-triggers Do not dump triggers 
--skip-tz-utcskip-tz-utc Turn off tz-utc5.1.2
--ssl-ca=file_namessl-ca The path to a file that contains a list of trusted SSL CAs 
--ssl-capath=dir_namessl-capath The path to a directory that contains trusted SSL CA certificates in PEM format 
--ssl-cert=file_namessl-cert The name of the SSL certificate file to use for establishing a secure connection 
--ssl-cipher=cipher_listssl-cipher A list of allowable ciphers to use for SSL encryption 
--ssl-key=file_namessl-key The name of the SSL key file to use for establishing a secure connection 
--ssl-verify-server-certssl-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=pathtab产生tab分割的数据文件。对于每个转储的表,mysqldump创建一个包含创建表的CREATE TABLE语句的tbl_name.sql文件,和一个包含其数据的tbl_name.txt文件。选项值为写入文件的目录。 默认情况,.txt数据文件的格式是在列值和每行后面的新行之间使用tab字符。可以使用--fields-xxx和--行--xxx选项明显指定格式。 注释:该选项只适用于mysqldumpmysqld服务器在同一台机器上运行时。你必须具有FILE权限,并且服务器必须有在你指定的目录中有写文件的许可。Produce tab-separated data files 
--tablestables覆盖---database或-B选项。选项后面的所有参量被看作表名。Override the --databases or -B option 
--triggerstriggers为每个转储的表转储触发器。该选项默认启用;用--skip-triggers禁用它。Dump triggers for each dumped table 
--tz-utctz-utc Add SET TIME_ZONE='+00:00' to the dump file5.1.2
--user=user_nameuser连接服务器时使用的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 
--xmlxml Produce XML output