Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Mar 20, 2018

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
You can’t perform that action at this time.