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
- 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.
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 Magento file system owner.
If you use the bash shell, you can use the following syntax to switch to the Magento file system owner and enter the command at the same time:
su <Magento file system owner> -s /bin/bash -c <command>
If the Magento file system owner does not allow logins, you can do the following:
sudo -u <Magento file system owner> <command>
To run Magento commands from any directory, add
<magento_root>/binto your system
Because shells have differing syntax, consult a reference like unix.stackexchange.com.
Sample bash shell for CentOS:
Optionally, you can run the commands in the following ways:
cd <magento_root>/binand run them as
./magento <command name>
<magento_root>/bin/magento <command name>
<magento_root>is a subdirectory of your web server docroot. Need help locating the docroot?
In addition to the command arguments discussed here, see Common arguments.
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 ulimit for the web server user
Why to set ulimit
Rolling back to a previous backup can silently fail, resulting in incomplete data being written to the file system or database. (This includes all types of rollbacks, including using System Upgrade, Component Manager, or 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 ulimit
We recommend setting the
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 Magento file system owner.
1 ulimit -s 65536
You can change this to a larger value if needed.
The syntax for
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 Magento file system owner.
/home/<username>/.bashrcin a text editor.
Add the following line:
ulimit -s 65536
- Save your changes to
.bashrcand exit the text editor.
We recommend you avoid setting a value for
php.ini because it can result in incomplete rollbacks with no failure notice.
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
Backs up the Magento file system (excluding var and pub/static directories). var/backups/ \_filesystem.tgz
Back up the pub/media directory. var/backups/ \_filesystem_media.tgz
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
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
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