Skip to main content

selective table restore from mysqldump

I recently had to restore a corrupted table that had resulted from a full disk. I had been using automysqlbackup as my backup script. Each database had a corresponding backup file. It would have been easy to use a text editor to edit out the tables that you didn't want to restore but this database had grown to 1G.

STEP 1: Restore table structure

If the table structure is corrupted, you need to retrieve the CREATE TABLE statements from the backup. To do this you can use grep:

zcat backup.sql.gz | grep -A 20 'CREATE TABLE `tablename1`' > tablestructure.sql

This command will create tablestructure.sql containing the CREATE TABLE statement and 20 lines after that. You may have to change the parameter from 20 to whatever to get the entire statement.

Once tablestructure.sql had been edited to only contain the proper CREATE TABLE statement, you may now restore the table structure.

mysql -u user -p dbname < tablestructure.sql

STEP 2: Extract and restore the table data

This command will create a sql file for the table.

zcat backup.sql.gz | grep 'INSERT INTO `tablename1`' > tabledata.sql

If the resulting sql file is small enough, you may load the table straight back into the database.

mysql -u user -p dbname < tabledata.sql

OPTIONAL: If you have a huge table and want to partially restore or batch things, you may want to split the table data into multiple lines.

split -l 100 tabledata.sql

This will create multiple files of 100 lines each named xaa, xab, xac, etc. You may then load each file separately.

If some rows are already on the table and you just don't want the INSERT to fail on a duplicate primary key, you can use sed to add the IGNORE option to the INSERT statement. The command below simply does a search and replace on the source file and writes it to a new file.

sed 's/INSERT/INSERT IGNORE/' < tabledata.sql > newtabledata.sql

That should do the trick.

Comments

Popular posts from this blog

iReport Default Date Parameter

If you want to set a default date for a jasperreports parameter, you can enter the following in the default value expression of the parameter. new Date() However, if you wanted the default to be relative from today, for example, 3 days ago, you will need to do go outside of the standard java date libraries (Date, Calendar) because JR parameter does not allow you to have multiple statements. You can write your own utility package or you can use Joda , a replacement library for the JDK date and time library. To use Joda in iReports, you need to extract the jar file and add it to Options -> Classpath. You may also drop the jar file in iReports' lib directory. You will also need to add this library to your application or report server. For setting a default date 5 days prior from today, use this: new org.joda.time.DateTime().minusDays(5).toDate() or this if you want the time part set to the beginning of the day: new org.joda.time.LocalDate().toDateTimeAtStartOfDay().minusDays(5).to...

Get uptime alerts in Telegram

You will be configuring 3 services. The assumption is that you have accounts already for these services. UptimeRobot — source of the uptime/downtime notifications. It currently supports email, Twitter, Slack, Pushover, Pushbullet and others but not Telegram. We will use the webhook notification as the way to send messages to Telegram. IFTTT — bridges UptimeRobot and Telegram by providing the webhook for UptimeRobot and triggering a message to Telegram. In IFTTT terminology: IF {event trigger from UptimeRobot}, then {send message to Telegram group} Telegram — destination of the notification. You will need to create a group for this. Telegram Configuration In Telegram, create a new group, if needed. You may opt to use an existing group. Go to @IFTTT and follow the instruction so link your Telegram account to your IFTTT account. Send /connect_group to @IFTTT and connect the group where you want to send the notifications. IFTTT Configuration Sign in to IFTTT and add a ne...

Activating NDD on your Globe landline

NDD and IDD is not available by default on your new Globe landline to protect you from inadvertent charges due to calls to mobile phones and long distance numbers. Note that these instructions are for Metro Manila accounts. To activate NDD, you need call 730-1000. The problem is, an Interactive Voice Response (IVR) menu greets you at that number and there is no menu option to activate NDD. There is also no easy way to get to a human in the options. For credit card companies, I always use the "Report a lost card" option to reach an agent immediately. It appears there is no such shortcut with Globe's number. After several attempts at the menus, the way I got to an agent was to pretend to customize my plan and select the option that says the information is not helpful. Here's the guide: 730-1000 IVR Menu: 1 - English 1 - Globelines 1 - Customize your plan 1 - Customize your plan 2 - Not helpful 1- Talk to a customer agent It will take about 3 minutes for the ...