An easy script for automatic schedule backup of single cPanel account
It does the following:
- Cleanup the backup folder for any old files
- Adds the current date and a random key (addition of this key is to avoid guessing of url, just an added measure)
- Take SQL backup using mysqldump
- Take the complete public_html and recentely taken mysql backup to a gz file
- 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
- Email the download link
Here’s the script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #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 |