Every production environment needs a proper backup plan. By default, Magento 2 has a complete set of options to create and roll back backups.
When creating a backup, we can use one of the following options:
Option |
Meaning |
Backup file name |
---|---|---|
|
This creates a backup from the filesystem (excluding |
|
|
This creates a backup from the |
|
|
This creates a backup from the current database |
|
When creating a backup, Magento will store it in the var/backups
directory. In this recipe, we will refer to the bin/magento setup:backup
and bin/magento setup:rollback
options.
For the purpose of this recipe, let's assume that we need to manage the Magento 2 backup setup. The following steps will guide you through this:
php bin/magento setup:backup --code
Once Magento sets the maintenance code flag, the web shop is offline for everybody. Creating a code backup takes some time. The backup will be stored in var/backups
. A clean Magento 2 code backup is around 123 MB.
php bin/magento setup:backup --db
A clean Magento 2 database backup is around 14 MB.
php bin/magento setup:backup --media
A clean Magento 2 media backup with sample data is around 153 MB.
php bin/magento info:backup:list
This will give us an overview of what's available:
Showing backup files in /var/www/html/var/backups. +---------------------------------+-------------+ | Backup Filename | Backup Type | +---------------------------------+-------------+ | 1448480907_filesystem_code.tgz | code | | 1448481232_db.gz | db | | 1448481295_filesystem_media.tgz | media | +---------------------------------+-------------+
php bin/magento setup:rollback [-c|--code-file="<name>"] [-m|--media-file="<name>"] [-d|--db-file="<name>"]
For example, to restore a database backup, we can use the following command:
php bin/magento setup:rollback -d 1448481232_db.gz
You will get the following notification to confirm your action:
You are about to remove current code and/or database tables. Are you sure?[y/N]
crontab -e */1 * * * * php /var/www/html/bin/magento cron:run
This command will create a cron schedule in the Magento 2 database.
Let's recap and find out what we did throughout this recipe. In steps 1 through 8, you learned how to create rollbacks and manage backups via the command line.
In step 1, we created a backup for our code base only. In step 2, we created a backup for the database and in step 3, for all the media files.
In step 4, we listed all the created backups that are located in var/backups
.
The process in step 5 is related to the rollback scenario. Depending on the backup size, rolling back could take some time.
In steps 7 and 8, we configured a cron job to schedule our daily backup process automatically.
18.225.57.126