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 "$@" 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.