Skip to content

Instantly share code, notes, and snippets.

@aurora
Created December 21, 2010 22:02
Show Gist options
  • Save aurora/750698 to your computer and use it in GitHub Desktop.
Save aurora/750698 to your computer and use it in GitHub Desktop.
mysql backup/restore using CSV files
mysql backup/restore using CSV files
====================================
backup
------
#!/usr/bin/env bash
mysqldump -u root -p... database --no-data > /tmp/backup/database-schema.sql
mysqldump -u root -p... -t -T/tmp/backup database --fields-enclosed-by=\" --fields-terminated-by=,
restore
-------
#!/usr/bin/env bash
mysql -u root -p... database < /tmp/backup/database-schema.sql
if [ $? != 0 ]; then
echo "error importing scheme"
exit 1
fi
for i in `ls -1 /tmp/backup/*.txt`; do
table=`basename $i .txt`
echo $table
mysql -u root -p... -e "USE database;
SET SESSION character_set_database=utf8;
SET SESSION character_set_server=utf8;
SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO';
LOAD DATA LOCAL INFILE '$i' INTO TABLE $table
FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n';"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment