Monday, February 14, 2011

ZenCart - MySQL Database Backup & Restore (mirror really)

While there seems to be a tremendous amount of information on MYSQL and backing up databases as well as restoring them, I ran into some challenges and had to follow the steps below to mirror a database.

My goal was to simply have the ability to backup my database while going through some testing.  I also wanted to verify that once I had a working "development" environment that I could export/import into a new "production" environment.  While mysqldump should accomodate this request possibly in one simple command, I had issues and ended up using mysqldump to export and the "source" command to populate a new database.  

# mysqldump -u user_name -p database_name > database_bkup_file_name.sql

Note:  Adding the '-p' will cause mysqldump to prompt for the password for the user that you stated in "user_name".

You should theoretically then be able to use mysqldump to then populate a database as follows:

# mysqldump -u user_name -p database_name < database_bkup_file_name.sql

Based on some challenges and a desire to move forward and not necessarily become a MySQL admin, I simply created a new database, added the proper user & permissions followed by:

mysql> use new_database_name;

mysql> source name_of_database_backup_file.sql;
 
It should have been obvious that the new database was being populated but if you desire to verify, you can simply view the tables.

mysql> show tables;

While this proved handy, it does not allow you to update a database.  I will be making some incremental changes to a "dev" environment and will want to update the "prod" environment.  In order to accomplish this, I will need to drop the "prod" database and then recreate it and populate it with data.  I can see how learning to properly use the mysqlimport command may be the better answer.

No comments: