A recent improvement in CPanel security means that you can no longer download your site’s backups using wget.
CPanel now disables “Basic Authentication” and instead uses cookies and a “cpsession” value embedded in the admin URLs. Whilst this is said to reduce the incidence of certain types of attacks it also creates major headaches for any admins who need to automatically retrieve their site or database backups.
There are many CPanel backup scripts floating around but all the examples I found still used “Basic Authentication” so were rendered useless by these new authentication requirements.
CPanel offers an undocumented feature in their XML-API to create a Full Site Backup however in my instance there were problems with this.
Unlike regular backups the Full Site Backup cannot be restored from within the CPanel admin (though it can be used by webhosts to transfer an entire site).
Also the Full Site Backup does not give you a download link, instead it begins generating an archive in your site’s space (thus potentially sending you over quote), and then once finished it fires you off an email. Alternatively it can FTP/SCP it to a remote server, but this requires you to have a server available as well as making it more difficult for scheduling multiple backups if your backup server has limited bandwidth.
A Solution
Below is a script I whipped up to download CPanel backups using curl. It logs into the admin site just like a web browser then uses the authentication cookies and the “cpsession” to download the site backups. It only requires the curl application and runs in bash so doesn’t require PHP or any other languages to run.
If you find it useful then please leave me a comment.
#!/bin/bash ## ## Download CPanel backups using Cookie authentication ## ## Backups: Home dir, Aliases, Filters and specified Databases ## ## This script is provided as is with no implied warranty. ## Use at your own risk. ## Copyright (c) 2012, McFang.com ########################################################## # Create a configuration file with the following # block of variables and call this script giving # the configuration file as the first argument. ########################################################## ## CPanel domain name #DOMAIN=example.com ## actual shared host url, should match the SSL certificate #SITE=webhost.example.com ## CPanel user & Password #USER=user #PASS=pass ## Databases to backup #DBarray=( db1 db2 db3 ) ## Dir for saving backups. No trailing slash. #DIR=/backups/$USER/`eval date +%Y%m%d` ########################################################## CONFIG=$1 if [ "$CONFIG" == "" ]; then echo "Usage: `basename $0` <config-file>" exit 1; fi if [ -f $CONFIG ]; then . $CONFIG else echo "Config file not found" exit 2; fi mkdir -p $DIR if [ "$?" -gt "0" ]; then echo "FAILED creating backup folder: $DIR"; exit 3; fi PWD=`pwd` cd $DIR # Submit POST login and save cookies to file REDIRECT=`curl -f -o /dev/null -w "%{redirect_url}" -c cookies.txt -d "user=${USER}&pass=${PASS}" -ssl https://${SITE}:2083/login/` #echo "Redirect to ${REDIRECT}" # eg. https://example.com:2083/cpsess11603609/frontend/x3/index.html?post_login=42460004784831 if [ "$?" -gt "0" ]; then echo "FAILED logging into $DOMAIN"; exit 3; else echo "Logged into $DOMAIN, downloading backups to $DIR"; fi CPSESS=`echo ${REDIRECT} | sed 's/.*\(cpsess[0-9]*\).*/\1/'` #echo "CPsession ${CPSESS}" # Download aliases curl -f -O -b cookies.txt -ssl https://${SITE}:2083/${CPSESS}/getaliasbackup/aliases-${DOMAIN}.gz if [ "$?" -gt "0" ]; then echo "FAILED downloading aliases"; fi # Download filters curl -f -O -b cookies.txt -ssl https://${SITE}:2083/${CPSESS}/getfilterbackup/filter_info.${USER}.yaml.gz if [ "$?" -gt "0" ]; then echo "FAILED downloading filters"; fi # Download Databases for element in $(seq 0 $((${#DBarray[@]} - 1))) do #echo -n "${DBarray[$element]}" curl -f -O -b cookies.txt -ssl https://${SITE}:2083/${CPSESS}/getsqlbackup/${USER}_${DBarray[$element]}.sql.gz if [ "$?" -gt "0" ]; then echo "FAILED downloading database: ${DBarray[$element]}"; fi done # Download backup curl -f -O -b cookies.txt -ssl https://${SITE}:2083/${CPSESS}/getbakcup/backup-${DOMAIN}.tar.gz if [ "$?" -gt "0" ]; then echo "FAILED downloading backup"; fi # Cleanup rm -f cookies.txt cd $PWD