How to Backup Db / Rollback, Media, Code in Magento 2

One of the most important things when running an online store is having a proper backup of everything so that if something goes wrong, you can take back or restore it. So it’s important to back up at least the most important data in your store. Once you have made a backup of your files, you can restore or roll back it. It’s quite easy to make mistakes in this process because the system will delete specific files and databases before restoring them.

That’s why in this tutorial, we will talk about How to Backup / Rollback Db, Media, Code in Command Line Series. As you know, from Magento 2, they add many commands in bin/magento. This may difficult to get approach this, let me explain more detail about How to Backup / Rollback Db, Media, Code.

Let’s start!

How to Backup / Rollback Db, Media, Code command line

Go to Magento admin root folder and show usages:

php bin/magento setup:backup --help
Usage:                                                                                                                     
  setup:backup [options]                                                                                                   
                                                                                                                           
Options:                                                                                                                   
      --code                                     Take code and configuration files backup (excluding temporary files)      
      --media                                    Take media backup                                                         
      --db                                       Take complete database backup                                             
      --magento-init-params=MAGENTO-INIT-PARAMS  Add to any command to customize Magento initialization parameters         
                                                 For example: "MAGE_MODE=developer&MAGE_DIRS[base][path]=/var/www/example.c
m&MAGE_DIRS[cache][path]=/var/tmp/cache"                                                                                   
  -h, --help                                     Display this help message                                                 
  -q, --quiet                                    Do not output any message                                                 
  -V, --version                                  Display this application version                                          
      --ansi                                     Force ANSI output                                                         
      --no-ansi                                  Disable ANSI output                                                       
  -n, --no-interaction                           Do not ask any interactive question                                       
  -v|vv|vvv, --verbose                           Increase the verbosity of messages: 1 for normal output, 2 for more verbos output and 3 for debug                                                                                                    
                                                                                                                           
Help:                                                                                                                      
  Takes backup of Magento Application code base, media and database                                                        

List all existed backup files

php bin/magento info:backups:list

Output :

Showing backup files in /var/www/html/sam/ce2252/var/backups.
+---------------------------------+-------------+
| Backup Filename                 | Backup Type |
+---------------------------------+-------------+
| 1534245793_db.sql               | db          |
| 1534245893_filesystem_media.tgz | media       |
+---------------------------------+-------------+

There are 2 backup files: database and media.

Backup Database

This command line will backup current database and save to a static file. You can store on server, upload to cloud or download to local machines.

php bin/magento setup:backup --db

Output:

Enabling maintenance mode
DB backup is starting...
DB backup filename: 1534245793_db.sql
DB backup path: ./var/backups/1534245793_db.sql
[SUCCESS]: DB backup completed successfully.
Disabling maintenance mode

Let me exaplain more details. At the begining of the process, it will maintenance mode, and start backup then store to ./var/backups/ folder. After that, it will disable maintenance mode again.

You can setup run backup database daily with cron job. See this example:

crontab -e

It will show your server’s current cron jobs table. Let’s add a cron job line:

0 0 * * * php /path/to/magento/bin/magento setup:backup --db

Change /path/to/magento/ to your server path. This schedule task will be ran at 00:00 everyday.

Backup Media

It is same with backup database

php bin/magento setup:backup --media

Output:

Enabling maintenance mode
Media backup is starting...
Media backup filename: 1534245893_filesystem_media.tgz (The archive can be uncompressed with 7-Zip on Windows systems)
Media backup path: ./var/backups/1534245893_filesystem_media.tgz
[SUCCESS]: Media backup completed successfully.
Disabling maintenance mode

Backup Code

It is same with backup database, media

php bin/magento setup:backup --code

Output:

Enabling maintenance mode
Code backup is starting...
Code backup filename: 1534246155_filesystem_code.tgz (The archive can be uncompressed with 7-Zip on Windows systems)
Code backup path: ./var/backups/1534246155_filesystem_code.tgz
[SUCCESS]: Code backup completed successfully.
Disabling maintenance mode

How to rollback Database, Media, Code

Go to Magento admin root folder and show usages:

php bin/magento setup:rollback --help
Usage:                                                                                                                      
  setup:rollback [options]                                                                                                  
                                                                                                                            
Options:                                                                                                                    
  -c, --code-file=CODE-FILE                      Basename of the code backup file in var/backups                            
  -m, --media-file=MEDIA-FILE                    Basename of the media backup file in var/backups                           
  -d, --db-file=DB-FILE                          Basename of the db backup file in var/backups                              
      --magento-init-params=MAGENTO-INIT-PARAMS  Add to any command to customize Magento initialization parameters          
                                                 For example: "MAGE_MODE=developer&MAGE_DIRS[base][path]=/var/www/example.com&MAGE_DIRS[cache][path]=/var/tmp/cache"                                                                                    
  -h, --help                                     Display this help message                                                  
  -q, --quiet                                    Do not output any message                                                  
  -V, --version                                  Display this application version                                           
      --ansi                                     Force ANSI output                                                          
      --no-ansi                                  Disable ANSI output                                                        
  -n, --no-interaction                           Do not ask any interactive question                                        
  -v|vv|vvv, --verbose                           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug                                                                                                     
                                                                                                                            
Help:                                                                                                                       
  Rolls back Magento Application codebase, media and database                                                                                                                   

In this example, I will show you how to restore / rollback Magento database. Before you rollback, let check exist backup files:

List all existed backup files

php bin/magento info:backups:list

Output :

Showing backup files in ./var/backups.
+---------------------------------+-------------+
| Backup Filename                 | Backup Type |
+---------------------------------+-------------+
| 1534245793_db.sql               | db          |
| 1534245893_filesystem_media.tgz | media       |
+---------------------------------+-------------+

Let run rollback a database

php bin/magento setup:rollback --db-file=1534245793_db.sql

It asks to confirm, type: y to proces the rollback process.

Output:

Enabling maintenance mode                                                          
You are about to remove current code and/or database tables. Are you sure?[y/N]y   
DB rollback is starting...                                                         
DB rollback filename: 1534245793_db.sql                                            
DB rollback path: /var/www/html/sam/ce2252/var/backups/1534245793_db.sql           
[SUCCESS]: DB rollback completed successfully.                                     
Please set file permission of bin/magento to executable                            
Disabling maintenance mode                                                         

Note

All backup files have to placed in ./var/backups/ folder.

Conclusion

We hope that with this straightforward and easy-to-follow process, you will be able to back up your store easil. Remember to keep an up-to-date backup of your store to avoid bad issues from happening. If you have any problems while following this tutorial, feel free to let us know.

Thanks for reading!

Image Description
With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.
x