DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Mysql Importing Without Dropping Tables

10.10.2008
| 3791 views |
  • submit to reddit
        // Had to import a lot of .sql backups into one server.  Unfortunately they included a lot of DROP TABLE statements which constantly hosed my data. Here I have skewered sed to delete those sql statements before importing.

# Create a temporary filename
uniq="/tmp/temp_"`date "+%s"`

for item in `ssh user@dbserver ls`;
do
     echo -n "Importing $item..."
     scp user@dbserver\:$item $uniq;
     gunzip < $uniq | sed {/$'DROP TABLE IF EXISTS'/d} | mysql -f -u root $DB_SCHEMA_NAME
     echo "..done"
done

# clean up
rm -f "$uniq"