Over a million developers have joined DZone.

Mysql Importing Without Dropping Tables

// 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`;
     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"

# clean up
rm -f "$uniq"

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}