Skip to content

Instantly share code, notes, and snippets.

@rothkj1022
Last active September 25, 2020 22:13
Show Gist options
  • Save rothkj1022/9850fa70ecf8bb3d9db2be8f6bea12fc to your computer and use it in GitHub Desktop.
Save rothkj1022/9850fa70ecf8bb3d9db2be8f6bea12fc to your computer and use it in GitHub Desktop.
cPanel Local Backup + S3 Remote Backup Script
#!/bin/bash
LOGFILE="backup.sh.log"
USE_INCREMENTAL=true
timestamp() {
date "+%Y-%m-%d %H:%M:%S"
}
if [ "$USE_INCREMENTAL" = true ]; then
echo "Starting incremental backup." | tee -a "${LOGFILE}"
else
echo "Starting non-incremental backup." | tee -a "${LOGFILE}"
# Delete old account backups
rm -rf /backup/accounts
# Recreate account backups folder
mkdir /backup/accounts
fi
# Loop through cpanel users
echo "$(timestamp): Starting local backup." | tee -a "${LOGFILE}"
for CPUSER in `\ls -A1 -I system /var/cpanel/users/ | xargs -n 1 basename`; do
echo "Now processing ${CPUSER} ..."
tarfile="/backup/accounts/${CPUSER}.tar";
# Loop through cpanel users
echo "$(timestamp): Starting local backup." | tee -a "${LOGFILE}"
for CPUSER in `\ls -A1 -I system /var/cpanel/users/ | xargs -n 1 basename`; do
echo "Now processing ${CPUSER} ..."
tarfile="/backup/accounts/${CPUSER}.tar";
acctfolder="/backup/accounts/${CPUSER}";
# Delete the existing folder
if [ -d $acctfolder ]
then
echo "Account folder exists, deleting '${acctfolder}'."
rm -rf ${acctfolder}
fi
if [ "$USE_INCREMENTAL" = true ]; then
# Incremental (keep tar files)
if [ ! -f "$tarfile" ]
then
echo "File '${tarfile}' not found, creating."
/scripts/pkgacct ${CPUSER} /backup/accounts --nocompress --backup
else
echo "File '${tarfile}' is found, updating."
/scripts/pkgacct ${CPUSER} /backup/accounts --backup --incremental
fi
else
# Non-incremental (delete tar.gz files)
tarfile="${tarfile}.gz";
echo "Creating file '${tarfile}'."
/scripts/pkgacct ${CPUSER} /backup/accounts --backup
fi
# Extract the tarball
echo "Extracting file '${tarfile}'."
tar -C /backup/accounts -xvf ${tarfile}
# Delete the tarball if non-incremental
if [ ! "$USE_INCREMENTAL" = true ]; then
echo "Deleting file '${tarfile}'."
rm -f ${tarfile}
fi
done
echo "$(timestamp): Local backup complete." | tee -a "${LOGFILE}"
# Backup to S3
echo "$(timestamp): Starting remote backup." | tee -a "${LOGFILE}"
nice -n 15 aws s3 sync /backup s3://{yourbucketnamehere}/backup/ --exclude '*.log' --exclude '*.tar' --exclude='*/logs/*' --exclude='*/tmp/*' --exclude='*/cache/*' --exclude='.meta/*' --follow-symlinks --delete --quiet
echo "$(timestamp): Remote backup complete." | tee -a "${LOGFILE}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment