Back up and roll back the file system, media, and database
Overview of backup
This command enables you to back up:
- The Magento file system (excluding
var
andpub/static
directories) - The
pub/media
directory - The Magento 2 database
Backups are stored in the var/backups
directory and can be restored at any time using the magento setup:rollback command.
After backing up, you can roll back at a later time.
For Adobe Commerce on cloud infrastructure projects, see Snapshots and backup management in the Cloud guide.
First steps
-
Log in to the Magento server as, or switch to, a user with permissions to write to the Magento file system. See switch to the file system owner.
If you use the bash shell, you can use the following syntax to switch to the file system owner and enter the command at the same time:
1
su <file system owner> -s /bin/bash -c <command>
If the file system owner does not allow logins, you can do the following:
1
sudo -u <file system owner> <command>
-
To run Magento commands from any directory, add
<magento_root>/bin
to your systemPATH
.Because shells have differing syntax, consult a reference like unix.stackexchange.com.
Sample bash shell for CentOS:
1
export PATH=$PATH:/var/www/html/magento2/bin
Optionally, you can run the commands in the following ways:
cd <magento_root>/bin
and run them as./magento <command name>
<magento_root>/bin/magento <command name>
<magento_root>
is a subdirectory of your web server docroot.
In addition to the command arguments discussed here, see Common arguments.
Enable backups
The Magento backup feature is disabled by default. To enable, enter the following CLI command:
1
bin/magento config:set system/backup/functionality_enabled 1
Deprecation Notice: Magento backup functionality is deprecated as of 2.1.16, 2.2.7, and 2.3.0. We recommend investigating additional backup technologies and binary backup tools (such as Percona XtraBackup).
Set the open files limit
Why open files ulimit should be set
Rolling back to a previous backup can silently fail, resulting in incomplete data being written to the file system or database using the magento setup:rollback
command.
In some cases, a very long query string causes the user’s allocated memory space to run out of memory because of too many recursive calls.
How to set open files ulimit
We recommend setting the open files ulimit
for the Magento file system user to a value of 65536 or more.
You can do this either on the command line or you can make it a permanent setting for the user by editing their shell script.
Before you continue, if you haven’t done so already, switch to the file system owner.
Command:
1
ulimit -s 65536
You can change this to a larger value if needed.
The syntax for open files ulimit
depends on the UNIX shell you use. The preceding setting should work with CentOS and Ubuntu with the Bash shell. However, for Mac OS, the correct setting is ulimit -S 65532
. Consult a man page or operating system reference for more information.
To optionally set the value in the user’s Bash shell:
- If you haven’t done so already, switch to the file system owner.
- Open
/home/<username>/.bashrc
in a text editor. -
Add the following line:
1
ulimit -s 65536
- Save your changes to
.bashrc
and exit the text editor.
We recommend you avoid setting a value for pcre.recursion_limit
in php.ini
because it can result in incomplete rollbacks with no failure notice.
Backing up
Command usage:
1
bin/magento setup:backup [--code] [--media] [--db]
The command performs the following tasks:
- Puts the store in maintenance mode.
-
Executes one of the following command options.
Option Meaning Backup file name and location --code
Backs up the Magento file system (excluding var and pub/static directories). var/backups/ \_filesystem.tgz --media
Back up the pub/media directory. var/backups/ \_filesystem_media.tgz --db
Back up the Magento 2 database. var/backups/ \_db.sql - Takes the store out of maintenance mode.
For example, to back up the file system and database,
1
bin/magento setup:backup --code --db
Messages similar to the following display:
1
2
3
4
5
6
7
8
9
10
Enabling maintenance mode
Code backup is starting...
Code backup filename: 1434133011_filesystem.tgz (The archive can be uncompressed with 7-Zip on Windows systems)
Code backup path: /var/www/html/magento2/var/backups/1434133011_filesystem.tgz
[SUCCESS]: Code backup completed successfully.
DB backup is starting...
DB backup filename: 1434133011_db.sql
DB backup path: /var/www/html/magento2/var/backups/1434133011_db.sql
[SUCCESS]: DB backup completed successfully.
Disabling maintenance mode
Roll back
This section discusses how to roll back to a backup you made previously. You must know the file name of the backup file to restore.
To find the name of your backups, enter:
1
bin/magento info:backups:list
The first string in the backup file name is the timestamp.
To roll back to a previous backup, enter:
1
bin/magento setup:rollback [-c|--code-file="<name>"] [-m|--media-file="<name>"] [-d|--db-file="<name>"]
For example, to restore a media backup named 1440611839_filesystem_media.tgz
, enter
1
bin/magento setup:rollback -m 1440611839_filesystem_media.tgz
Messages similar to the following display:
1
2
3
[SUCCESS]: Media rollback completed successfully.
Please set file permission of bin/magento to executable
Disabling maintenance mode