Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Automation»Magento 2 Codebase & Database Backup Script

    Magento 2 Codebase & Database Backup Script

    RahulBy RahulMarch 10, 20203 Mins ReadUpdated:March 11, 2020

    This tutorial will help you to automate the Magento2 codebase and database backup process using a shell script. The script will perform automatic backups on a scheduled interval. The script also has the ability to remove older backups as per configuration.

    Setup Magerun2

    You need to download and configure the Magerun2 script on your system.

    wget https://files.magerun.net/n98-magerun2.phar
    mv n98-magerun2.phar /usr/local/bin/n98-magerun2
    chmod +x /usr/local/bin/n98-magerun2 
    

    Download Shell Script

    You can download Magento2 backup script from here. Alternativly, use below command to download script using wget command.

    wget https://tecadmin.net/wp-content/downloads/scripts/magento2-backup.sh
    

    You can also copy the script below and paste it in a file on your machine.

    #!/bin/bash
    
    #######################################################################################
    ##
    ##   Magento 2 database and codebase backup script
    ##   Written By: Rahul Kumar
    ##   Written on: Mar 06, 2020
    ##   Last Update: Mar 11, 2020
    ##
    #######################################################################################
    
    ################## Modify below values  ###############################################
    
    
    MAGENTO_DOCUMENT_ROOT="/var/www/magento2"
    BACKUP_PATH="/var/www/magento2/var/backups"
    
    BACKUP_RETAIN_DAYS=30     # Number of days to keep a local backup copy
    
    GZIP="/bin/gzip"
    RM="/bin/rm"
    MKDIR="/bin/mkdir"
    N98_MAGERUN2="/usr/local/bin/n98-magerun2"
    
    
    
    #######################################################################################
    ##################              Do not change below values              ###############
    
    export PATH=/bin:/usr/bin:/usr/local/bin
    TODAY="$(date "+%Y-%m-%d-%H-%M")"
    CURRENT_BACKUP_DIR="${BACKUP_PATH}/${TODAY}"
    
    #######################################################################################
    ##################              Functions               ###############################
    
    exit_on_error(){
            echo -e "[email protected]"
            exit 99
    }
    
    maintenance_mode(){
            ${N98_MAGERUN2} sys:maintenance ${1} --skip-root-check --root-dir=${MAGENTO_DOCUMENT_ROOT}
    }
    
    check_cmds(){
        [ ! -x ${GZIP} ] && exit_on_error "FILENAME $GZIP does not exists. Make sure correct path is set in config section."
        [ ! -x ${RM} ] && exit_on_error "FILENAME $RM does not exists. Make sure correct path is set in config section."
        [ ! -x ${MKDIR} ] && exit_on_error "FILENAME $MKDIR does not exists. Make sure correct path is set config section."
        [ ! -x ${N98_MAGERUN2} ] && exit_on_error "FILENAME $N98_MAGERUN2 does not exists. \nDownload script from https://files.magerun.net/ and Make sure correct path is set in config section."
    }
    
    create_backup_dir(){
            [ ! -d ${CURRENT_BACKUP_DIR} ] && ${MKDIR} -p ${CURRENT_BACKUP_DIR}
    }
    
    database_backup(){
    
            ${N98_MAGERUN2} --skip-root-check --root-dir=${MAGENTO_DOCUMENT_ROOT} db:dump ${CURRENT_BACKUP_DIR}/database-${TODAY}.sql
    
            if [ $? -eq 0 ]; then
                    echo "Database backup successfully completed"
            else
                    maintenance_mode --off    ##### Disable mainenence even database backup failed
                    exit_on_error "Database backup failed. "
            fi
    }
    
    
    
    codebase_backup(){
    
            cd $MAGENTO_DOCUMENT_ROOT && \
            tar -cpzf ${CURRENT_BACKUP_DIR}/codebase-${TODAY}.tar.gz --exclude=var/* .
    
            if [ $? -eq 0 ]; then
                    echo "Codebase backup successfully completed"
            else
                    maintenance_mode --off    ##### Disable mainenence even codebase backup failed
                    exit_on_error "Codebase backup failed. "
            fi
    }
    
    
    cleanup_old_backup(){
    
            REMOVE_DIR_NAME=`date "+%Y-%m-%d-%H-%M" --date="${BACKUP_RETAIN_DAYS} days ago"`
    
            if [ ! -z ${BACKUP_PATH} ]; then
                      cd ${BACKUP_PATH}
                      if [ ! -z ${REMOVE_DIR_NAME} ] && [ -d ${REMOVE_DIR_NAME} ]; then
                                    rm -rf ${REMOVE_DIR_NAME}
                      fi
            fi
    }
    
    ########################################################################################
    ##################              Main (Calling functions)           #####################
    
    check_cmds
    create_backup_dir
    maintenance_mode --on
    database_backup
    codebase_backup
    maintenance_mode --off
    cleanup_old_backup
    
    
    ##########################################################################################
    ##################                      Script Ends Here                ##################
    ##########################################################################################
    

    Schedule Backup Scrpt

    Schedule this script using crontab on your system to run on a daily basis. Use below command to edit crontab configuration:

    crontab -e
    

    And add below entry at the end of file.

    0 0 * * * sh magento2-backup.sh
    

    Save file and close. You have successfully scheduled cronjob to run on 12:00 AM daily basis. To learn more about using the cronjob read this tutorial.

    backup Magento magento2 shell script
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Clear Log Files of A Docker Container
    Next Article How to Install Angular on Fedora 35/34/33

    Related Posts

    Setup Selenium with Python and Chrome on Ubuntu & Debian

    Updated:June 20, 20224 Mins Read

    Setup Selenium with Python and Chrome on Fedora

    Updated:June 18, 20223 Mins Read

    Backup MySQL Databases to Amazon S3 (Shell Script)

    Updated:May 28, 20222 Mins Read

    How to Backup Website to Amazon S3 using Shell Script

    Updated:March 24, 20222 Mins Read

    How to Backup SQL Server Database

    Updated:June 28, 20212 Mins Read

    Shell Script to Add Two Numbers

    Updated:May 28, 20222 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    • What is the /etc/nsswitch.conf file in Linux
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.