| CARVIEW |
The world's most popular open source database
- Documentation Library
- Table of Contents
- MySQL 6.0 Reference Manual
- MySQL 5.1 Reference Manual
- MySQL 5.0 Reference Manual
- MySQL 3.23/4.0/4.1 Manual
- Table of Contents
- 4.5 MySQL Client Programs
- 4.5.1 mysql — The MySQL Command-Line Tool
- 4.5.2 mysqladmin — Client for Administering a MySQL Server
- 4.5.3 mysqlcheck — A Table Maintenance and Repair Program
- 4.5.4 mysqldump — A Database Backup Program
- 4.5.5 mysqlimport — A Data Import Program
- 4.5.6 mysqlshow — Display Database, Table, and Column Information
- 4.5.7 mysqlslap — Load Emulation Client
The mysqldump client is a backup program originally written by Igor Romanenko. It can be used to dump a database or a collection of databases for backup or transfer to another SQL server (not necessarily a MySQL server). The dump typically contains SQL statements to create the table, populate it, or both. However, mysqldump can also be used to generate files in CSV, other delimited text, or XML format.
If you are doing a backup on the server and your tables all are
MyISAM tables, consider using the
mysqlhotcopy instead because it can
accomplish faster backups and faster restores. See
Section 4.6.9, “mysqlhotcopy — A Database Backup Program”.
There are three general ways to invoke mysqldump:
shell>mysqldump [shell>options]db_name[tables]mysqldump [shell>options] --databasesdb_name1[db_name2db_name3...]mysqldump [options] --all-databases
If you do not name any tables following
db_name or if you use the
--databases or --all-databases
option, entire databases are dumped.
mysqldump does not dump the
INFORMATION_SCHEMA database. If you name that
database explicitly on the command line,
mysqldump silently ignores it.
To get a list of the options your version of mysqldump supports, execute mysqldump --help.
Some mysqldump options are shorthand for
groups of other options. --opt and
--compact fall into this category. For example,
use of --opt is the same as specifying
--add-drop-table --add-locks --create-options
--disable-keys --extended-insert --lock-tables --quick
--set-charset. Note that as of MySQL 5.1,
all of the options that --opt stands for also
are on by default because --opt is on by
default.
To reverse the effect of a group option, uses its
--skip- form
(xxx--skip-opt or
--skip-compact). It is also possible to select
only part of the effect of a group option by following it with
options that enable or disable specific features. Here are some
examples:
To select the effect of
--optexcept for some features, use the--skipoption for each feature. For example, to disable extended inserts and memory buffering, use--opt --skip-extended-insert --skip-quick. (As of MySQL 5.1,--skip-extended-insert --skip-quickis sufficient because--optis on by default.)To reverse
--optfor all features except index disabling and table locking, use--skip-opt --disable-keys --lock-tables.
When you selectively enable or disable the effect of a group
option, order is important because options are processed first
to last. For example, --disable-keys --lock-tables
--skip-opt would not have the intended effect; it is
the same as --skip-opt by itself.
mysqldump can retrieve and dump table
contents row by row, or it can retrieve the entire content from
a table and buffer it in memory before dumping it. Buffering in
memory can be a problem if you are dumping large tables. To dump
tables row by row, use the --quick option (or
--opt, which enables --quick).
The --opt option (and hence
--quick) is enabled by default in MySQL
5.1; to enable memory buffering, use
--skip-quick.
If you are using a recent version of
mysqldump to generate a dump to be reloaded
into a very old MySQL server, you should not use the
--opt or --extended-insert
option. Use --skip-opt instead.
Note
mysqldump from the MySQL 5.1.21 distribution cannot be used to create dumps from MySQL server versions 5.1.20 and older. This issue is fixed in MySQL 5.1.22. (Bug#30123)
Table 4.5. mysqldump Option Reference
| Format | Config File | Description | Introduction |
|---|---|---|---|
| --add-drop-database | add-drop-database | Add a DROP DATABASE statement before each CREATE DATABASE statement | |
| --add-drop-table | add-drop-table | Add a DROP TABLE statement before each CREATE TABLE statement | |
| --add-locks | add-locks | Surround each table dump with LOCK TABLES and UNLOCK TABLES statements | |
| --all-databases | all-databases | Dump all tables in all databases | |
| --allow-keywords | allow-keywords | Allow creation of column names that are keywords | |
| --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 |
| --comments | comments | Add comments to the dump file | |
| --compact | compact | Produce less verbose output | |
| --compatible=name[,name,...] | compatible | Produce output that is more compatible with other database systems or with older MySQL servers | |
| --complete-insert | complete-insert | Use complete INSERT statements that include column names | |
| --create-options | create-options | Include all MySQL-specific table options in the CREATE TABLE statements | |
| --databases | databases | Dump several databases | |
| --debug[=debug_options] | debug | 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 |
| --delayed-insert | delayed-insert | Write INSERT DELAYED statements rather than INSERT statements | |
| --delete-master-logs | delete-master-logs | On a master replication server, delete the binary logs after performing the dump operation | |
| --disable-keys | disable-keys | For each table, surround the INSERT statements with disable and enable keys statements | |
| --dump-date | dump-date | Include dump date in "Dump completed on" comment if --comments is given | 5.1.23 |
| -E | events | Dump events from the dumped databases | |
| --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 -T 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 -T 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 -T 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 -T option and has the same meaning as the corresponding clause for LOAD DATA INFILE | |
| --lock-all-tables | first-slave | Deprecated. Now renamed to --lock-all-tables | |
| --flush-logs | flush-logs | 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 | Dump binary columns using hexadecimal notation (for example, 'abc' becomes 0x616263) | |
| --ignore-table=db_name.tbl_name | ignore-table | Do not dump the given table | |
| --insert-ignore | insert-ignore | Write INSERT statements with the IGNORE option | |
| --lines-terminated-by=string | lines-terminated-by | This option is used with the -T option and has the same meaning as the corresponding clause for LOAD DATA INFILE | |
| --lock-all-tables | lock-all-tables | Lock all tables across all databases | |
| --lock-tables | 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 | Write the binary log file name and position to the output | |
| --max_allowed_packet=value | max_allowed_packet | The maximum packet length to send to or receive from the server | |
| --net_buffer_length=value | 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 | This option suppresses the CREATE DATABASE statements | |
| --no-create-info | no-create-info | Do not write CREATE TABLE statements that re-create each dumped table | |
| --no-data | no-data | Do not write any table row information (that is, do not dump table contents) | |
| --no-set-names | no-set-names | Turn off complete-insert | |
| --opt | opt | This option is shorthand; it is the same as specifying --add-drop-table --add-locks --create-options --disable-keys --extended-insert --lock-tables --quick --set-charset. | |
| --order-by-primary | order-by-primary | Sorts each table's rows by its primary key, or by its first unique index | |
| --password[=password] | password | The password to use when connecting to the server | |
| --pipe | On Windows, connect to server via a named pipe | ||
| --port=port_num | port | The TCP/IP port number to use for the connection | |
| --quick | quick | Retrieve rows for a table from the server a row at a time | |
| --quote-names | quote-names | Quote database, table, and column names within backtick characters | |
| --replace | replace | Write REPLACE statements rather than INSERT statements | |
| --result-file=file | result-file | Direct output to a given file | |
| -R | routines | Dump stored routines (procedures and functions) from the dumped databases | |
| --set-charset | set-charset | Add SET NAMES default_character_set to the output | |
| --single-transaction | single-transaction | This option issues a BEGIN SQL statement before dumping data from the server | |
| --skip-add-drop-table | skip-add-drop-table | Do not add | |
| --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 | Turn off compact | |
| --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 | Turn off quote names | |
| -skip-set-charset | skip-set-charset | Suppress the SET NAMES statement | |
| --skip-triggers | skip-triggers | Turn off triggers | |
| --skip-tz-utc | skip-tz-utc | Turn off tz-utc | |
| --ssl-ca=file_name | ssl-ca | The path to a file that contains a list of trusted SSL CAs | |
| --ssl-capath=directory_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 | Produce tab-separated data files | |
| --tables | tables | Override the --databases or -B option | |
| --triggers | triggers | Dump triggers for each dumped table | |
| --tz-utc | tz-utc | Add SET TIME_ZONE='+00:00' to the dump file | |
| --verbose | Verbose mode | ||
| --version | Display version information and exit | ||
| --where='where_condition' | where | Dump only rows selected by the given WHERE condition | |
| --xml | xml | Produce XML output |
mysqldump supports the following options:
-
Display a help message and exit.
-
Add a
DROP DATABASEstatement before eachCREATE DATABASEstatement. -
Add a
DROP TABLEstatement before eachCREATE TABLEstatement. -
Surround each table dump with
LOCK TABLESandUNLOCK TABLESstatements. This results in faster inserts when the dump file is reloaded. See Section 7.2.20, “Speed ofINSERTStatements”. -
Dump all tables in all databases. This is the same as using the
--databasesoption and naming all the databases on the command line. -
Adds to a table dump all SQL statements needed to create any tablespaces used by an
NDBCLUSTERtable. This information is not otherwise included in the output from mysqldump. This option is currently relevant only to MySQL Cluster tables.This option was added in MySQL 5.1.6.
-
Allow creation of column names that are keywords. This works by prefixing each column name with the table name.
-
The directory where character sets are installed. See Section 9.2, “The Character Set Used for Data and Sorting”.
-
Write additional information in the dump file such as program version, server version, and host. This option is enabled by default. To suppress this additional information, use
--skip-comments. -
Produce less verbose output. This option enables the
--skip-add-drop-table,--skip-add-locks,--skip-comments,--skip-disable-keys, and--skip-set-charsetoptions.Note
Prior to release 5.1.21, this option did not create valid SQL if the database dump contained views. The recreation of views requires the creation and removal of temporary tables and this option suppressed the removal of those temporary tables. As a workaround, use
--compactwith the--add-drop-tableoption and then manually adjust the dump file. -
Produce output that is more compatible with other database systems or with older MySQL servers. The value of
namecan beansi,mysql323,mysql40,postgresql,oracle,mssql,db2,maxdb,no_key_options,no_table_options, orno_field_options. To use several values, separate them by commas. These values have the same meaning as the corresponding options for setting the server SQL mode. See Section 5.1.7, “Server SQL Modes”.This option does not guarantee compatibility with other servers. It only enables those SQL mode values that are currently available for making dump output more compatible. For example,
--compatible=oracledoes not map data types to Oracle types or use Oracle comment syntax.This option requires a server version of 4.1.0 or higher. With older servers, it does nothing.
-
Use complete
INSERTstatements that include column names. -
Compress all information sent between the client and the server if both support compression.
-
Include all MySQL-specific table options in the
CREATE TABLEstatements. -
Dump several databases. Normally, mysqldump treats the first name argument on the command line as a database name and following names as table names. With this option, it treats all name arguments as database names.
CREATE DATABASEandUSEstatements are included in the output before each new database. -
--debug[=,debug_options]-# [debug_options]Write a debugging log. The
debug_optionsstring is often'd:t:o,. The default value isfile_name''d:t:o,/tmp/mysqldump.trace'. -
Print some debugging information when the program exits. This option was added in MySQL 5.1.21.
-
Print debugging information and memory and CPU usage statistics when the program exits. This option was added in MySQL 5.1.14.
-
--default-character-set=charset_nameUse
charset_nameas the default character set. See Section 9.2, “The Character Set Used for Data and Sorting”. If no character set is specified, mysqldump usesutf8, and earlier versions uselatin1.This option has no effect for output data files produced by using the
--taboption. See the description for that option. -
Write
INSERT DELAYEDstatements rather thanINSERTstatements. -
On a master replication server, delete the binary logs after performing the dump operation. This option automatically enables
--master-data. -
For each table, surround the
INSERTstatements with/*!40000 ALTER TABLEandtbl_nameDISABLE KEYS */;/*!40000 ALTER TABLEstatements. This makes loading the dump file faster because the indexes are created after all rows are inserted. This option is effective only for non-unique indexes oftbl_nameENABLE KEYS */;MyISAMtables. -
mysqldump produces a
-- Dump completed oncomment at the end of the dump if theDATE--commentsoption is given. However, the date causes dump files for identical data take at different times to appear to be different.--dump-dateand--skip-dump-datecontrol whether the date is added to the comment. The default is--dump-date(include the date in the comment).--skip-dump-datesuppresses date printing. This option was added in MySQL 5.1.23. -
Dump events from the dumped databases. This option was added in MySQL 5.1.8.
-
Use multiple-row
INSERTsyntax that include severalVALUESlists. This results in a smaller dump file and speeds up inserts when the file is reloaded. -
--fields-terminated-by=...,--fields-enclosed-by=...,--fields-optionally-enclosed-by=...,--fields-escaped-by=...These options are used with the
-Toption and have the same meaning as the corresponding clauses forLOAD DATA INFILE. See Section 12.2.6, “LOAD DATA INFILESyntax”. -
Deprecated. Now renamed to
--lock-all-tables. -
Flush the MySQL server log files before starting the dump. This option requires the
RELOADprivilege. Note that if you use this option in combination with the--all-databases(or-A) option, the logs are flushed for each database dumped. The exception is when using--lock-all-tablesor--master-data: In this case, the logs are flushed only once, corresponding to the moment that all tables are locked. If you want your dump and the log flush to happen at exactly the same moment, you should use--flush-logstogether with either--lock-all-tablesor--master-data. -
Emit a
FLUSH PRIVILEGESstatement after dumping themysqldatabase. This option should be used any time the dump contains themysqldatabase and any other database that depends on the data in themysqldatabase for proper restoration. This option was added in MySQL 5.1.12. -
Continue even if an SQL error occurs during a table dump.
One use for this option is to cause mysqldump to continue executing even when it encounters a view that has become invalid because the definition refers to a table that has been dropped. Without
--force, mysqldump exits with an error message. With--force, mysqldump prints the error message, but it also writes an SQL comment containing the view definition to the dump output and continues executing. -
--host=,host_name-hhost_nameDump data from the MySQL server on the given host. The default host is
localhost. -
Dump binary columns using hexadecimal notation (for example,
'abc'becomes0x616263). The affected data types areBINARY,VARBINARY,BLOB, andBIT. -
--ignore-table=db_name.tbl_nameDo not dump the given table, which must be specified using both the database and table names. To ignore multiple tables, use this option multiple times. This option also can be used to ignore views.
-
Write
INSERTstatements with theIGNOREoption. -
This option is used with the
-Toption and has the same meaning as the corresponding clause forLOAD DATA INFILE. See Section 12.2.6, “LOAD DATA INFILESyntax”. -
Lock all tables across all databases. This is achieved by acquiring a global read lock for the duration of the whole dump. This option automatically turns off
--single-transactionand--lock-tables. -
Lock all tables before dumping them. The tables are locked with
READ LOCALto allow concurrent inserts in the case ofMyISAMtables. For transactional tables such asInnoDBandBDB,--single-transactionis a much better option, because it does not need to lock the tables at all.Please note that when dumping multiple databases,
--lock-tableslocks tables for each database separately. Therefore, this option does not guarantee that the tables in the dump file are logically consistent between databases. Tables in different databases may be dumped in completely different states. -
Append warnings and errors to the named file. This option was added in MySQL 5.1.18.
-
Use this option to dump a master replication server to produce a dump file that can be used to set up another server as a slave of the master. It causes the dump output to include a
CHANGE MASTER TOstatement that indicates the binary log coordinates (file name and position) of the dumped server. These are the master server coordinates from which the slave should start replicating.If the option value is 2, the
CHANGE MASTER TOstatement is written as an SQL comment, and thus is informative only; it has no effect when the dump file is reloaded. If the option value is 1, the statement takes effect when the dump file is reloaded. If the option value is not specified, the default value is 1.This option requires the
RELOADprivilege and the binary log must be enabled.The
--master-dataoption automatically turns off--lock-tables. It also turns on--lock-all-tables, unless--single-transactionalso is specified, in which case, a global read lock is acquired only for a short time at the beginning of the dump (see the description for--single-transaction). In all cases, any action on logs happens at the exact moment of the dump.It is also possible to set up a slave by dumping an existing slave of the master. To do this, use the following procedure on the existing slave:
-
Stop the slave's SQL thread and get its current status:
mysql>
STOP SLAVE SQL_THREAD;mysql>SHOW SLAVE STATUS; From the output of the SHOW SLAVE STATUS statement, get the binary log coordinates of the master server from which the new slave should start replicating. These coordinates are the values of the Relay_Master_Log_File and Exec_Master_Log_Pos values. Denote those values as file_name and file_pos.
-
Dump the slave server:
shell>
mysqldump --master-data=2 --all-databases > dumpfile -
Restart the slave:
mysql>
START SLAVE; -
On the new slave, reload the dump file:
shell>
mysql < dumpfile -
On the new slave, set the replication coordinates to those of the master server obtained earlier:
mysql>
CHANGE MASTER TO->MASTER_LOG_FILE = 'file_name', MASTER_LOG_POS = file_pos;The
CHANGE MASTER TOstatement might also need other parameters, such asMASTER_HOSTto point the slave to the correct master server host. Add any such parameters as necessary.
-
-
Enclose the
INSERTstatements for each dumped table withinSET autocommit = 0andCOMMITstatements. -
This option suppresses the
CREATE DATABASEstatements that are otherwise included in the output if the--databasesor--all-databasesoption is given. -
Do not write
CREATE TABLEstatements that re-create each dumped table. -
Do not write any table row information (that is, do not dump table contents). This is very useful if you want to dump only the
CREATE TABLEstatement for the table. -
This option is shorthand; it is the same as specifying
--add-drop-table --add-locks --create-options --disable-keys --extended-insert --lock-tables --quick --set-charset. It should give you a fast dump operation and produce a dump file that can be reloaded into a MySQL server quickly.The
--optoption is enabled by default. Use--skip-optto disable it. See the discussion at the beginning of this section for information about selectively enabling or disabling certain of the options affected by--opt. -
Sorts each table's rows by its primary key, or by its first unique index, if such an index exists. This is useful when dumping a
MyISAMtable to be loaded into anInnoDBtable, but will make the dump itself take considerably longer. -
--password[=,password]-p[password]The password to use when connecting to the server. If you use the short option form (
-p), you cannot have a space between the option and the password. If you omit thepasswordvalue following the--passwordor-poption on the command line, you are prompted for one.Specifying a password on the command line should be considered insecure. See Section 5.5.6, “Keeping Passwords Secure”.
-
On Windows, connect to the server via a named pipe. This option applies only for connections to a local server, and only if the server supports named-pipe connections.
-
The TCP/IP port number to use for the connection.
-
--protocol={TCP|SOCKET|PIPE|MEMORY}The connection protocol to use for connecting to the server. It is useful when the other connection parameters normally would cause a protocol to be used other than the one you want. For details on the allowable values, see Section 4.2.2, “Connecting to the MySQL Server”.
-
This option is useful for dumping large tables. It forces mysqldump to retrieve rows for a table from the server a row at a time rather than retrieving the entire row set and buffering it in memory before writing it out.
-
Quote database, table, and column names within “
`” characters. If theANSI_QUOTESSQL mode is enabled, names are quoted within “"” characters. This option is enabled by default. It can be disabled with--skip-quote-names, but this option should be given after any option such as--compatiblethat may enable--quote-names. -
Write
REPLACEstatements rather thanINSERTstatements. Available as of MySQL 5.1.3. -
--result-file=,file_name-rfile_nameDirect output to a given file. This option should be used on Windows to prevent newline “
\n” characters from being converted to “\r\n” carriage return/newline sequences. The result file is created and its contents overwritten, even if an error occurs while generating the dump. The previous contents are lost. -
Dump stored routines (procedures and functions) from the dumped databases. Use of this option requires the
SELECTprivilege for themysql.proctable. The output generated by using--routinescontainsCREATE PROCEDUREandCREATE FUNCTIONstatements to re-create the routines. However, these statements do not include attributes such as the routine creation and modification timestamps. This means that when the routines are reloaded, they will be created with the timestamps equal to the reload time.If you require routines to be re-created with their original timestamp attributes, do not use
--routines. Instead, dump and reload the contents of themysql.proctable directly, using a MySQL account that has appropriate privileges for themysqldatabase.This option was added in MySQL 5.1.2. Before that, stored routines are not dumped. Routine
DEFINERvalues are not dumped until MySQL 5.1.8. This means that before 5.1.8, when routines are reloaded, they will be created with the definer set to the reloading user. If you require routines to be re-created with their original definer, dump and load the contents of themysql.proctable directly as described earlier. -
Add
SET NAMESto the output. This option is enabled by default. To suppress thedefault_character_setSET NAMESstatement, use--skip-set-charset. -
This option issues a
BEGINSQL statement before dumping data from the server. It is useful only with transactional tables such asInnoDB, because then it dumps the consistent state of the database at the time whenBEGINwas issued without blocking any applications.When using this option, you should keep in mind that only
InnoDBtables are dumped in a consistent state. For example, anyMyISAMorMEMORYtables dumped while using this option may still change state.While a
--single-transactiondump 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. A consistent read is not isolated from those statements, so use of them on a table to be dumped can cause theSELECTperformed by mysqldump to retrieve the table contents to obtain incorrect contents or fail.This option is not supported for MySQL Cluster tables; the results cannot be guaranteed to be consistent due to the fact that the
NDBCLUSTERstorage engine supports only theREAD_COMMITTEDtransaction isolation level. You should always useNDBbackup and restore instead.The
--single-transactionoption and the--lock-tablesoption are mutually exclusive, becauseLOCK TABLEScauses any pending transactions to be committed implicitly.To dump large tables, you should combine this option with
--quick. -
See the description for the
--commentsoption. -
See the description for the
--optoption. -
For connections to
localhost, the Unix socket file to use, or, on Windows, the name of the named pipe to use. -
Options that begin with
--sslspecify whether to connect to the server via SSL and indicate where to find SSL keys and certificates. See Section 5.5.7.3, “SSL Command Options”. -
Produce tab-separated data files. For each dumped table, mysqldump creates a
file that contains thetbl_name.sqlCREATE TABLEstatement that creates the table, and afile that contains its data. The option value is the directory in which to write the files.tbl_name.txtBy default, the
.txtdata files are formatted using tab characters between column values and a newline at the end of each line. The format can be specified explicitly using the--fields-andxxx--lines-terminated-byoptions.Column values are dumped using the
binarycharacter set and the--default-character-setoption is ignored. In effect, there is no character set conversion. If a table contains columns in several character sets, the output data file will as well and you may not be able to reload the file correctly. -
Override the
--databasesor-Boption. mysqldump regards all name arguments following the option as table names. -
Dump triggers for each dumped table. This option is enabled by default; disable it with
--skip-triggers. -
This option enables
TIMESTAMPcolumns to be dumped and reloaded between servers in different time zones. mysqldump sets its connection time zone to UTC and addsSET TIME_ZONE='+00:00'to the dump file. Without this option,TIMESTAMPcolumns are dumped and reloaded in the time zones local to the source and destination servers, which can cause the values to change.--tz-utcalso protects against changes due to daylight saving time.--tz-utcis enabled by default. To disable it, use--skip-tz-utc. This option was added in MySQL 5.1.2. -
--user=,user_name-uuser_nameThe MySQL user name to use when connecting to the server.
-
Verbose mode. Print more information about what the program does.
-
Display version information and exit.
-
--where=',where_condition'-w 'where_condition'Dump only rows selected by the given
WHEREcondition. Quotes around the condition are mandatory if it contains spaces or other characters that are special to your command interpreter.Examples:
--where="user='jimf'" -w"userid>1" -w"userid<1"
-
Write dump output as well-formed XML.
NULL,'NULL', and Empty Values: For some column namedcolumn_name, theNULLvalue, an empty string, and the string value'NULL'are distinguished from one another in the output generated by this option as follows:Value: XML Representation: NULL(unknown value)<field name="column_name" xsi:nil="true" />''(empty string)<field name="column_name"></field>'NULL'(string value)<field name="column_name">NULL</field>Beginning with MySQL 5.1.12, the output from the mysql client when run using the
--xmloption also follows these rules. (See Section 4.5.1.1, “mysql Options”.)Beginning with MySQL 5.1.18, XML output from mysqldump includes the XML namespace, as shown here:
shell>
mysqldump --xml -u root world City<?xml version="1.0"?> <mysqldump xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"> <database name="world"> <table_structure name="City"> <field Field="ID" Type="int(11)" Null="NO" Key="PRI" Extra="auto_increment" /> <field Field="Name" Type="char(35)" Null="NO" Key="" Default="" Extra="" /> <field Field="CountryCode" Type="char(3)" Null="NO" Key="" Default="" Extra="" /> <field Field="District" Type="char(20)" Null="NO" Key="" Default="" Extra="" /> <field Field="Population" Type="int(11)" Null="NO" Key="" Default="0" Extra="" /> <key Table="City" Non_unique="0" Key_name="PRIMARY" Seq_in_index="1" Column_name="ID" Collation="A" Cardinality="4079" Null="" Index_type="BTREE" Comment="" /> <options Name="City" Engine="MyISAM" Version="10" Row_format="Fixed" Rows="4079" Avg_row_length="67" Data_length="27329 3" Max_data_length="18858823439613951" Index_length="43008" Data_free="0" Auto_increment="4080" Create_time="2007-03-31 01:47:01" Updat e_time="2007-03-31 01:47:02" Collation="latin1_swedish_ci" Create_options="" Comment="" /> </table_structure> <table_data name="City"> <row> <field name="ID">1</field> <field name="Name">Kabul</field> <field name="CountryCode">AFG</field> <field name="District">Kabol</field> <field name="Population">1780000</field> </row>...<row> <field name="ID">4079</field> <field name="Name">Rafah</field> <field name="CountryCode">PSE</field> <field name="District">Rafah</field> <field name="Population">92020</field> </row> </table_data> </database> </mysqldump>
You can also set the following variables by using
--
syntax:
var_name=value
-
The maximum size of the buffer for client/server communication. The maximum is 1GB.
-
The initial size of the buffer for client/server communication. When creating multiple-row-insert statements (as with option
--extended-insertor--opt), mysqldump creates rows up tonet_buffer_lengthlength. If you increase this variable, you should also ensure that thenet_buffer_lengthvariable in the MySQL server is at least this large.
The most common use of mysqldump is probably for making a backup of an entire database:
shell> mysqldump db_name > backup-file.sql
You can read the dump file back into the server like this:
shell> mysql db_name < backup-file.sql
Or like this:
shell> mysql -e "source /path-to-backup/backup-file.sql" db_name
mysqldump is also very useful for populating databases by copying data from one MySQL server to another:
shell> mysqldump --opt db_name | mysql --host=remote_host -C db_name
It is possible to dump several databases with one command:
shell> mysqldump --databases db_name1 [db_name2 ...] > my_databases.sql
To dump all databases, use the --all-databases
option:
shell> mysqldump --all-databases > all_databases.sql
For InnoDB tables,
mysqldump provides a way of making an online
backup:
shell> mysqldump --all-databases --single-transaction > all_databases.sql
This backup acquires a global read lock on all tables (using
FLUSH TABLES WITH READ
LOCK) at the beginning of the dump. As soon as this
lock has been acquired, the binary log coordinates are read and
the lock is released. If long updating statements are running
when the FLUSH statement is
issued, the MySQL server may get stalled until those statements
finish. After that, the dump becomes lock-free and does not
disturb reads and writes on the tables. If the update statements
that the MySQL server receives are short (in terms of execution
time), the initial lock period should not be noticeable, even
with many updates.
For point-in-time recovery (also known as “roll-forward,” when you need to restore an old backup and replay the changes that happened since that backup), it is often useful to rotate the binary log (see Section 5.2.4, “The Binary Log”) or at least know the binary log coordinates to which the dump corresponds:
shell> mysqldump --all-databases --master-data=2 > all_databases.sql
Or:
shell>mysqldump --all-databases --flush-logs --master-data=2> all_databases.sql
The --master-data and
--single-transaction options can be used
simultaneously, which provides a convenient way to make an
online backup suitable for point-in-time recovery if tables are
stored using the InnoDB storage engine.
For more information on making backups, see Section 6.1, “Database Backups”, and Section 6.2, “Example Backup and Recovery Strategy”.
If you encounter problems backing up views, please read the section that covers restrictions on views which describes a workaround for backing up views when this fails due to insufficient privileges. See Section D.4, “Restrictions on Views”.
MySQL Enterprise. MySQL Enterprise subscribers will find more information about mysqldump in the Knowledge Base article, How Can I Avoid Inserting Duplicate Rows From a Dump File?. Access to the MySQL Knowledge Base collection of articles is one of the advantages of subscribing to MySQL Enterprise. For more information, see https://www.mysql.com/products/enterprise/advisors.html.


User Comments
for "mysqldump" ing innodb tables
take the dump as you would normally do using mysqldump
open the dump file put this statement at the beginning
SET FOREIGN_KEY_CHECKS=0;
of the sql dump text file
and import the same file as you would nomally import an sql dump file
For a faster "mysqldump" innodb tables
1. mysqldump --opt --user=username --password database > filetosaveto.sql
2. open the dump file put this statement at the beginning of the sql dump text file:
SET FOREIGN_KEY_CHECKS=0;
3. mysql --user=username --password database < dumpfile.sql
Very fast.
After adding "SET FOREIGN_KEY_CHECKS=0;" remember to append the "SET FOREIGN_KEY_CHECKS=1;" at the end of the import file. The potential problem is that any data inconsistency that would've made the foreign key failed during import would have made it into the database even after the forieng keys are turned back on. This is especially true if the foreign keys aren't turned back on after a long period of time which can happen if the "SET FOREIGN_KEY_CHECKS=1;" was not appended to the import file in the first place.
You can even do your mysqldump backups with logrotate.
Simply put something like this into /etc/logrotate.conf:
/var/backups/mysql/dump.sql {
daily
rotate 14
missingok
compress
postrotate
/usr/bin/mysqldump --defaults-extra-file=/.../backup-credentials.cnf --opt --flush-logs --all-databases > /var/backups/mysql/dump.sql
endscript
}
Following mysqldump import example for InnoDB tables is at least 100x faster than previous examples.
1. mysqldump --opt --user=username --password database > dumbfile.sql
2. Edit the dump file and put these lines at the beginning:
SET AUTOCOMMIT = 0;
SET FOREIGN_KEY_CHECKS=0;
3. Put these lines at the end:
SET FOREIGN_KEY_CHECKS = 1;
COMMIT;
SET AUTOCOMMIT = 1;
4. mysql --user=username --password database < dumpfile.sql
If you want to schedule a task on windows to backup and move your data somewhere, the lack of documentation and command-line tools in windows can make it a real beast. I hope this helps you keep your data safe.
First off, you will need a command line file compressor (or your should use one, anyway). I like GNU gzip. You can get it for windows here https://gnuwin32.sourceforge.net/packages/gzip.htm
Secondly, you will need to use windowsw FTP via command line. It took me all day to find documentation on this guy, so I hope this saves some time for somebody.
Anyway, you need two files -- the batch file and a script for your ftp client. The Batch file should look like this guy (it uses random numbers in the file name so that multiple backups are not overwritten):
@ECHO OFF
@REM Set dir variables. Use ~1 format in win2k
SET basedir=C:\BACKUP~1
SET workdir=c:\TEMP
SET mysqldir=c:\mysql\bin
SET gzipdir=c:\PROGRA~1\GnuWin32\bin
SET mysqlpassword=mygoodpassword
SET mysqluser=myrootuser
@REM Change to mysqldir
CD %mysqldir%
@REM dump database. This is all one line
mysqldump -u %mysqluser% -p%mysqlpassword% --all-databases >%workdir%\backup.sql
@REM Change to workdir
CD %workdir%
@REM Zip up database
%gzipdir%\gzip.exe backup.sql
@REM Move to random file name
MOVE backup.sql.gz backup.%random%.gz
@REM FTP file to repository
FTP -n -s:%basedir%\ftp-commands.txt
@REM Remove old backup files
del backup.sql
del backup.*.gz
@REM Change back to base dir
CD %basedir%
And your ftp script should look like this guy (and be named ftp-commands.txt so the above script can find it)
open
ftp.mybackuplocation.com
user
myusername
mypassword
bin
put backup.*.gz
quit
Make sure both of the above files are in whatever directory you set up as %basedir% and test it out and make sure everything works for you. Then schedule it to run every day to protect your data!
Corey's example is helpful, but I don't care for the random file name. Here is the manual script I use on Windows for kicking off a MYSQL backup.
You could easily add all the other bells and whistles of ZIP, FTP, and scheduling should you need it. Note that I didn't use a password or many of the other args for mysqldump, you can add those if ya need 'em.
@ECHO OFF
for /f "tokens=1-4 delims=/ " %%a in ('date/t') do (
set dw=%%a
set mm=%%b
set dd=%%c
set yy=%%d
)
SET bkupdir=C:\path\to\where\you\want\backups
SET mysqldir=D:\path\to\mysql
SET dbname=this_is_the_name_of_my_database
SET dbuser=this_is_my_user_name
@ECHO Beginning backup of %dbname%...
%mysqldir%\bin\mysqldump -B %dbname% -u %dbuser% > %bkupdir%\dbBkup_%dbname%_%yy%%mm%%dd%.sql
@ECHO Done! New File: dbBkup_%dbname%_%yy%%mm%%dd%.sql
pause
A little reformulation of the actions that occur during an online dump with log-point registration, i.e. a dump that does not unduly disturb clients using the database during the dump (N.B.: only from 4.1.8 on!) and that can be used to start a slave server from the correct point in the logs.
Use these options:
--single-transaction
--flush-logs
--master-data=1
--delete-master-logs
If you have several databases that are binary-logged and you want to keep a consistent binary log you may have to include all the databases instead of just some (is that really so?):
--all-databases
Now, these are the actions performed by the master server:
1) Acquire global read lock using FLUSH TABLES WITH READ LOCK. This also flushes the query cache and the query result cache. Caused by option --single-transaction.
2) All running and outstanding transactions terminate. MySQL server stalls for further updates.
3) Read lock on all tables acquired.
4) All the logs are flushed, in particular the binary log is closed and a new generation binary log is opened. Caused by option --flush-logs
5) Binary lock coordinates are read and written out so that the slave can position correctly in the binary log. Caused by --master-data=1
6) Read lock is released, MySQL server can proceed with updates. These updates will also go to the binary log and can thus be replayed by the slave. Meanwhile, the InnoDB tables are dumped in a consistent state, which is the state they were in in step 5. (Not guaranteed for MyISAM tables)
7) Dump terminates after a possibly long time.
8) Any old binary log files are deleted. Caused by --delete-master-logs.
Additionally, there are performance-influencing options:
--extended-insert: use multiple-row insert statements
--quick: do not do buffering of row data, good if tables are large
And there are format-influencing options:
--hex-blob: dump binary columns in hex
--complete-insert: use complete insert statements that include column names works nicely with --extended-insert
--add-drop-table: add a DROP TABLE statement before each CREATE TABLE statement.
4.1 to earlier version backwards compabibility: Bug 203 notes that database names are only quoted using -q from mysqldump version 10.1 (part of 4.1) because the mysql command line program does not support quoted database names until version 13.0. Version 12 is the one distributed with 4.0. This can be a problem if you are using mysqldump 10.1 and need quoted table and field names but can't have quoted database names for import into a 4.0 or earlier server. Using the later mysql client program on the destination or the earlier mysqldump version on the source may be a workaround.
Bug 203: https://bugs.mysql.com/bug.php?id=203
Following Lon B helpful post:
You can pipe it to gzip to compress in windows. I didn't think it would work on windows, but apparently it does.
@ECHO Beginning backup of %dbname%...
%mysqldir%\bin\mysqldump -B %dbname% -u %dbuser% | gzip> %bkupdir%\dbBkup_%dbname%_%yy%%mm%%dd%.sql.gz
Of course,you need gng gzip in your path or directory.
When using mysqldump on a replication master, if you want the slave(s) to follow, you may want to avoid the --delete-master-logs option, because it can delete binary logs before the "CHANGE MASTER" is read by the slaves, therefore breaking the replication (then you have to issue manually the "CHANGE MASTER" on the slave(s)). If you want to get rid of old and useless binary logs, it is better to issue a "PURGE MASTER" SQL command on the master after the mysqldump.
I moved my MySQL installation from Linux to Windows 2003 and had to create a new backup script. I was using hotcopy but with windows it's not avaliable.
So, Inspired by Lon B and Corey Tisdale (above) I created a batch file that will create a mysqldump GZiped file for each database and put them into seperate folders. It also creates a log file. You will have to set the vars at the top to match your system.
You will also need GZip to do the compression...
It could still use some work (like no error trapping etc...) but it's in production for me now.
I used a utility "commail.exe" to send the log file to me after the backup is complete.
//--- Begin Batch File ---//
@echo off
:: Set some variables
set bkupdir=E:\MySQL\backup
set mysqldir=E:\MySQL
set datadir=E:\MySQL\data
set logdir=E:\MySQL\logs
set dbuser=username
set dbpass=password
set zip=C:\GZip\bin\gzip.exe
set endtime=0
:GETTIME
:: get the date and then parse it into variables
for /F "tokens=2-4 delims=/ " %%i in ('date /t') do (
set mm=%%i
set dd=%%j
set yy=%%k
)
:: get the time and then parse it into variables
for /F "tokens=5-8 delims=:. " %%i in ('echo.^| time ^| find "current" ') do (
set hh=%%i
set ii=%%j
set ss=%%k
)
:: If this is the second time through then go to the end of the file
if "%endtime%"=="1" goto END
:: Create the filename suffix
set fn=_%yy%%mm%%dd%_%hh%%mm%%ss%
:: Switch to the data directory to enumerate the folders
pushd %datadir%
:: Write to the log file
echo Beginning MySQLDump Process > %logdir%\LOG%fn%.txt
echo Start Time = %yy%-%mm%-%dd% %hh%:%ii%:%ss% >> %logdir%\LOG%fn%.txt
echo --------------------------- >> %logdir%\LOG%fn%.txt
echo. >> %logdir%\LOG%fn%.txt
:: Loop through the data structure in the data dir to get the database names
for /d %%f in (*) do (
:: Create the backup sub-directory is it does not exist
if not exist %bkupdir%\%%f\ (
echo Making Directory %%f
echo Making Directory %%f >> %logdir%\LOG%fn%.txt
mkdir %bkupdir%\%%f
) else (
echo Directory %%f Exists
echo Directory %%f Exists >> %logdir%\LOG%fn%.txt
)
:: Run mysqldump on each database and compress the data by piping through gZip
echo Backing up database %%f%fn%.sql.gz
echo Backing up database %%f%fn%.sql.gz >> %logdir%\LOG%fn%.txt
%mysqldir%\bin\mysqldump --user=%dbuser% --password=%dbpass% --databases %%f --opt --quote-names --allow-keywords --complete-insert | %zip% > %bkupdir%\%%f\%%f%fn%.sql.gz
echo Done...
echo Done... >> %logdir%\LOG%fn%.txt
)
:: Go back and get the end time for the script
set endtime=1
goto :GETTIME
:END
:: Write to the log file
echo. >> %logdir%\LOG%fn%.txt
echo --------------------------- >> %logdir%\LOG%fn%.txt
echo MySQLDump Process Finished >> %logdir%\LOG%fn%.txt
echo End Time = %yy%-%mm%-%dd% %hh%:%ii%:%ss% >> %logdir%\LOG%fn%.txt
echo. >> %logdir%\LOG%fn%.txt
:: Return to the scripts dir
popd
:: Send the log file in an e-mail
c:\commail\commail -host=smtp.yourcompany.com -from="server <server@yourcompany.com>" -to=serveradmins@yourcompany.com -subject="MySQL Backup" -msg=%logdir%\LOG%fn%.txt
//--- End Batch File ---//
Here's a bash wrapper for mysqldump I cron'd to run at night. It's not the sexiest thing but it's reliable.
It creates a folder for each day, a folder for each db & single bzip2'd files for each table. There are provisions for exclusions. See below where it skips the entire tmp & test db's and in all db's, tables tbl_session & tbl_parameter. It also cleans up files older than 5 days (by that time they've gone to tape).
Be sure to update <user> & <pwd>. Ideally these would be in constants but I couldn't get the bash escaping to work.
# setup
suffix=`date +%Y%m%d`
dest=/mirror/mysqldumps
cmd='/usr/bin/mysqldump'
databases=(`echo 'show databases;' | mysql -u <user> --password='<pwd>' | grep -v ^Database$`)
for d in "${databases[@]}"; do
if [[ $d != 'tmp' && $d != 'test' ]]
then
echo "DATABASE ${d}"
s="use ${d}; show tables;"
tables=(`echo ${s} | mysql -u <user> --password='<pwd>' | grep -v '^Tables_in_'`)
for t in "${tables[@]}"; do
if [[ $t != 'tbl_parameter' && $t != 'tbl_session' ]]
then
echo " TABLE ${t}"
path="${dest}/${suffix}/${d}"
mkdir -p ${path}
${cmd} --user=<user> --password='<pwd>' --quick --add-drop-table --all ${d} ${t} | bzip2 -c > ${path}/${t}.sql.bz2
fi
done
fi
done
# delete old dumps (retain 5 days)
find ${dest} -mtime +5 -exec rm {} \;
You always wanted to BACKUP your most important database somewhere in your Linux system, as well as send the dump by email, so that you can recover the entire content if the system crashes.
You can use these 2 scripts.
First Step:
-Install the mutt client that will transfer emails on the command-line : "apt-get install mutt"
-Create the backup directory : "mkdir /home/backups"
Second Step:
- Copy these 2 scripts on your root directory or your user directory :
#!/bin/sh
# Script name : auto_mysql_dump.sh
# Backup the dbname database
dir=`date +%Y-%m-%d`
dbname=`mybase`
if [ -d /home/backups ]; then
mkdir /home/backups/$dir
mysqldump -B --user=user_of_my_base --password=pwd_of_my_base --host=host_of_my_base $dbname > /home/backups/$dir/$dbname.sql
fi
# End of script auto_mysql_dump.sh
#!/bin/sh
# Script Name : auto_mail_dump.sh
# Sends an email with the dump realized before
dir=`date +%Y-%m-%d`
dbname=`mybase`
mutt -s "Today backup" -a /home/backups/$dir/$dbname.sql user@tosend.com < /dev/null
# End of script auto_mail_dump.sh
-Don't forget to change the access to make them executable:
"chmod 700 auto_mysql_dump.sh"
"chmod 700 auto_mail_dump.sh"
Third step:
-Edit the CronTab to schedule the execution of the two scripts.
"crontab -e" (you will use the vi editor)
We consider that the 2 scripts are in the /root directory
-I want the dump to be executed at 8.30 everyday
-I want the mail to be sent at 9.00 everyday
Thus I add these 2 rows after the existing lines :
Hit the "i" to insert new characters...
30 8 * * * /root/auto_mysql_dump.sh > /dev/null
00 9 * * * /root/auto_mail_dump.sh > /dev/null
Save the crontab by hitting : "Esc" + ":wq" (means Write and Quit)
What you should do now :
Once you've written the scripts, test-them !
Enjoy the automatic backup from now on :-)
When you need to import the data from a mysqldump, instead of using "shell>mysql < dump.sql" using "mysql> source dump.sql" is much better.
This way of importing the data avoids problems with language specific characters being turned into garble.
Here's a DOS script that will backup all your databases to a seperate file in a new folder, zip the folder, encrypt the zip and email the encrypted zip to one or many adresses. If the backup is larger than a specified limit only the logfile is emailed. The unencrypted zipfile is left on your local machine.
The script is also available at https://www.jijenik.com/projects/mysqlbackup/
Many thanks to Wade Hedgren whose script formed the basis for this version.
//--- Begin Batch File ---//
::
:: Creates a backup of all databases in MySQL.
:: Zip, encrypts and emails the backup file.
::
:: Each database is saved to a seperate file in a new folder.
:: The folder is zipped and then deleted.
:: the zipped backup is encrypted and then emailed, unless the file exceeds the maximum filesize
:: In all cases the logfile is emailed.
:: The encrypted backup is deleted, leaving the unencrypted zipfile on your local machine.
::
:: Version 1.1
::
:: Changes in version 1.1 (released June 29th, 2006)
:: - backups are now sent to the address specified by the mailto variable
::
:: The initial version 1.0 was released on May 27th, 2006
::
::
:: This version of the script was written by Mathieu van Loon (mathieu-public@jijenik.com)
:: It is based heavily on the script by Wade Hedgren (see comments at https://dev.mysql.com/doc/refman/5.0/en/mysqldump.html)
::
:: This script requires several freeware libraries:
:: - zipgenius (a compression tool), www.zipgenius.it
:: - blat (an emailer tool), www.blat.net
:: - doff (extracts datetime, ignores regional formatting), www.jfitz.com/dos/index.html
::
:: Some areas where this script could be improved:
:: - include error trapping and handling
:: - make steps such as encryption and email optional
:: - allow the user to specify a single database on the command line
::
@echo off
::
:: Configuration options
::
:: The threshold for emailing the backup file. If the backup is larger
:: it will not be emailed (the logfile is always sent).
set maxmailsize=10000000
:: The passphrase used to encrypt the zipfile. Longer is more secure.
set passphrase=secret
:: Name of the database user
set dbuser=root
:: Password for the database user
set dbpass=password
:: Recipients of database backup, comma seperated, enclosed in quotes
set mailto="backups@example.com,backups2@example.com"
:: From address for email
set mailfrom="MySQL Backup Service <noreply@example.com>"
:: Email server
set mailsmtp=localhost
:: Email subject
set mailsubject="MySQL Backup"
:: directory where logfiles are stored
set logdir=C:\DatabaseBackups\logs
:: directory where backup files are stored
set bkupdir=C:\DatabaseBackups
:: Install folder of MySQL
set mysqldir=C:\Program Files (x86)\MySQL\MySQL Server 4.1
:: Data directory of MySQL (only used to enumerate databases, we use mysqldump for backup)
set datadir=C:\Program Files (x86)\MySQL\MySQL Server 4.1\data
:: Path of zipgenius compression tool
set zip=C:\Program Files (x86)\ZipGenius 6\zg.exe
:: Path of blat mail tool
set mail=C:\DatabaseBackups\Backupscript\libraries\Blat250\full\blat.exe
:: Path of doff date tool (specify only the folder not the exe)
set doff=C:\DatabaseBackups\Backupscript\libraries\doff10
::
::
:: NO NEED TO CHANGE ANYTHING BELOW
::
::
:: get the date and then parse it into variables
pushd %doff%
for /f %%i in ('doff.exe yyyymmdd_hhmiss') do set fn=%%i
for /f %%i in ('doff.exe dd-mm-yyyy hh:mi:ss') do set nicedate=%%i
popd
set logfile="%logdir%\%fn%_Backuplog.txt"
:: Switch to the data directory to enumerate the folders
pushd "%datadir%"
:: Write to the log file
echo Beginning MySQLDump Process > %logfile%
echo Start Time = %nicedate% >> %logfile%
echo --------------------------- >> %logfile%
echo. >> %logfile%
:: Create the backup folder
if not exist "%bkupdir%\%fn%\" (
echo Making Directory %fn%
echo Making Directory %fn% >> %logfile%
mkdir "%bkupdir%\%fn%"
)
:: Loop through the data structure in the data dir to get the database names
for /d %%f in (*) do (
:: Run mysqldump on each database and compress the data by piping through gZip
echo Backing up database %fn%_%%f.sql
echo Backing up database %fn%_%%f.sql >> %logfile%
"%mysqldir%\bin\mysqldump" --user=%dbuser% --password=%dbpass% --databases %%f --opt --quote-names --allow-keywords --complete-insert > "%bkupdir%\%fn%\%fn%_%%f.sql"
echo Done... >> %logfile%
)
:: return from data dir
popd
pushd %bkupdir%
echo Zipping databases
echo Zipping databases >> %logfile%
REM C9 : maximum compression
REM AM : Delete source files
REM F1 : Store relative path
REM R1 : include subfolders
REM K0 : Do not display progress
"%zip%" -add "%fn%_MySQLBackup.zip" C9 AM F1 R1 K0 +"%bkupdir%\%fn%"
echo Crypting zipfile
echo Crypting zipfile >> %logfile%
REM C : Create non-executable zip
REM S : Do not delete after x tries
REM 3 : Use AES encryption
"%zip%" -encrypt "%fn%_MySQLBackup.zip" C S 3 "%passphrase%" %mailfrom%
echo Deleting directory %fn%
echo Deleting directory %fn% >> %logfile%
rmdir /s /q "%bkupdir%\%fn%"
:: Go back and get the end time for the script
set endtime=1
:: return from backup dir
popd
:: update the nicedate for the log
pushd %doff%
for /f %%i in ('doff.exe dd-mm-yyyy hh:mi:ss') do set nicedate=%%i
popd
:: Write to the log file
echo. >> %logfile%
echo --------------------------- >> %logfile%
echo MySQLDump Process Finished >> %logfile%
echo End Time = %nicedate% >> %logfile%
echo. >> %logfile%
:: Send the log file in an e-mail, include the backup file if it is not too large
:: We use the CALL Trick to enable determination of the filesize (type CALL /? at prompt for info)
:: note that you _must_ specify the full filename as the argument
pushd %bkupdir%
Call :MAILFILE "%bkupdir%\%fn%_MySQLBackup.czip"
echo Backup completed
goto :EOF
:MAILFILE
if /i %~z1 LSS %maxmailsize% (
echo Emailing backup file
"%mail%" %logfile% -q -attach %1 -serverSMTP %mailsmtp% -f %mailfrom% -to %mailto% -subject %mailsubject%
) ELSE (
echo Size of backup file %~z1 B exceeds configured email size %maxmailsize% B.
echo Emailing logfile only
echo Size of backup file %~z1 B exceeds configured email size %maxmailsize% B. only emailing logfile. >> %logfile%
"%mail%" %logfile% -q -serverSMTP %mailsmtp% -f %mailfrom% -to %mailto% -subject %mailsubject%
)
echo Deleting encrypted backup file
del %1
popd
//--- End Batch File ---//
RE: Mathieu van Loon
Excellent, I had this installed and configured in about 10 minutes. I do have one minor fix however.
You aren't getting the time portion of the DOFF command captured into your variable. It appears that the output formatting string MUST NOT CONTAIN ANY BLANKS so I changed mine to:
for /f %%i in ('doff.exe dd-mm-yyyy_at_hh:mi:ss') do set nicedate=%%i
This is terrific, wish I found it 10 hrs ago (darn mySQL Administrator Backup - such a waste!!!
***
Now the problem is that my backups won't restore.... I am backing up multiple instances of MediaWiki, Mantis, and Joomla. I'm playing around with the
--max_allowed_packet= nnn and that should fix it based on manual backups working. Now is that nnn bytes or an abbreviation? Hmmm.
I often get errors [MySQL 4.* and 5.*] on reloading a dump of databases having big blobs. I found the solution disabling the --extended-insert (that comes inside the multiple option --opt, enabled by default) with --skip-extended-insert. I think this way is safer, but it is also more more slow.
To backup certain tables in a database, you have to set the -B operator (for the proper database) AND use the --tables operator to list the tables you wish to backup.
Example:
mysqldump --user=.. --password=.. -B <Database> --tables <Table1> <Table2> <Table3> <Table4>
Here's a python script that does rolling WinRAR'd backups on Windows. It should be trivial to change to Linux, or another compression program.
Please note:
1) this was a quick hack, so please test thoroughly before using in production. Still, I hope it will be a useful basis for your own script.
2) the --single-transaction switch is used as I am backing up InnoDB tables.
3) mysqldump is run with the root user. It would be A Good Thing to make this more secure - eg. create a backup user with read-only permissions to the tables.
4) <tab> is the tab character. Indentation is significant in Python.
import glob
import os
import time
# configuration
baseBackupFileName = "backupName"
maxBackups = 3
mySqlDumpCommand = "d:\\programs\\mysql\\bin\\mysqldump --user=root --password=rootpass --single-transaction DBName Table1Name Table2Name Table3Name"
winRarPath = "\"c:\\Program Files\\WinRAR\\WinRAR.exe\"" # path is quoted as it contains spaces
print "--- START ---"
# create new backup
newBackupFileName = baseBackupFileName + time.strftime("_%Y%m%d_%H%M%S", time.localtime())
os.system(mySqlDumpCommand+" > "+newBackupFileName+".sql")
# compress new backup
os.system(winRarPath+" a "+newBackupFileName+" "+newBackupFileName+".sql")
os.remove(newBackupFileName+".sql")
print "Created new backup \""+newBackupFileName+".rar\""
# delete old backups
oldBackupFileNames = glob.glob(baseBackupFileName+"_*_*.rar")
oldBackupFileNames.sort()
if len(oldBackupFileNames) > maxBackups:
<tab>for fileName in oldBackupFileNames[0:len(oldBackupFileNames)-maxBackups]:
<tab><tab>os.remove(fileName)
<tab><tab>print "Deleted old backup \""+fileName+"\""
print "--- END ---"
It seems one needs to be careful when using --skip-opt with databases containing non-ascii latin1 characters, especially if one has not been paying much attention to character sets.
I am just using default character sets - normally latin1. However, the dump produced by mysqldump is, perhaps surprisingly, in utf8. This seems fine, but leads to trouble with the --skip-opt option to mysqldump, which turns off --set-charset but leaves the dump in utf8.
This seems to lead to a dump that will be silently incorrectly reloaded if strings in the database contain non-ascii latin1 characters.
(Is this a documentation flaw, a design flaw or a bug??)
Perhaps the fact that mysqldump uses utf8 by default, and the importance of the --set-charset option should be more prominently documented (see the documentation for the --default-character-set attribute for the current mention of the use of utf8)
I am fairly new to bash scripting, however I encountered the problem of all the databases going into one *.sql file. If you have a large amount of databases to backup it can take forever to restore your backup. This is a script I wrote to accomplish what I felt was needed. It grabs the name of the database and puts them in separate *.sql.bz2 files with a corresponding timetamp. Please let me know if this helps and perhaps if I can make it more elegant.
#!/bin/sh
TIME_1=`date +%s`
cd /backup/mysql
DBS="$(mysql --user=youruser --password=yourpass -Bse 'show databases')"
for db in ${DBS[@]}
do
echo ${db}-$(date +%m-%d-%y).sql.bz2 is being saved in /backup/mysql
# remember to add the options you need with your backups here.
mysqldump --user=youruser --password=yourpass $db --single-transaction -R | bzip2 -c > ${db}-$(date +%m-%d-%y).sql.bz2
done
TIME_2=`date +%s`
elapsed_time=$(( ( $TIME_2 - $TIME_1 ) / 60 ))
## just a sanity check to make sure i am not running a dump for 4 hours
echo "This mysql dump ran for a total of $elapsed_time minutes." > mysql_dump_runtime.txt
# delete old databases. I have it setup on a daily cron so
# anything older than 60 minutes is fine
for del in $(find /backup/mysql -name '*.sql.bz2' -mmin +60)
do
echo This directory is more than one day old and it is being removed: $del
rm $del
done
#!/bin/bash
# SEE : https://dev.mysql.com/doc/refman/5.0/en/mysqldump.html
# SEE : https://safari.oreilly.com/0596526784/date_and_time_string_formatting_with_strftime
#
# Improved by Bill Hernandez (Plano, Texas) on Tuesday, August 21, 2007 (12:55 AM)
# ( 1 ) Backs up all info to time stamped individual directories, which makes it easier to track
# ( 2 ) Now maintains a single log that contains additional information
# ( 3 ) Includes a file comment header inside each compressed file
# ( 4 ) Used more variables instead of hard-code to make routine easier to use for something else
# ( 5 ) Where I have mysql5, you may have to replace it with mysql
#
# Posted by Ryan Haynes on July 11 2007 6:29pm
# DO NOT DELETE AUTOMATICALLY FOR NOW, MAYBE LATER
DELETE_EXPIRED_AUTOMATICALLY="TRUE"
# DELETE EXPIRED BACKUPS THAT ARE MORE THAN
# expire_minutes=$(( 1 * 30 )) # 30 minutes old
# expire_minutes=$(( 60 * 24 )) # 1 day old
# expire_minutes=$(( 60 * 24 * 7 )) # 7 days old
# expire_minutes=$(( 60 * 24 * 30 )) # 30 days old
expire_minutes=$(( 60 * 24 * 7 )) # 7 days old
if [ $expire_minutes -gt 1440 ]; then
expire_days=$(( $expire_minutes /1440 ))
else
expire_days=0
fi
function pause(){
read -p "$*"
}
# pause "HIT RETURN, and then enter your sudo password..."
echo "Please enter your sudo password..."
sudo echo
mysql_username="your_mysql_username"
mysql_password="your_mysql_password"
current_dir=`pwd`
echo -n "Current working directory is : "
echo $current_dir
echo "------------------------------------------------------------------------"
TIME_1=`date +%s`
TS=$(date +%Y.%m.%d\-%I.%M.%p)
BASE_DIR=/backups/mysql
BACKUP_DIR=${BASE_DIR}/$TS
BACKUP_LOG_NAME=mysql_dump_runtime.log
BACKUP_LOG=${BASE_DIR}/${BACKUP_LOG_NAME}
sudo mkdir -p $BACKUP_DIR
sudo chown mysql:admin $BACKUP_DIR
sudo chmod 775 $BASE_DIR
sudo chmod -R 777 $BACKUP_DIR
cd $BACKUP_DIR
echo -n "Changed working directory to : "
pwd
echo "Saving the following backups..."
echo "------------------------------------------------------------------------"
DBS="$(mysql5 --user=${mysql_username} --password=${mysql_password} -Bse 'show databases')"
for db in ${DBS[@]}
do
normal_output_filename=${db}.sql
compressed_output_filename=${normal_output_filename}.bz2
echo $compressed_output_filename
# remember to add the options you need with your backups here.
echo "-- $compressed_output_filename - $TS" > $normal_output_filename
echo "-- Logname : `logname`" >> $normal_output_filename
# mysqldump5 --user=${mysql_username} --password=${mysql_password} $db --single-transaction -R | bzip2 -c > $compressed_output_filename
mysqldump5 --user=${mysql_username} --password=${mysql_password} $db --single-transaction -R >> $normal_output_filename
bzip2 -c $normal_output_filename > $compressed_output_filename
rm $normal_output_filename
done
echo "------------------------------------------------------------------------"
TIME_2=`date +%s`
elapsed_seconds=$(( ( $TIME_2 - $TIME_1 ) ))
elapsed_minutes=$(( ( $TIME_2 - $TIME_1 ) / 60 ))
# just a sanity check to make sure i am not running a dump for 4 hours
cd $BASE_DIR
echo -n "Changed working directory to : "
pwd
echo "Making log entries..."
if [ ! -f $BACKUP_LOG ]; then
echo "------------------------------------------------------------------------" > ${BACKUP_LOG_NAME}
echo "THIS IS A LOG OF THE MYSQL DUMPS..." >> ${BACKUP_LOG_NAME}
echo "DATE STARTED : [${TS}]" >> ${BACKUP_LOG_NAME}
echo "------------------------------------------------------------------------" >> ${BACKUP_LOG_NAME}
echo "[BACKUP DIRECTORY ] [ELAPSED TIME]" >> ${BACKUP_LOG_NAME}
echo "------------------------------------------------------------------------" >> ${BACKUP_LOG_NAME}
fi
echo "[${TS}] This mysql dump ran for a total of $elapsed_seconds seconds." >> ${BACKUP_LOG_NAME}
echo "------------------------------------------------------------------------" >> ${BACKUP_LOG_NAME}
# delete old databases. I have it setup on a daily cron so anything older than 60 minutes is fine
if [ $DELETE_EXPIRED_AUTOMATICALLY == "TRUE" ]; then
counter=0
for del in $(find $BASE_DIR -name '*-[0-9][0-9].[0-9][0-9].[AP]M' -mmin +${expire_minutes})
do
counter=$(( counter + 1 ))
echo "[${TS}] [Expired Backup - Deleted] $del" >> ${BACKUP_LOG_NAME}
done
echo "------------------------------------------------------------------------"
if [ $counter -lt 1 ]; then
if [ $expire_days -gt 0 ]; then
echo There were no backup directories that were more than ${expire_days} days old:
else
echo There were no backup directories that were more than ${expire_minutes} minutes old:
fi
else
echo "------------------------------------------------------------------------" >> ${BACKUP_LOG_NAME}
if [ $expire_days -gt 0 ]; then
echo These directories are more than ${expire_days} days old and they are being removed:
else
echo These directories are more than ${expire_minutes} minutes old and they are being removed:
fi
echo "------------------------------------------------------------------------"
echo "\${expire_minutes} = ${expire_minutes} minutes"
counter=0
for del in $(find $BASE_DIR -name '*-[0-9][0-9].[0-9][0-9].[AP]M' -mmin +${expire_minutes})
do
counter=$(( counter + 1 ))
echo $del
rm -R $del
done
fi
fi
echo "------------------------------------------------------------------------"
cd `echo $current_dir`
echo -n "Restored working directory to : "
pwd
if you want to check the result of mysqldump in a shell, you should pay attention. this will not work:
mysqldump --all-databases | gzip -c > dumpfile.sql.gz
if [ $? -eq 0 ]
...
fi
since the "|gzip" will always return 0.
instead you should use a pipe:
### create a pipe named "pipe"
mkfifo pipe
### compress the pipe in background
gzip < pipe > dumpfile.sql.gz &
### write directly to the pipe
mysqldump --all-databases > pipe
### get the real return code of mysqldump
result=$?
### wait until the gzip completes
wait
### now it is safe to remove the pipe
rm pipe
kind regards....
--
Ludovico
Hi all,
And thanks for the great script examples - I've taken a bit of the batch files and made a perl script for backing up mysql databases - it's pretty crude, but it's what I'm using right now to back up the servers nightly.
It's done on a win2003 server box that has perl and gzip (https://gnuwin32.sourceforge.net/packages.html)installed.
I chose perl 'cause if I need to port the script to 'nix it's trivial. (well, so's the script, really) And it's easy to bang out small scripts in!
Hope this helps someone!
=============
#!/usr/bin/perl
## DB info struct
##{ host, db, username, password}
@DBsToBackup =(
##host 1
['192.168.0.1', 'database1', 'username', 'password'],
['192.168.0.1', 'database2', 'username', 'password'],
['192.168.0.1', 'database3', 'username', 'password'],
['192.168.0.1', 'database4', 'username', 'password'],
##host 2
['192.168.0.2', 'database5', 'username', 'password']
);
$backupdir = 'D:\\Database\\DBdumps';
print "Starting dump of databases...\n\n";
foreach $dbinfo (@DBsToBackup){
($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $DayOfYear, $IsDST) = localtime(time) ;
$Year += 1900 ; $Month += 1;
$outputFilename = sprintf("%04d%02d%02d[%02d%02d%02d]", $Year, $Month, $Day, $Hour, $Minute, $Second ) . "_$$dbinfo[1]_.sql.gz";
print "dumping: $$dbinfo[0] / $$dbinfo[1]...\n";
$ex = "\"C:\\Program Files\\MySQL\\MySQL\ Server\ 5.0\\bin\\mysqldump\" --user=$$dbinfo[2] --password=$$dbinfo[3] --host=$$dbinfo[0] --add-drop-database --add-drop-table $$dbinfo[1] | gzip > ${backupdir}\\$outputFilename";
print $ex ."\n\n";
print `$ex`;
}
print "DONE dumping\n";
print "deleting dumps older than 14 days...\n";
print `forfiles /P $backupdir /D -14 /C "cmd /c del @path\@file" /S`;
print "script finished\n";
==============
You can find this script and more at https://www.lancetek.com
Thanks,
Lance
Host on a shared server?
Then you will most probably not be able to create or retrieve a MySQL dump.
For a solution try this:
https://www.dwalker.co.uk/phpmysqlautobackup/
it will export your database and email to you. Its GPL open source.
This is a variation from the example above to deal with huge dump files that are too big to edit:
1. mysqldump --opt --user=username --password database > dumbfile.sql
2. Create file to execute prior to the dump file:
echo 'SET AUTOCOMMIT = 0;
SET FOREIGN_KEY_CHECKS=0;
' > pre.sql
3. Create a file to execute after the dump file is imported:
echo 'SET FOREIGN_KEY_CHECKS = 1;
COMMIT;
SET AUTOCOMMIT = 1;
' > post.sql
4. cat pre.sql dumpfile.sql post.sql | mysql --user=username --password database
In case this helps anyone. This backs up all databases and tables and keeps all files for a week, placing them in a directory structure of the format:
/backup_dir/db_name/day/table.sql.gz
Useful if you want to restore to a particular days data.
It checks new backups are different to last before overwriting files. This helps if you are rsyncing your filesystems as normally mysql writes a date into the dump so the files always appear to differ even if the data is the same.
Also saves a directory with just your schema in, checks and repairs tables where necessary and defrags tables on a Sunday.
<?php
// Change Me
$username = "root";
$password = "password";
$backup_dir = "/mnt/backup";
$dump = "/usr/bin/mysqldump";
$grep = "/bin/grep";
$gzip = "/bin/gzip";
// This should not need changing from here
function sql_dict($sql){
$x = mysql_query($sql);
if ($x) return mysql_fetch_assoc($x);
}
function cleanup($dir){
$d = dir($dir);
echo "Path: " . $d->path . "\n";
while (false !== ($entry = $d->read())) {
if ($entry=="." or $entry=="..") continue;
$e = str_replace(".sql.gz","",$entry);
$x = sql_dict("describe $e");
if (!$x) {
print "Removing old backup file [$entry]\n";
unlink("$dir/$entry");
}
}
$d->close();
}
function crc32_file($filename)
{
global $gzip;
$x = exec("$gzip --list --verbose $filename");
$x = explode(" ",$x);
return $x[1];
}
if (mysql_connect("localhost",$username,$password)) print "Connected.\n";
else die("Failed to connect to database.");
$dbs = mysql_query("show databases");
if ($dbs) while ($db = mysql_fetch_array($dbs, MYSQL_ASSOC)) {
$db = $db['Database'];
if ($db=="information_schema") continue;
if (mysql_select_db($db)) print "Selected [$db]\n";
else die("Failed to select db [$db]");
foreach (array("schema","data") as $pass){
$sql = mysql_query("show tables");
$day = date("l");
if ($pass=="schema") $dir = "/$backup_dir/$db/schema";
else $dir = "/$backup_dir/$db/$day";
if (!file_exists($dir)) system("mkdir -p $dir");
if (!file_exists($dir)) die("Couldn't Create $dir");
if ($pass=="data"){
$latest = "/$backup_dir/$db/latest";
unlink($latest);
system("/bin/ln -s \"$dir\" \"$latest\"");
}
cleanup($dir);
if ($sql) while ($s = mysql_fetch_assoc($sql)) {
if (!isset($s["Tables_in_{$db}"])) {
print "no result";
print_r($sql);
die();
}
$t = $s["Tables_in_{$db}"];
if (
$pass=="schema" ) $data = "--no-data";
else $data = "--lock-tables";
$tab = $t;
$lim = 30;
if (strlen($tab)>$lim) $tab = substr($tab,0,$lim-3)."...";
while (strlen($tab)<30) $tab .= " ";
print "BACKUP: $pass : $day : $db : $tab : ";
if ($pass=="data"){
print "Check : ";
$check = sql_dict("check table $t");
$check = $check['Msg_text'];
print "$check : ";
if ($check != "OK") {
print "Repair";
$repair = sql_dict("repair table $t");
$repair = $repair['Msg_text'];
print " : $repair : ";
}
if ($day=="Sunday"){
// optimize
print "Optimize : ";
$type = sql_dict("show table status like '$t'");
$type = $type['Engine'];
if ($type=="MyISAM") sql("optimize table $t");
if ($type=="InnoDB") sql("alter table $t engine='InnoDB'");
}
}
if (isset($argv[1])){
print "Skipping dump\n";
} else {
$temp = "/tmp/backup.$t.sql.gz";
$out = "$dir/$t.sql.gz";
print "Dump : ";
$cmd = "$dump -u$username -p$password $data --quick --add-drop-table $db $t | $grep -v 'Dump completed' | $gzip -n > $temp";
system($cmd);
print "CRC32 : ";
if (!file_exists($out)){
print "Saving : ";
$cmd = "/bin/mv $temp $out";
system($cmd);
} else {
$md5 = crc32_file($temp);
$nmd5 = crc32_file($out);
if ($md5!=$nmd5) {
print "Saving : ";
$cmd = "/bin/mv $temp $out";
system($cmd);
} else {
print "Skipped : ";
unlink($temp);
}
}
$size = filesize($out);
print "[$size]\n";
}
}
}
}
?>
Here is example of usage mysqldump with rdiff-backup inside of noe server with synchronization using rsync:
#!/bin/sh
# Incremental backup script using rdiff
# Author: Driantsov Alexander
# Requirements:
# * rdiff-backup - https://www.nongnu.org/rdiff-backup/
# * rsync
# * ssh ;)
####
BACKUP_ADMIN_EMAIL="yourmail@yourserver.com"
BACKUP_MAIL_SUBJECT="`hostname`: SQL Backup Synchronization Result"
BACKUP_LOG="/var/log/backup-sql"
BACKUP_PRIO="20" # Priority for the MySQL dump and rdiff-backup Min: 20 Max: -20
BACKUP_TMP_DIR="/var/backup/mysql_tmp" # New dumps will be stored here
BACKUP_DIFF_DIR="/var/backup/hosting/mysql" # Diffs of dumps will be stored there
SYNC_SRV="BAC.KUP.SER.VER" # Remote server for backup storage
SYNC_USER="backup_user" # User at remote storage
SYNC_SPEED="200" # Limit Synchronization Bandwidth to this number of KB/s
SYNC_DIR="/backup/hosting/mysql" #Directory on Remote server to synchronize backups in
MYSQL_USER="admin" # MySQL user
MYSQL_PASSWD=`cat /etc/psa/.psa.shadow` # Password for MySQL. You may obtain password from /etc/psa/.psa.shadow if you are using Plesk on your server.
#Implementaition
RSCONSTR="$SYNC_USER@$SYNC_SRV"
# Dump
echo "Backup Started at `date`" > $BACKUP_LOG
load_average=`uptime|awk '{print $10" "$11" "$12}'`
echo "Load overage at start: $load_average" >> $BACKUP_LOG
echo "\nBackingUP MySQL:" >> $BACKUP_LOG
for i in `mysql -u$MYSQL_USER -p$MYSQL_PASSWD -Bse "show databases"`; do echo "Backing up database $i..." >> $BACKUP_LOG ; nice -n $BACKUP_PRIO mysqldump --single-transaction --quick --skip-extended-insert -u$MYSQL_USER -p$MYSQL_PASSWD $i > $BACKUP_TMP_DIR/$i.dump; done
echo "MySQL dump completed at `date`" >> $BACKUP_LOG
load_average=`uptime|awk '{print $10" "$11" "$12}'`
echo "\nLoad overage after MySQL dumps: $load_average\n" >> $BACKUP_LOG
# Diff
echo "Looking for difference in database" >> $BACKUP_LOG
nice -n $BACKUP_PRIO rdiff-backup $BACKUP_TMP_DIR $BACKUP_DIFF_DIR/$HOSTNAME 2>&1 >> $BACKUP_LOG
echo "Done" >> $BACKUP_LOG
echo "Rdiff-backup completed at `date`" >> $BACKUP_LOG
load_average=`uptime|awk '{print $10" "$11" "$12}'`
echo "\nLoad overage after rdiff-backup: $load_average\n" >> $BACKUP_LOG
# Synchronize
sql_result=`rsync -avz --bwlimit $SYNC_SPEED $BACKUP_DIFF_DIR $RSCONSTR:$SYNC_DIR|tail -n 2`
free_space_info=`ssh $RSCONSTR df -h --sync -t ext3`
echo -en " MySQL backup synchronization:\n $sql_result \nSynchronization completed at `date` \n\nInformation on free space on remote backup server: \n $free_space_info \n\n Backup Log: \n\n`cat $BACKUP_LOG`"| mail -s "$BACKUP_MAIL_SUBJECT" $BACKUP_ADMIN_EMAIL
This is an example of a Windows batch script that implements a rotating archive of backups on a daily, weekly, and monthly basis. It also provides an installation option for the creation of the backup directories and an option to add a scheduled task to the system to run the batch file.
I started with what Lon B posted and many editions/revisions later this was produced. I hope you find it as useful as we have.
~~~ BEGIN FILE ~~~
@ECHO OFF
SET VERSIONMAJOR=10
SET VERSIONMINOR=6
FOR /f "tokens=1-4 delims=/ " %%a IN ('date/t') DO (
SET dw=%%a
SET mm=%%b
SET dd=%%c
SET yy=%%d
)
REM *** VERIFY AND UPDATE THESE SETTINGS BEFORE INITIAL RUN ***
REM *** mysqldir must point to the \bin directory! ***
SET bkupdir=C:\MySQL-Backups
SET mysqldir=C:\wamp\bin\mysql\mysql5.0.51b\bin
SET dbhost=localhost
SET dbuser=
SET dbpass=
REM *** END USER CONFIGURABLE SETTINGS ***
IF /i "%1" == "--INSTALL" GOTO INSTALLER
IF /i "%1" == "--CREATEDIRS" GOTO CREATEDIRS
IF /i "%1" == "--ADDSCHEDULEDTASK" GOTO TASKSCHED
IF ""%1"" == """" GOTO ALLDB
IF /i "%1" == "--ALL" GOTO ALLDB
IF /i "%1:~0,2%" == "--" GOTO PARAMERROR
SET ALLDBS=0
SET dbnames=%1
SET dbnamesf=%1
SHIFT
:setArgs
IF ""%1""=="""" GOTO BKUP
SET dbnames=%dbnames% %1
SET dbnamesf=%dbnamesf%_%1
SHIFT
GOTO setArgs
:ALLDB
SET ALLDBS=1
SET dbnames=ALL DATABASES
SET dbnamesf=ALL_DATABASES
:BKUP
@ECHO MySQLdump script for Windows v%VERSIONMAJOR%.%VERSIONMINOR% > %bkupdir%\dbBkup_%dbnamesf%_%yy%%mm%%dd%.log
@ECHO. >> %bkupdir%\dbBkup_%dbnamesf%_%yy%%mm%%dd%.log
@ECHO MySQLdump script for Windows v%VERSIONMAJOR%.%VERSIONMINOR%
@ECHO.
IF NOT EXIST %bkupdir%\INSTALLED.OK (
@ECHO DIRECTORY STRUCTURE NOT IN PLACE. >> %bkupdir%\dbBkup_%dbnamesf%_%yy%%mm%%dd%.log
@ECHO PLEASE RUN %0 --INSTALL OR %0 --CREATEDIRS >> %bkupdir%\dbBkup_%dbnamesf%_%yy%%mm%%dd%.log
@ECHO FAILED TO BACKUP DATABASES. >> %bkupdir%\dbBkup_%dbnamesf%_%yy%%mm%%dd%.log
@ECHO DIRECTORY STRUCTURE NOT IN PLACE.
@ECHO PLEASE RUN %0 --INSTALL OR %0 --CREATEDIRS
@ECHO FAILED TO BACKUP DATABASES.
GOTO BOTTOM
)
@ECHO Beginning backup of %dbnames%... >> %bkupdir%\dbBkup_%dbnamesf%_%yy%%mm%%dd%.log
@ECHO Beginning backup of %dbnames%...
IF %ALLDBS% == 1 (
SET dumpparams=--host=%dbhost% -u %dbuser% -p%dbpass% -A -f -x -q --create-options --flush-privileges -r %bkupdir%\dbBkup_%dbnamesf%_%yy%%mm%%dd%.sql
) ELSE (
SET dumpparams=--host=%dbhost% -u %dbuser% -p%dbpass% -f -x -q --create-options --flush-privileges -r %bkupdir%\dbBkup_%dbnamesf%_%yy%%mm%%dd%.sql --databases %dbnames%
)
%mysqldir%\mysqldump %dumpparams% >> %bkupdir%\dbBkup_%dbnamesf%_%yy%%mm%%dd%.log
@ECHO Done! New File: dbBkup_%dbnamesf%_%yy%%mm%%dd%.sql >> %bkupdir%\dbBkup_%dbnamesf%_%yy%%mm%%dd%.log
@ECHO Done! New File: dbBkup_%dbnamesf%_%yy%%mm%%dd%.sql
COPY /Y %bkupdir%\dbBkup_%dbnamesf%_%yy%%mm%%dd%.sql /A %bkupdir%\Daily\dbBkup_%dbnamesf%_%dw%.sql /A > NUL
@ECHO Created Daily Backup: Daily\dbBkup_%dbnamesf%_%dw%.sql >> %bkupdir%\dbBkup_%dbnamesf%_%yy%%mm%%dd%.log
@ECHO Created Daily Backup: Daily\dbBkup_%dbnamesf%_%dw%.sql
REM Check to see if it's time for the Weekend backup
IF /i "%dw%" NEQ "Sat" GOTO SKIPWKBK
IF EXIST %bkupdir%\Weekly\safety_%dbnamesf%_%yy%%mm%%dd%.txt GOTO WKCUR
IF NOT EXIST %bkupdir%\Weekly\dbBkup_%dbnamesf%_Current.sql GOTO WKCUR
IF NOT EXIST %bkupdir%\Weekly\dbBkup_%dbnamesf%_Previous.sql GOTO WKPRE
IF NOT EXIST %bkupdir%\Weekly\dbBkup_%dbnamesf%_Previous_2.sql GOTO WKPR2
MOVE /Y %bkupdir%\Weekly\dbBkup_%dbnamesf%_Previous_2.sql %bkupdir%\Weekly\dbBkup_%dbnamesf%_Previous_3.sql > NUL
:WKPR2
MOVE /Y %bkupdir%\Weekly\dbBkup_%dbnamesf%_Previous.sql %bkupdir%\Weekly\dbBkup_%dbnamesf%_Previous_2.sql > NUL
:WKPRE
MOVE /Y %bkupdir%\Weekly\dbBkup_%dbnamesf%_Current.sql %bkupdir%\Weekly\dbBkup_%dbnamesf%_Previous.sql > NUL
:WKCUR
COPY /Y %bkupdir%\dbBkup_%dbnamesf%_%yy%%mm%%dd%.sql /A %bkupdir%\Weekly\dbBkup_%dbnamesf%_Current.sql /A > NUL
@ECHO. > %bkupdir%\Weekly\safety_%dbnamesf%_%yy%%mm%%dd%.txt
@ECHO Created Weekly Backup: Weekly\dbBkup_%dbnamesf%_Current.sql >> %bkupdir%\dbBkup_%dbnamesf%_%yy%%mm%%dd%.log
@ECHO Created Weekly Backup: Weekly\dbBkup_%dbnamesf%_Current.sql
:SKIPWKBK
REM if (day >= 28) write EoM backup
IF %dd% GEQ 28 (
COPY /Y %bkupdir%\dbBkup_%dbnamesf%_%yy%%mm%%dd%.sql /A %bkupdir%\Monthly\dbBkup_%dbnamesf%_%mm%.sql /A > NUL
@ECHO Created End of Month Backup: Monthly\dbBkup_%dbnamesf%_%mm%.sql >> %bkupdir%\dbBkup_%dbnamesf%_%yy%%mm%%dd%.log
@ECHO Created End of Month Backup: Monthly\dbBkup_%dbnamesf%_%mm%.sql
)
DEL /q /f %bkupdir%\dbBkup_%dbnamesf%_%yy%%mm%%dd%.sql
@ECHO Backup stored in rotating archives. >> %bkupdir%\dbBkup_%dbnamesf%_%yy%%mm%%dd%.log
@ECHO. >> %bkupdir%\dbBkup_%dbnamesf%_%yy%%mm%%dd%.log
@ECHO End MySQLdump Script >> %bkupdir%\dbBkup_%dbnamesf%_%yy%%mm%%dd%.log
@ECHO Backup stored in rotating archives.
@ECHO.
@ECHO End MySQLdump Script
GOTO BOTTOM
:INSTALLER
@ECHO VERIFY: Path to mysqldump: %mysqldir%
@ECHO VERIFY: Path to backups: %bkupdir%
@ECHO VERIFY: MySQL User: %dbuser%
@ECHO VERIFY: MySQL Pass: %dbpass%
@ECHO VERIFY: MySQL Host: %dbhost%
IF NOT EXIST %bkupdir%\INSTALLED.OK (
@ECHO ALERT: Backup directory does not exist. Create base directory and subdirectories?
SET /p domkdir=[Y/N]:
IF /i "%domkdir%" == "N" (
@ECHO ALERT: CANNOT CONTINUE WITHOUT DIRECTORIES IN PLACE.
GOTO BOTTOM
)
)
:CREATEDIRS
IF NOT EXIST %bkupdir%\INSTALLED.OK (
MD "%bkupdir%" > NUL
MD "%bkupdir%\Daily" > NUL
MD "%bkupdir%\Weekly" > NUL
MD "%bkupdir%\Monthly" > NUL
@ECHO INSTALLED CORRECTLY > %bkupdir%\INSTALLED.OK
)
GOTO BOTTOM
:TASKSCHED
@ECHO Preparing add Scheduled Task...
:STUPIDUSER1
SET /p taskuser=Domain\User to run task:
IF /i ""%taskuser%"" == """" GOTO STUPIDUSER1
:STUPIDUSER2
SET /p taskpwd1=Password:
SET /p taskpwd2=Confirm Password:
IF %taskpwd1% NEQ %taskpwd2% GOTO STUPIDUSER2
:STUPIDUSER3
SET /p taskname=Task name:
IF /i ""%taskname%"" == """" GOTO STUPIDUSER3
SET /p taskparam=Parameters to pass to batch file:
SCHTASKS /Create /SC DAILY /ST 04:00:00 /TN "%taskname%" /TR "%~f0 %taskparam%" /RU "%taskuser%" /RP %taskpwd1%
GOTO BOTTOM
:PARAMERROR
@ECHO ERROR: Unknown Parameter Passed.
@ECHO Current supported parameters:
@ECHO --ALL - Backup all databases, same as passing nothing to batch file
@ECHO --ADDSCHEDULEDTASK - Adds a scheduled task for this process
@ECHO --CREATEDIRS - Creates Directory Structure
@ECHO --INSTALL - Creates directory structure and outputs configuration settings that need verification
:BOTTOM
To export a limited amount of records, you may use the following:
mysqldump -u [username] -p [dbname] {--opt} --where="true limit 100" > dump.sql
This adds the clause "where true limit 100" when creating the SELECT statement. (Adding "true" is a workaround: I ignore whether there is a way to add "limit 100" without adding the WHERE keyword).
Original post: https://www.geeknewz.com/board/index.php?automodule=blog&blogid=76&showentry=434
Add your own comment.