Backing Up Your Database With MAMP
One of the changes in Craft 3 is that, by default, it relies on the mysqldump
command to create database backups. That’s the most reliable way to dump a database without hitting PHP resource limits like could happen in Craft 2 with large databases. The problem for MAMP users, however, is that its mysql
and mysqldump
executables are not available to the PHP process when making requests in the Control Panel via HTTP(S). A few Google searches will surface some hacks that work for some people, and not for others.
Craft provides two configuration settings to help with this: backupCommand
and restoreCommand
. We’ll provide MAMP-compatible commands for you here. We recommend putting them in your .env
file so they don’t need to be shared in other environments such as production, staging, or your teammates’ computers.
Edit /.env
#
We’ll add two variables: BACKUP_COMMAND
and RESTORE_COMMAND
. Copy and paste this code block exactly as it is into your .env
file, each command on a single line, and double-quotes as they are.
BACKUP_COMMAND="/Applications/MAMP/Library/bin/mysqldump -h localhost -u root -proot --add-drop-table --comments --create-options --dump-date --no-autocommit --routines --set-charset --triggers --single-transaction --no-data --result-file=\"{file}\" {database} && /Applications/MAMP/Library/bin/mysqldump -h localhost -u root -proot --add-drop-table --comments --create-options --dump-date --no-autocommit --routines --set-charset --triggers --no-create-info --ignore-table={database}.assetindexdata --ignore-table={database}.assettransformindex --ignore-table={database}.cache --ignore-table={database}.sessions --ignore-table={database}.templatecaches --ignore-table={database}.templatecachecriteria --ignore-table={database}.templatecacheelements --ignore-table={database}.templatecachequeries {database} >> \"{file}\""
RESTORE_COMMAND="/Applications/MAMP/Library/bin/mysql -h localhost -u root -proot {database} < \"{file}\""
If you changed your MAMP preferences for your MySQL password, then find and replace -proot
with -pyourpassword
.
The missing space between -p
and root
is not a typo. MySQL wants it without a space. ¯\_(ツ)_/¯
Edit /config/general.php
#
Craft comes set up with a multi-environment config. Add this to the dev environment settings:
// Dev environment settings
'dev' => [
// existing settings ...
'backupCommand' => getenv('BACKUP_COMMAND'),
'restoreCommand' => getenv('RESTORE_COMMAND'),
],
What does it do? #
These commands tell PHP to look for MAMP’s own mysql
and mysqldump
executables by providing the full path to each; e.g., /Applications/MAMP/Library/bin/mysqldump
. The complex backup command exports essential data and skips non-essentials like template caches. That keeps the backup file lean and portable.
If you’re comfortable adding a module to your project, you can skip the copy/paste procedure above and check out this MAMP Helper Gist on GitHub.