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.
First steps
-
Log in to the Magento server as, or switch to, a user who has permissions to write to the Magento file system. One way to do this is to switch to the Magento file system owner.
If you use the bash shell, you can also 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
<your Magento install dir>/bin
to your systemPATH
.Because shells have differing syntax, consult a reference like unix.stackexchange.com.
bash shell example for CentOS:
export PATH=$PATH:/var/www/html/magento2/bin
You can also run the commands in the following ways:
cd <your Magento install dir>/bin
and run them as./magento <command name>
php <your Magento install dir>/bin/magento <command name>
<your Magento install dir>
is a subdirectory of your web server's docroot. Need help locating the docroot? Click here.
In addition to the command arguments discussed here, see Common arguments.
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.
Command:
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.
- Open
/home/<username>/.bashrc
in a text editor. -
Add the following line:
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:
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
andpub/static
directories).var/backups/<timestamp>_filesystem.tgz
--media
Back up the
pub/media
directory.var/backups/<timestamp>_filesystem_media.tgz
--db
Back up the Magento 2 database.
var/backups/<timestamp>_db.sql
</tbody> </table> - Takes the store out of maintenance mode.
For example, to back up the file system and database,
magento setup:backup --code --db
Messages similar to the following display:
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:
magento info:backups:list
The first string in the backup file name is the timestamp.
To roll back to a previous backup, enter:
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
magento setup:rollback -m 1440611839_filesystem_media.tgz
Messages similar to the following display:
[SUCCESS]: Media rollback completed successfully.
Please set file permission of bin/magento to executable
Disabling maintenance mode
If the command results in a Segmentation fault
message, see Segmentation fault during rollback.
Related topics
- Installing the Magento software using the command line
- Remove sample data modules or update sample data
- Display or change the Admin URI
- Enable or disable modules
- Uninstall modules
- Create or update the deployment configuration
- Enable or disable maintenance mode
- Create the Magento database schema
- Update the Magento database schema and data
- Configure the store
- Create a Magento administrator
- Uninstall themes
- Uninstall language packages
- Uninstall the Magento software
- Update the Magento software
- Reinstall the Magento software