Skip to content

Instantly share code, notes, and snippets.

@FreshLondon
Last active September 8, 2021 06:45
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 FreshLondon/0e2bd776ba69ab542b1afef0ecdd0db9 to your computer and use it in GitHub Desktop.
Save FreshLondon/0e2bd776ba69ab542b1afef0ecdd0db9 to your computer and use it in GitHub Desktop.
Custom backup script for CWP > Amazon S3 bucket
#!/usr/bin/bash
# CWP - Custom backup script
# Modified for external Amazon S3 buckets
# based on a backup script by ejsolutions/Teo/cynique
#
# Set the following 3 items to suit
tmp_dir=/home/backups_tmp/
s3_bucket=YOUR-BUCKET-NAME-HERE
retention=1
# -------------------
if [ -d ${tmp_dir} ]; then
mkdir -p ${tmp_dir}
fi
date=$(date -d "today" +"%Y-%m-%d-%H:%M")
echo "The date now is " $date
# -------------------
mysql root_cwp -B -N -s -e "SELECT username,domain FROM user WHERE backup='on'" | while read -r username domain
do
# backup home directory
echo Custom backup task starting for $username at $domain
mkdir -p ${tmp_dir}${username}/home_dir
echo ">>Copying home directory"
if [ -f /home/${username}/backup_exclude.conf ]; then
ionice -c 3 nice rsync -a --exclude={'tmp','cache','cwp_stats','ai1wm-backups','wpvividbackups','backup','backups','backupcwp'} /home/${username}/ ${tmp_dir}${username}/home_dir
else
ionice -c 3 nice rsync -a /home/${username}/ ${tmp_dir}${username}/home_dir
fi
# backup databases
echo ">>Backing up databases"
mkdir -p ${tmp_dir}${username}/mysql/
mysql --defaults-extra-file=/root/.my.cnf -e "show databases LIKE '${username}%';" | grep -v Database | while read databasename
do
echo ">>>>Dumping" $databasename
nice -n 5 mysqldump --defaults-extra-file=/root/.my.cnf "$databasename" > ${tmp_dir}${username}/mysql/"$databasename.sql" \
2> ${tmp_dir}${username}/mysql/errors.txt && sync && \
nice gzip ${tmp_dir}${username}/mysql/"$databasename.sql"
done
# backup emails
if [ -d /var/vmail/${domain} ]; then
mkdir -p ${tmp_dir}${username}/vmail/
echo ">>Copying email"
ionice -c 3 nice cp -fR /var/vmail/${domain} ${tmp_dir}${username}/vmail/
fi
# compress backups
for i in home_dir mysql vmail
do
echo ">>Compressing" $i
ionice -c 3 nice -n 15 tar -cjf ${tmp_dir}${username}/$i.tar.bz2 ${tmp_dir}${username}/$i 2>/dev/null
echo ">>Removing unzipped directory for" $i
rm -Rf ${tmp_dir}${username}/$i
done
# send backup to s3
echo ">>Sending backup to s3 bucket for" $username
aws s3 sync ${tmp_dir}/${username} s3://${s3_bucket}/${date}/${username}
# Remove backups older than 1 days
echo ">>Removing temporary backup files for " $username
rm -Rf ${tmp_dir}${username}
# -------------------
done
echo Custom Backup Job Finished
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment