mysql备份与恢复


Dump and restore a single table from .sql

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

Dump and restore a single table from a compressed (.sql.gz) format

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
  • $line cotent names tables ;)
Hello, -AN(--no-auto-rehash, -A | --skip-column-names, -N Do not write column names in results.) -e(--execute=statement, -e statement | Execute the statement and quit. The default output format is like that produced with --batch.) fuente: dev.mysql.com/doc/refman/5.6/en/mysql-command-options.html –  robin.gomez


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

最后:

Syntax

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
指定编码的参数为:

--default-character-set=utf8

转自:
http://stackoverflow.com/questions/6682916/how-to-backup-a-single-table-in-a-mysql-database

完整的官方文档:
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解决了!



你可能感兴趣的:(mysql备份与恢复)