MYSQL back up in the CLI

Backing up MySQL database in the command line is relatively easy:

mysqldump --password=mypassword --user=myuser database [table] > dump.sql

In the above, –password and –user are only used if necessary. Naturally, you’ll replace mypassword and myuser with the appropriate values. If you don’t want to enter the password in the command (so it’s not saved in history) just remove the =mypassword, and it’ll prompt you for the password.

database is the name of the database you want to dump. If you don’t enter a table name, it’ll dump the entire database.

To restore the database is easy as:

mysql --user=myuser --password=mypassword [database] < dump.sql

The database name is used if you are restoring an individual table.