Close [x]

Back up and roll back the file system, media, and database

Edit this page on GitHub

Contents

See one of the following sections:

Overview of backup

This command enables you to back up:

  • The Magento file system (excluding var and pub/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

  1. 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>
    
  2. To run Magento commands from any directory, add <your Magento install dir>/bin to your system PATH.

    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.

Backing up

Command usage:

magento setup:backup [--code] [--media] [--db]

The command performs the following tasks:

  1. Puts the store in maintenance mode.
  2. 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/<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.gz

  3. 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.gz (The archive can be uncompressed with 7-Zip on Windows systems)
DB backup path: /var/www/html/magento2/var/backups/1434133011_db.gz
[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.