Backup your Mysql database


It is not always that in a project you can use library as yaml_db or seed_fu. Sometimes you just want to move a database from this server to another server. And ofcourse, use mysqldump will be faster.

First, you my want to connect to remote server via ssh and private key:

sudo ssh -i path_to_your_key remote_user_name@remote_server_address

Dump database:

## Dump all tables of database
mysqldump -h database_host -u database_user_name -p database_name > my_db_dump.sql

## Dump some tables of database
mysqldump -h database_host -u database_user_name -p database_name table_1 table_2 table_3 > db_part_1_dump.sql

## Dump database ignore some
mysqldump -h database_host -u database_user_name -p database_name --ignore-table=my_database.table1 my_database.table2 > db_part_1_dump.sql

Restore database:

mysql -u db_user_name databse_name -p -h host_name < path_to_sql_dump_file

If you need to copy file from your remote server to local computer, use this

sudo scp -i path_to_private_ssh_key remote_user@remote_host:path_to_dump_db_file_on_remote path_to_where_you_want_to_store_on_local

## Or reverse way
sudo scp -i path_to_private_ssh_key path_to_where_you_want_to_store_on_local remote_user@remote_host:path_to_dump_db_file_on_remote 

Published by

Colin Dao

I am a hardworking Rubyist in Hanoi, Vietnam

One thought on “Backup your Mysql database”

Leave a comment