Magento 2 Rollback Database, Media, Code

It’s possible to rolllback code, media, and database in Magento 2. In today’s post, I will demonstrate how to roll back all these important elements in your store.

Let’s start!

Main contents

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.

Rollback Media, Code

It is the same with rollback / recover database. Run the following command line:

Rollback media

php bin/magento setup:rollback --media-file=your-filesystem_media.tgz   

Rollback Code

php bin/magento setup:rollback --code-file=your-filesystem_code.tgz   

Wrap up

It’s essential to back up everything in your store. In case something goes wrong, you can restore it anytime. But, taking a backup isn’t sufficient. Your backup needs to be saved carefully. This is when you need to know how to rollback your backups.

This post has just guided you on how to rollback important filesystem, media, or database. I hope this is a helpful tutorial for you. If you have any question about this topic, feel free to leave a comment in the comment section.

Thank you for reading!

Enjoyed the tutorial? Spread it to your friends!

magento-2-module-development
command
line
cli
console
indexer
reindex

Sam Thomas
Sam Thomas

CEO and Founder of Mageplaza. Pursueing a simple and healthy lifestyle. A friend, a husband and a dad of two children, a trainer and an influencer wannabe. He is a big fan of sports and travel, also.