Skip to content

Instantly share code, notes, and snippets.

@FreshLondon
Last active February 12, 2023 05:54
Show Gist options
  • Select an option

  • Save FreshLondon/72679f800fd0cb2324f4c2cb0e61248c to your computer and use it in GitHub Desktop.

Select an option

Save FreshLondon/72679f800fd0cb2324f4c2cb0e61248c to your computer and use it in GitHub Desktop.

Revisions

  1. FreshLondon revised this gist Feb 12, 2023. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions backups-s3.sh
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,8 @@
    # Modified for external Amazon S3 buckets
    # based on a backup script by ejsolutions/Teo/cynique
    #
    # set cron job with something like: 0 4 * * * /backups-s3.sh > /dev/null 2>&1
    #
    # Set the following 3 items to suit
    tmp_dir=/home/backups_tmp/
    s3_bucket=freshlondon-backups-singapore
  2. FreshLondon created this gist Feb 12, 2023.
    66 changes: 66 additions & 0 deletions backups-s3.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    #!/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=freshlondon-backups-singapore
    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={'.trash','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