Automatic Schedule Backup of single cPanel account

An easy script for automatic schedule backup of single cPanel account

It does the following:

  1. Cleanup the backup folder for any old files
  2. Adds the current date and a random key (addition of this key is to avoid guessing of url, just an added measure)
  3. Take SQL backup using mysqldump
  4. Take the complete public_html and recentely taken mysql backup to a gz file
  5. rsync that file to a remote file server and at the same time cleans up the remote server folder location if there are old files from previous months
  6. Email the download link

Here’s the script

#Cleanup old backup files
rm -f /home/ACCOUNT_NAME/BACKUP_FOLDER/*

#Insert date and a random key (key added to use it on public url to avoid guessing of public download link)
date=$(date +%Y-%m-%d)
key=$(head /dev/urandom | md5sum | cut -c1-10)

#MySQLdump of the account to the backup folder
mysqldump -uDB_USER -pDB_PASSWORD --single-transaction DB_NAME 2>&1 | gzip > /home/ACCOUNT_NAME/BACKUP_FOLDER/backup-${date}-${key}_SITE_NAME.sql.gz

#Compress the web directory and sql backup to a single backup file
tar czf /home/ACCOUNT_NAME/BACKUP_FOLDER/backup-${date}-${key}_SITE_NAME.tgz /home/ACCOUNT_NAME/public_html /home/ACCOUNT_NAME/BACKUP_FOLDER/backup-${date}-${key}_SITE_NAME.sql.gz 2> /dev/null

#Rsync the final backup file to remote server, also cleanup existing old backup prior to the transfer by comparing source directory
rsync -a --delete /home/ACCOUNT_NAME/BACKUP_FOLDER/ REMOTE_USER@REMOTE_SERVER_URL:/home/ACCOUNT_NAME/public_html/SITE_NAME_BACKUP/

#Email the backup link
echo "Latest backup is available at http://REMOTE_SERVER_URL/SITE_NAME_BACKUP/backup-${date}-${key}_SITE_NAME.tgz" | mail -s "Server backup available for download" USERNAME@DOMAIN.COM
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.