Skip to content

Instantly share code, notes, and snippets.

@akirattii
Last active March 19, 2019 04:23
Show Gist options
  • Save akirattii/87c2977fbd3cc9a455cde15175d118fe to your computer and use it in GitHub Desktop.
Save akirattii/87c2977fbd3cc9a455cde15175d118fe to your computer and use it in GitHub Desktop.
How to Backup and Restore the MySQL database or its tables.
# BACKUP
## Backup all tables of a specific database:
mysqldump -h {HOST} -P{PORT} -u{USER} -p{DB} > {DB}.dump.sql
## Backup a specific table:
mysqldump -h {HOST} -P{PORT} -u{USER} -p{DB} -t -c --skip-extended-insert {TABLE} > {DB}.{TABLE}.dump.sql
# RESTORE
## Restore all tables of a specific database from backuped file:
mysql -h {HOST} -P{PORT} -u{USER} -p{DB} < {DB}.dump.sql
## Restore a specific table from backuped file:
mysql -h {HOST} -P{PORT} -u{USER} -p{DB} < {DB}.{TABLE}.dump.sql
# Bonus: Get size of tables
SELECT
table_schema as `Database`,
table_name AS `Table`,
round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB`
FROM
information_schema.TABLES
-- WHERE table_schema = 'test' -- filtrated by database name if you want
ORDER BY
(data_length + index_length) DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment