Dump
mysqldump db_name table_name > table_name.sql
Dumping from a remote database
mysqldump -u <db_username> -h <db_host> -p db_name table_name > table_name.sql
For further reference:
http://www.abbeyworkshop.com/howto/lamp/MySQL_Export_Backup/index.html
Restore
mysql -u <user_name> -p db_name mysql> source <full_path>/table_name.sql
or in one line
mysql -u username -p db_name < /path/to/table_name.sql
Credit: John McGrath
Dump
mysqldump db_name table_name | gzip > table_name.sql.gz
Restore
gunzip < table_name.sql.gz | mysql -u username -p db_name
try
for line in $(mysql -u... -p... -AN -e "show tables from NameDataBase"); do mysqldump -u... -p.... NameDataBase $line > $line.sql ; done
We can take a mysql dump of any particular table with any given condition like below
mysqldump -uusername -p -hhost databasename tablename --skip-lock-tables
If we want to add a specific where condition on table then we can use the following command
mysqldump -uusername -p -hhost databasename tablename --where="date=20140501" --skip-lock-tables
There are in general three ways to use mysqldump—in order to dump a set of one or more tables, a set of one or more complete databases, or an entire MySQL server—as shown here:
shell> mysqldump [options] db_name [tbl_name ...] shell> mysqldump [options] --databases db_name ... shell> mysqldump [options] --all-databases指定编码的参数为:
完整的官方文档:
http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html
Cyper实战:
#备份crazy_blog mysqldump -uroot -pxxx --default-character-set=utf8 crazy_blog|gzip > ~/backup/crazy_blog-$(date +%Y%m%d).sql.gz #还原 gunzip < ~/backup/crazy_blog-20141220.sql.gz |mysql -uroot -pxxx crazy_blog然后我将其配置为定时任务
crontab -e 0 0 * * * export d=$(date -d last-day +\%Y\%m\%d) && /usr/bin/mysqldump -uroot -pxxx --default-character-set=utf8 crazy_blog |gzip > ~/backup/crazy_blog-$d.sql.gz sudo service cron restart这里有个备份脚本,收藏:
#!/bin/sh export d=`date +%Y%m%d%H%M%S` mkdir -p /data/dbbak/baktables/$d for i in `echo "show tables" | mysql -u bak -ppassword db|grep -v Tables`; do echo $i; mysqldump --add-drop-table --allow-keywords -q -a -c -u bak -ppasswod mbook_hd $i > backup/$d/$i.sql done tar czf backup/$d.tar.gz backup/$d/ rm -rf backup/$d/ lftp -e "cd /dbbak/; mput -c $d.tar.gz; quit" -u bak,password sftp://192.168.0.138
注:写crontab的时候遇到了一点问题,参考这里http://stackoverflow.com/questions/3313413/syntax-error-only-when-command-is-run-from-cron解决了!