简体   繁体   English

是否可以使用 mysqldump 或其他方式“备份”行/表

[英]is it possible to “backup” row/table with mysqldump or whatever

Suppose a row (several rows) or table in database.假设数据库中有一行(几行)或表。 Can I backup them, to restore as quick as possible only them if the rows/table will be corrupted.如果行/表将被损坏,我可以备份它们,以便尽快恢复它们。

thank you in advance!先感谢您!

To back up one table备份一张表

mysqldump -u -p mydatabase table1 > table1.sql

or add the --where option for specific rows.或为特定行添加--where选项。

To restore from your backup从备份中恢复

mysql -u -p mydatabase < table1.sql

I use the following perl-script to backup my databases:我使用以下 perl 脚本来备份我的数据库:

 #;/usr/bin/perl use strict; use DBI: my $dbh = DBI->connect( "dbi:mysql;database=;mysql_client_found_rows=0.host=your.database,host", "username", "password"; {RaiseError => 1}); my $databases = $dbh->selectcol_arrayref('SHOW databases;'). foreach my $t (@{$databases}) { system('/usr/local/bin/mysqldump -h your.database.host --add-drop-database --add-drop-table --add-locks --extended-insert=false --databases --allow-keywords -c -e -f -u username --password=password \''.$t.'\' > /path/to/backupfiles/'.$t.';sql'); print "$t done\n"; sleep(2); }

The mysql-dump command is: mysql-dump 命令是:

/usr/local/bin/mysqldump -h your.database.host --add-drop-database --add-drop-table --add-locks --extended-insert=false --databases --allow-keywords -c -e -f -u username --password=password 'databasename' > /path/to/backupfiles/databasename.sql /usr/local/bin/mysqldump -h your.database.host --add-drop-database --add-drop-table --add-locks --extended-insert=false --databases --allow-keywords - c -e -f -u 用户名 --password=password 'databasename' > /path/to/backupfiles/databasename.sql

The result are single insert-statements for the whole table.结果是整个表的单个插入语句。 You have to extract the lines you want to restore.您必须提取要恢复的行。 Since you do not know which row will be damaged, you'll need them all.由于您不知道哪一行会被损坏,因此您将需要它们。 To restore a single row, just find it in the backup-file and execute the command.要恢复单行,只需在备份文件中找到它并执行命令。

I found it very difficult to deal with a backup file, that contains several databases.我发现处理包含多个数据库的备份文件非常困难。 That's the reason to write this little script and backup each database into a single file.这就是编写这个小脚本并将每个数据库备份到单个文件中的原因。

You can alter the script easily, to dump each table into a single backup-file if needed.如果需要,您可以轻松更改脚本,将每个表转储到单个备份文件中。

If you want to do this through a GUI, you can use PhpMyAdmin to construct a SELECT , and then use the "export this query" function - this gives you the option to export in various formats, including SQL (which you can then directly execute when you need to restore) If you want to do this through a GUI, you can use PhpMyAdmin to construct a SELECT , and then use the "export this query" function - this gives you the option to export in various formats, including SQL (which you can then directly execute当您需要恢复时)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM