Skip to content

Instantly share code, notes, and snippets.

@thecancerus
Last active September 24, 2019 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thecancerus/7402a75a67eb8bf23248c9ddedc65de2 to your computer and use it in GitHub Desktop.
Save thecancerus/7402a75a67eb8bf23248c9ddedc65de2 to your computer and use it in GitHub Desktop.
Backup Script for WordPress to s3 using s3cmd
#!/usr/bin/env bash
# Back up Script to Amazon S3
# Source:
# Author: Amit
#path to WordPress installations
SITESTORE=/var/www
#S3 bucket
S3DIR="s3://bucket-name/"
MYSQL_USER=''
MYSQL_PASS=''
DATE=$(date +%Y%m%d)
LOGFILE=/var/log/s3backup/$DATE.log
EMAIL=''
mkdir -p /var/log/s3backup
touch $LOGFILE
#create array of sites based on folder names
SITELIST=($(ls -lh $SITESTORE | awk '{print $9}'))
#start the loop
for SITE in ${SITELIST[@]}; do
echo Backing up $SITE
#enter the WordPress folder
#cd $SITESTORE/$SITE
if [ $SITE == '22222' ]|| [ $SITE == 'html' ]; then
echo Skipping $SITE
continue
fi
DBNAME=${SITE//./_}
echo database is $DBNAME
if [ ! -e $SITESTORE/$SITE/db ]; then
mkdir $SITESTORE/$SITE/db
fi
#back up the WordPress database
mysqldump -u $MYSQL_USER -p$MYSQL_PASS --routines --single-transaction --quick --lock-tables=false $DBNAME | gzip -9 > $SITESTORE/$SITE/db/$DBNAME.sql.gz
done
#back up the WordPress folder
/usr/local/bin/s3cmd sync --exclude '22222/*' --exclude 'html' --exclude '.git/*' --exclude 'logs/*' $SITESTORE/ --delete-removed --preserve $S3DIR | tee -a $LOGFILE
#/usr/bin/mail -s "Backup report for KABIR DO Server ON $DATE" $EMAIL < $LOGFILE
@thecancerus
Copy link
Author

LOGFILE="/var/log/s3backup/$DATE-loantap-uploads.log"
touch $LOGFILE

#/usr/local/bin/s3cmd sync $SITESTORE/ --preserve $S3DIR >> $LOGFILE
/usr/local/bin/aws s3 sync $SITESTORE/ $S3DIR --endpoint-url http://s3-accelerate.amazonaws.com --profile default | tee -a $LOGFILE

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