Magento 2 - Take backup
Taking a website backup is one of the most important things to do when you want to avoid unexpected accidents such as site crashes, new feature failures etc. However, most of us don't take it very seriously until the problem is serious.
Magento 2 provides in-built backup options to make it easy for the store owners/administrators to take regular backups of their e-commerce stores and protect their data and their stores from unexpected loss. You can learn more about website backups here.
Following are the 3 ways you can take a backup of your Magento 2 store:
- Using Command-Line Interface (CLI)
- Using the Magento 2 admin
- Manual backup of files and database on the server
Note: The internal Magento 2 backup options (CLI and admin) do not backup the directories pub/static and var. These are directories contain generated content which does not need backing up. However, if you do need to take a backup of these directories, use the Manual Backup option.
Backup using Command-Line Interface (CLI):
The following is the general command to backup your Magento 2 store:
php bin/magento setup:backup [--code] [--media] [--db]
This command puts the store in maintenance mode and takes a backup of the code, media and the database, depending on the flags set and takes the store out of maintenance mode.
--code |
Takes a backup of all the files excluding var and pub/static |
--media |
When set backs up the pub/media directory |
--db | When set takes a backup of the database |
Following is the command to take the backup of the code only.
php bin/magento setup:backup --code
To know more about command-line backup, go to: Back up and roll back the file system, media, and database
Backup using admin panel
To backup using the admin panel, go to System > Tools > Backups.
You can choose from the following 3 actions:
- System Backups: This option is the same as
--code
which takes a backup of the entire filesystem exlcudingvar
andpub/static directory.
- Database and Media Backup: This option takes a backup of the database and pub/media directory. The CLI equivalent of this action is
--db --media.
- Database Backup: Take a backup of the database using this option.
Note: The backups taken using internal Magento backup options are saved in var/backups
directory. These can be restored using the CLI command php bin/magento setup:rollback
or from the
Magento admin.
Manual Backup:
For manual backups you will need to take a backup of the filesystem and export a copy of your database.
Filesystem backup: To take a backup of your file system go to your Magento root directory (where your Magento files are located). Download the entire directory directly which will usually take longer or compress the directory(using the control panel or the command line interface) and then download it.
Database backup: To take a backup of your database you can use phpMyAdmin, CLI or any other software that allows you to connect to the database and export it (such as Sequel Pro). You can either choose to export the entire database or select/unselect the tables you want or don't want to export.