Managing Magento 2 backup via the command line

Every production environment needs a proper backup plan. By default, Magento 2 has a complete set of options to create and roll back backups.

When creating a backup, we can use one of the following options:

Option

Meaning

Backup file name

--code

This creates a backup from the filesystem (excluding /var and /pub/static

var/backups/<timestamp>_filesystem.tgz

--media

This creates a backup from the /media directory

var/backups/<timestamp>_filesystem_media.tgz

-db

This creates a backup from the current database

var/backups/<timestamp>_db.gz

Tip

Besides the Magento backup options, it is always advisable to use an alternative backup solution connected to a backup storage.

Getting ready

When creating a backup, Magento will store it in the var/backups directory. In this recipe, we will refer to the bin/magento setup:backup and bin/magento setup:rollback options.

How to do it...

For the purpose of this recipe, let's assume that we need to manage the Magento 2 backup setup. The following steps will guide you through this:

  1. Let's start creating a code-only backup using the following command:
    php bin/magento setup:backup --code
    

    Once Magento sets the maintenance code flag, the web shop is offline for everybody. Creating a code backup takes some time. The backup will be stored in var/backups. A clean Magento 2 code backup is around 123 MB.

  2. Now we will create a database-only backup using the following command:
    php bin/magento setup:backup --db
    

    A clean Magento 2 database backup is around 14 MB.

  3. For a media backup, we use the following command:
    php bin/magento setup:backup --media
    

    A clean Magento 2 media backup with sample data is around 153 MB.

  4. Now let's check what backups have been created, using the following command:
    php bin/magento info:backup:list
    

    This will give us an overview of what's available:

    Showing backup files in /var/www/html/var/backups.
    +---------------------------------+-------------+
    | Backup Filename         | Backup Type |
    +---------------------------------+-------------+
    | 1448480907_filesystem_code.tgz  | code    |
    | 1448481232_db.gz        | db      |
    | 1448481295_filesystem_media.tgz | media     |
    +---------------------------------+-------------+
    
  5. Rolling back a backup is very easy. You can use the following command on the shell:
    php bin/magento setup:rollback [-c|--code-file="<name>"] [-m|--media-file="<name>"] [-d|--db-file="<name>"]
    

    For example, to restore a database backup, we can use the following command:

    php bin/magento setup:rollback -d 1448481232_db.gz
    

    You will get the following notification to confirm your action:

    You are about to remove current code and/or database tables. Are you sure?[y/N]
    

    Tip

    While confirming the action, you could get a Segmentation fault. You can fix this using the following command. This could be related to PHP 7. Always use the latest version:

    ulimit -s 65536
    

    You can also store this in the .bashrc file on your system.

  6. Congratulations, you just rolled back a backup. Pick any type to do the same. Always flush and clean your cache once you are done.
  7. Setting up a scheduled backup schema is a whole different ball game. We first need to set up a cron job using the following command:
    crontab -e
    */1 * * * * php /var/www/html/bin/magento cron:run
    

    This command will create a cron schedule in the Magento 2 database.

  8. Creating a daily, weekly, or monthly backup can be done in the administrator backend. Log in and navigate to Stores | Configuration | Advanced | System | Scheduled Backup Settings:
    How to do it...

How it works…

Let's recap and find out what we did throughout this recipe. In steps 1 through 8, you learned how to create rollbacks and manage backups via the command line.

In step 1, we created a backup for our code base only. In step 2, we created a backup for the database and in step 3, for all the media files.

In step 4, we listed all the created backups that are located in var/backups.

The process in step 5 is related to the rollback scenario. Depending on the backup size, rolling back could take some time.

In steps 7 and 8, we configured a cron job to schedule our daily backup process automatically.

There's more…

You can also check the current status of the cron schedule using MySQL. Run the following command from the shell:

mysql -u <username> --database <dbname> -p -e "select * from cron_schedule"
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.78.136