Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Automation»Magento 2 Codebase & Database Backup Script

    Magento 2 Codebase & Database Backup Script

    By RahulMarch 11, 20203 Mins Read

    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.

    Advertisement

    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

    Related Posts

    Best Practices and Tips for Writing Shell Scripts as Pro

    Backup and Clone Disk Partitions of Different Sizes in Linux

    Backing Up Your Linux System with Rsync: A Step-by-Step Guide

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to List Manually Installed Packages in Ubuntu & Debian
    • 10 Bash Tricks Every Developer Should Know
    • How to Validate Email Address in JavaScript
    • Firewalld: Common Firewall Rules and Commands
    • 12 Apk Commands in Alpine Linux Package Management
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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