Skip to content

Instantly share code, notes, and snippets.

@alicial
Created September 4, 2015 18:15
Show Gist options
  • Save alicial/809be397d45fb1829d91 to your computer and use it in GitHub Desktop.
Save alicial/809be397d45fb1829d91 to your computer and use it in GitHub Desktop.
Restoring accidentally deleted records on Mysql
Instructions for restoring a set of arbitrary records from a backup database, which preserves autoincrement IDs.
In the backup database, create a new table that only contains the records that should be restored.
CREATE new_mytable LIKE mytable;
INSERT INTO new_mytable SELECT * FROM mytable WHERE [condition of records that should not have been deleted];
Export the new_mytable from the backup, and import it in to the production DB
mysqldump -h <backuphost> -P <port> -u <username> -p mydatabase new_mytable > new_mytable.sql
mysql -u <username> -p -h <prodhost> -P <port> mydatabase < new_mytable.sql
Now in the production database, copy the records from the new table into the existing table.
INSERT INTO mytable SELECT * FROM new_mytable;
DROP new_mytable;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment