Last active
May 11, 2020 18:38
-
-
Save magenx/07503580876a372b2b54 to your computer and use it in GitHub Desktop.
backup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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="[email protected]" | |
### 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: