Saturday, December 24, 2011

Backup all MySQL databases in one step

Here's a nifty line of code that you can run to save a backup of all databases.
First you have to cd to the directory you want the .sql.gz files saved.

Begin code:

for I in $(mysql -u user_name -p -e 'show databases' -s --skip-column-names); do mysqldump -u user_name -p $I | gzip > "$I.sql.gz"; done

End code

At the prompt enter your MySQL password. Twice.

You are served. Have a nice day.