Skip to content

Instantly share code, notes, and snippets.

@magenx
Last active May 11, 2020 18:38
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save magenx/07503580876a372b2b54 to your computer and use it in GitHub Desktop.
backup
#!/bin/bash
### System Setup ###
DIRS="/var/www/shop/public_html"
BACKUP=/tmp/backup.$$
NOW=$(date +"%d-%m-%Y")
DELDATE=$(date -d "-7 days" +"%d-%m-%Y")
### MySQL Setup ###
MUSER="xxx"
MPASS="xxx"
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
### FTP server Setup ###
FTPD="/backup"
FTPU="xxxxxx"
FTPP="xxxxxx"
FTPS="xxxxxx"
### CALLBACK ###
EMAILID="admin@xxxxxx.com"
### Start Backup ###
[ ! -d ${BACKUP} ] && mkdir -p ${BACKUP} || :
### make a full backup ###
FILE="html-full-${NOW}.tar.gz"
tar -zcf ${BACKUP}/${FILE} ${DIRS} >/dev/null 2>&1
### Start MySQL Backup ###
# Get all databases name
DBS="$(${MYSQL} -u ${MUSER} -h ${MHOST} -p${MPASS} -Bse 'show databases' | grep -v performance_schema)"
for db in ${DBS}
do
FILE=${BACKUP}/mysql-${db}.${NOW}-$(date +"%T").gz
${MYSQLDUMP} -u ${MUSER} -h ${MHOST} -p${MPASS} --single-transaction --routines --triggers ${db} | ${GZIP} > ${FILE}
done
### DigitalOcean S3 backup ###
#/opt/s3cmd/s3cmd rm s3://${SPACENAME}/${DELDATE}/ --recursive --force
#/opt/s3cmd/s3cmd put ${BACKUP}/${NOW}/* s3://${SPACENAME}/${NOW}/
### Start FTP backup ###
ncftp -u ${FTPU} -p ${FTPP} ${FTPS}<<EOF
cd ${FTPD}/${DELDATE}
rm *.gz
cd ${FTPD}
rmdir ${DELDATE}
mkdir ${FTPD}
mkdir ${FTPD}/${NOW}
cd ${FTPD}/${NOW}
lcd ${BACKUP}
mput *
quit
EOF
### If ftp backup failed ###
if [ "$?" == "0" ]; then
find ${BACKUP}/${NOW}/ -type f -name "*.gz" -exec rm -rf {} \;
find ${BACKUP} -type d -empty -delete
else
CALLBACK=/tmp/backup.failed
echo "Date: $(date)">${CALLBACK}
echo "Hostname: $(hostname)" >>${CALLBACK}
echo "Backup failed" >>${CALLBACK}
mail -s "BACKUP FAILED" "${EMAILID}" <${CALLBACK}
fi
@MagePsycho
Copy link

I have also written a bash shell script to backup Magento 2 codebase + database here -
https://github.com/MagePsycho/magento2-db-code-backup-bash-script

You can simply use command as:

./mage2-backup.sh --type=all --skip-media=1 --src-dir=/path/to/magento2/root --dest-dir=/path/to/destination

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment