简体   繁体   中英

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.

To restore from your backup

mysql -u -p mydatabase < table1.sql

I use the following perl-script to backup my databases:

 #;/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:

/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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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