A guide on how to backup your Magento 2 store

Get fast, reliable hosting for WordPress
Power your site with the industry’s fastest, most optimized WordPress hosting

Getting your Magento 2 store backed up regularly is one of the most important aspects of ensuring that it stays protected from the consequences of unwanted events.
Failed updates, server hardware or software issues, various security threats — you need to be prepared for everything, knowing all your data can be easily recovered.
Below we are going to learn how to backup Magento 2 stores using three different methods. To learn more about Magento 2 backups, just keep reading.
The types of backups
Magento is an ecommerce platform that can provide unlimited opportunities to store owners, which makes having a backup strategy even more vital.
Magento 2 became a successor to the Magento 1 content management system when the latter reached its end of life, and essentially extended the functionality of the previous version.
All products added, customers’ orders, session data like carts, and a large amount of other information — all of this is stored within the Magento 2 database, and we can not allow any of that to be lost. That’s where a backup comes in.
Get fully managed Magento hosting
Accelerate your store’s potential, without the ongoing maintenance
There are three types of Magento 2 backups we can create:
- System backup. This type of backup saves everything, including the Magento 2 file system, the media folder, and the database.
- Database and media backup. As the name suggests, you can create a backup of your media folder and the Magento 2 database.
- Database backup. If you would like to save a backup of your database only, that is also possible.
We can backup Magento 2 websites from the admin panel, command line, making use of the Magento commands, or by creating a Magento 2 backup manually.
The process of creating a backup manually can be applied to any other content management system.
Via the command line
Magento 2 backups can be created from the command line using certain Magento commands.
Backups created are stored in the var/backups folder inside your store installation directory and can be used to restore the website at any time.
Let’s see how to backup Magento 2 from the command line.
Step 1: Log in to the server via SSH and go to your Magento 2 store installation directory
To create a backup, we need to be logged in via SSH with the account user. Once we are logged in, let’s move to the document root of our store where our Magento 2 installation resides.
As you can see in the screenshot below, we have logged in as the magentotest user, which is the owner of our Magento installation, and public_html is our current working directory.
The user we are logged in as [magentotest@newvps public_html]$ whoami magentotest Our current working directory [magentotest@newvps public_html]$ pwd /home2/magentotest/public_htmlStep 2: Create a backup
Using the magento setup:backup command, we can backup our Magento 2 store fast and easily. With the help of the flags we can apply to the commands, we can specify what type of a backup we would like to create.
Please note that the backup command will put the store into maintenance mode. When your store is put into maintenance mode, its front page will display a message saying that the website is currently unavailable.
Once a backup is complete, the store will be operational again.
Related reading: How to create a custom Magento 2 maintenance page without using an extension >>
In the example below, we have created a full system backup containing our Magento 2 file system, the media directory, and the database; you can see the corresponding flags –code, –media, –db we used.
magentotest@newvps public_html]$ magento setup:backup --code --media --db Enabling maintenance mode Code backup is starting... Code backup filename: 1648215256_filesystem_code.tgz (The archive can be uncompressed with 7-Zip on Windows systems) Code backup path: /home2/magentotest/public_html/var/backups/1648215256_filesystem_code.tgz [SUCCESS]: Code backup completed successfully. Media backup is starting... Media backup filename: 1648215256_filesystem_media.tgz (The archive can be uncompressed with 7-Zip on Windows systems) Media backup path: /home2/magentotest/public_html/var/backups/1648215256_filesystem_media.tgz [SUCCESS]: Media backup completed successfully. DB backup is starting... DB backup filename: 1648215256_db.sql DB backup path: /home2/magentotest/public_html/var/backups/1648215256_db.sql [SUCCESS]: DB backup completed successfully. Disabling maintenance modeStep 3: View the backups you created
We can find all the backups we created both from the command line and the admin panel in the var/backups directory.
Let’s take a look at the list below.
We have 2 backups that we created from the admin panel, and the full backup we just created, which consists of 3 files: the database backup, Magento 2 file system, and the media files archives.
The backups created from the admin panel -rw-rw-r-- 1 magentotest magentotest 136702556 Mar 24 16:14 1648209993_snapshot_March_25.tgz -rw-rw-r-- 1 magentotest magentotest 943961 Mar 24 16:16 1648210578_db_March_25.sql The full backup we created from the command line -rw-rw-r-- 1 magentotest magentotest 1113288 Mar 25 12:38 1648215256_db.sql -rw-rw-r-- 1 magentotest magentotest 133462787 Mar 25 12:38 1648215256_filesystem_code.tgz -rw-rw-r-- 1 magentotest magentotest 110237 Mar 25 12:38 1648215256_filesystem_media.tgz The backup directory [magentotest@newvps backups]$ pwd /home2/magentotest/public_html/var/backupsRestoring from a backup via command line
You can restore your website from any of the backups saved from your admin panel or using the magento setup:rollback command.
For example, below we have restored our Magento store from the full system backup we created earlier from the command line, but skipped the restoration of the database.
The flags that can be used are -m, -c, and -d for media, file system, and database restoration accordingly. All flags need to be followed by the backup file we are restoring from.
[magentotest@newvps backups]$ magento setup:rollback -c 1648215256_filesystem_code.tgz -m 1648215256_filesystem_media.tgz Enabling maintenance mode You are about to remove current code and/or database tables. Are you sure?[y/N]y Do you want to keep the backups?[y/N]y Code rollback is starting ... Code rollback filename: 1648215256_filesystem_code.tgz Code rollback file path: /home2/magentotest/public_html/var/backups/1648215256_filesystem_code.tgz [SUCCESS]: Code rollback completed successfully. Media rollback is starting ... Media rollback filename: 1648215256_filesystem_media.tgz Media rollback file path: /home2/magentotest/public_html/var/backups/1648215256_filesystem_media.tgz [SUCCESS]: Media rollback completed successfully. Disabling maintenance modeVia the admin panel
We can easily create a Magento 2 backup from the admin panel.
This method does not require connecting to the server directly via SSH as we did when we created a backup using the Magento command line interface.
Additional resources
What is managed WordPress hosting? →
Get details and decide if managed WordPress hosting is right for you.
Must-have web development plugins to create WordPress websites at scale →
Explore the best web development plugins to enhance functionality, improve performance, and streamline your WordPress site.
A complete guide to WordPress shortcodes →
Shortcodes make life easier. Learn how to get started!