Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
docs:mysql:mysqldump [2007/08/08 11:27] – created billh | docs:mysql:mysqldump [2008/08/03 00:25] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 10: | Line 10: | ||
===== examples ===== | ===== examples ===== | ||
+ | My example...adds create database statements too so the only thing you need to do is source the file to restore | ||
+ | < | ||
+ | mysqldump -u user -p --opt --databases " | ||
+ | </ | ||
+ | |||
The most normal use of mysqldump is probably for making a backup of whole databases. | The most normal use of mysqldump is probably for making a backup of whole databases. | ||
< | < | ||
- | mysqldump --opt database > backup-file.sql | + | shell> |
Note: --opt is the same as: --add-drop-table --add-locks --all --extended-insert --quick --lock-tables | Note: --opt is the same as: --add-drop-table --add-locks --all --extended-insert --quick --lock-tables | ||
</ | </ | ||
+ | Most likely, you will need to provide user credentials: | ||
+ | < | ||
+ | shell> mysqldump --opt -u user -p database > backup-file.sql | ||
+ | </ | ||
You can read this back into MySQL with: | You can read this back into MySQL with: | ||
< | < | ||
- | mysql database < backup-file.sql | + | shell> |
or | or | ||
- | mysql -e ' | + | shell> |
+ | |||
+ | or | ||
+ | |||
+ | mysql> source / | ||
+ | |||
+ | or, most likely... | ||
+ | |||
+ | mysql> create database databasename; | ||
+ | mysql> use databasename; | ||
+ | mysql> source / | ||
+ | |||
+ | * the source command can be followed with a path containing spaces, without escapes or quotes | ||
</ | </ | ||
More options and examples are available in the man page and in the MySQL manual | More options and examples are available in the man page and in the MySQL manual | ||
+ | |||
+ | ===== automated shell script ===== | ||
+ | < | ||
+ | #!/bin/bash | ||
+ | |||
+ | # user must have select and lock tables privileges | ||
+ | |||
+ | destination='/ | ||
+ | |||
+ | databases=" | ||
+ | database2 | ||
+ | database3" | ||
+ | |||
+ | user=username | ||
+ | pass=password | ||
+ | |||
+ | # make the destination folder, if it doesn' | ||
+ | if [ ! -d " | ||
+ | mkdir -p " | ||
+ | fi | ||
+ | |||
+ | # continue if the directory was successfully created | ||
+ | if [ -d " | ||
+ | for db in $databases | ||
+ | do | ||
+ | echo " | ||
+ | mysqldump --opt -u$user -p$pass " | ||
+ | done | ||
+ | fi | ||
+ | exit 0 | ||
+ | </ |