Merchants know how much hard work and creativity it takes to build and run a successful ecommerce store. A technical failure or cyber-attack can quickly undermine the long hours spent on backend development and storefront design.
Regular backups enable merchants to maintain reliable long-term records and are an essential ecommerce security practice.
Find out how to backup and restore your Magento 2 store and prevent a devastating loss of data and reputation.
Magento 2 Backup Methods
The built-in backup system in Magento 2 has been deprecated as of versions 2.1.16, 2.2.7, and 2.3.0. Even though the backup feature is disabled by default, it is still possible to create backups using the Magento Admin or the command line.
Note: If you are still using Magento 1, find out how to plan and complete a migration from Magento 1 to Magento 2.
Future Magento 2 versions are likely to discontinue and eliminate the backup feature. Performing manual backups or using a third-party system or extension is going to become the primary method for backing up a Magento store.
Back Up Using Admin Panel
To backup system settings, store media, and databases in Magento 2:
1. Access the Magento Admin.
2. Open the Stores menu.
3. Select Configuration.
4. Open the Advanced drop-down and click Admin.
4. Expand the Advanced dropdown.
5. Click System.
6. Open Backup Settings.
7. Set the Enable Backup option to Yes.
8. (Optional) Use the Enable Scheduled Backup section to define the parameters for regular and automated backups.
9. Click Save Config to apply the settings.
10. Navigate to System.
11. Click Backups.
12. Select the type of backup to create. The available options include:
- System Backup - Create a backup of the file system and database, with the option to include the media directory.
- Database and Media Backup - Make a backup of the database and the media directory.
- Database Backup - Backup the entire database.
The Backups menu contains a searchable list of existing backups. The list is invaluable when updating Magento to a newer version.
13. Enter a Backup Name.
14. Check the box to put the store in Maintenance mode during the backup process. The store’s status is going to return to the Production mode automatically once the backup is complete.
15. Click Ok to start the backup.
After the process is complete, review the list on the Backups page to confirm if the backup file is available.
Back Up Via Command Line
To create a backup from the command line, access the store's root directory and enable the Magento backup feature:
php bin/magento config:set system/backup/functionality_enabled 1
The system confirms that the backup feature is active.
The built-in backup system allows users to create 3 types of backups.
Back Up Database
Access the Magento root directory and back up the Magento database:
php bin/magento setup:backup --db
The output contains the backup file’s full path and filename.
The Maintenance mode is enabled at the start of the process and reverts to the Production mode once the backup is complete.
Back Up Media Files
Back up the pub/media directory using the following command:
php bin/magento setup:backup --media
By default, media backup files are stored as compressed archives in the /var/backups/timestamp_filesystem_media.tgz directory.
Back Up File System
Use the following command to back up the Magento file system:
php bin/magento setup:backup --code
The command does not back up the content of the pub/static and var directories. File system backups are stored as compressed timestamp_filesystem.tgz files in the var/backups/ directory.
Back Up Manually
To manually back up files from the Magento server:
1. Access the server using cPanel or a preferred FTP Client.
2. Locate the public_html directory and download the website's files locally.
3. Select phpMyAdmin in the Databases menu.
4. Access the Magento database and click Export.
5. Choose the Quick Export method.
6. Select the format of the file, for example, SQL.
7. Click Go to export the SQL file.
Create several backup copies and securely store the files in multiple locations.
Magento 2 Restore Backup (Rollback)
A backup enables merchants to return their store to a previously saved state. The rollback process needs to be carried out efficiently, without excessive downtime and minimal data loss.
Restore via Admin Panel
To roll back the Magento store using the Admin panel:
1. Access the Magento Admin.
2. Open the System menu.
3. Select Backups.
4. Find the backup on the list and click the corresponding Rollback option.
5. A system warning explains that all data created after the backup is going to be erased. Click Ok to proceed.
6. Enter the Magento User Password.
7. Check the box to enable the Maintenance mode during the rollback.
8. Click Ok to roll back the Magento store.
The time the system takes to complete the process depends on the backup file's size.
Restore via Command Line
To restore a backup file using the command line:
1. Access the Magento root directory and retrieve a list of existing backups:
php bin/magento info:backups:list
The list displays the backup file name and type.
2. The command to roll back to a specific backup file depends on the backup type.
To restore file system backups:
php bin/magento setup:rollback -c filename
To restore media file backups:
php bin/magento setup:rollback -m filename
To restore database backups:
php bin/magento setup:rollback -d filename
For example, the following command rolls back the existing database backup:
php bin/magento setup:rollback -d 1622558078_db.sql
Enter y to proceed with the rollback and keep the backup file.
The command executes the rollback and automatically enables and disables the Maintenance mode during the process.
You can now backup and restore your Magento 2 store by choosing between three distinct methods.
Stores that do not back up their data regularly are much less likely to recover after an attack or a major technical issue. Try to schedule and automate backup tasks to ensure that your backups are always up to date.