sarpay
4/10/2013 - 10:42 PM

[mysql] [unix] back up, restore and import/export a mysql database

[mysql] [unix] back up, restore and import/export a mysql database

# Back up from the command line (using mysqldump)
mysqldump --opt -u [uname] -p[pass] [dbname] > [backupfile.sql]
 
# Back up the MySQL database
mysqldump -u [uname] -p[pass] [dbname] | gzip -9 > [backupfile.sql.gz]
 
# Restoring the MySQL database
#create a header.txt and footer.txt file to be wrapped around your dump file.
#include the following line in your header.txt file: SET FOREIGN_KEY_CHECKS = 0;
#include the following line in your footer.txt file: SET FOREIGN_KEY_CHECKS = 1;
copy header.txt + [backupfile.sql] + footer.txt restore_db.sql
mysql -u [uname] -p[pass] [db_to_restore] < [restore_db.sql]

# import
mysql -u username -p password -h localhost database_name < file.sql

# export
mysqldump -u username -p password -h localhost database_name > file.sql