How to backup and restore with Magento 2


Posted by Andrew McCombe on Tue Mar 1, 2016

Magento 2 comes with built-in functionality to backup and restore the code, media, and database. You can choose to backup and restore code, media, or database separately. Here’s how to backup and restore with Magento 2.

Backup

Change directory to your Magento 2 install and run the following command:

php bin/magento setup:backup --code --media --db

This will enable maintenance mode and perform the backup. Note that the live site will not be available when this is happening.

Output from a backup is similar to the below:

amc@Andrews-MacBook-Pro ~/Sites/m2/public_html$ php bin/magento setup:backup --code --media --db
Enabling maintenance mode
Code backup is starting...
Code backup filename: 1452245140_filesystem_code.tgz (The archive can be uncompressed with 7-Zip on Windows systems)
Code backup path: /Users/amc/Sites/m2/public_html/var/backups/1452245140_filesystem_code.tgz
[SUCCESS]: Code backup completed successfully.
Media backup is starting...
Media backup filename: 1452245140_filesystem_media.tgz (The archive can be uncompressed with 7-Zip on Windows systems)
Media backup path: /Users/amc/Sites/m2/public_html/var/backups/1452245140_filesystem_media.tgz
[SUCCESS]: Media backup completed successfully.
DB backup is starting...
DB backup filename: 1452245140_db.gz (The archive can be uncompressed with 7-Zip on Windows systems)
DB backup path: /Users/amc/Sites/m2/public_html/var/backups/1452245140_db.gz
[SUCCESS]: DB backup completed successfully.
Disabling maintenance mode

Restore

Restoring backups is as easy as creating them. First list the available backups:

php bin/magento info:backups:list

This will list the three backups:

Showing backup files in /Users/amc/Sites/m2/public_html/var/backups. 
+---------------------------------+-------------+
| Backup Filename                 | Backup Type | 
+---------------------------------+-------------+
| 1452245140_filesystem_code.tgz  | code        |
| 1452097621_filesystem_code.tgz  | code        |
| 1452245140_db.gz                | db          |
| 1452097621_db.gz                | db          |
| 1452245140_filesystem_media.tgz | media       |
| 1452097621_filesystem_media.tgz | media       |
+---------------------------------+-------------+

You can then restore these using the following syntax:

php bin/magento setup:rollback -c 1452245140_filesystem_code.tgz -d 1452245140_db.gz -m 1452245140_filesystem_media.tgz

Again, this will enable maintenance mode and perform the restore. Note that the live site will not be available when this is happening.