Skip to content

Automatic cPanel Backup via Curl & Cron

This setup creates a full backup of your cPanel account each month, maintaining one backup for each previous year, and monthly backups for the current year. 

You need two files:

  • crontab.txt (for setting up the cron job, that is, the automation)
  • full_backup.sh (the actual backup commands)

Crontab.txt

MAILTO="webmaster@example.com"
 
5 4 28 * * /home/yourdomain/full_backup.sh
5 4 31 12 * /home/yourdomain/full_backup.sh --year

You'll have to set your crontab with a command like:

crontab crontab.txt

You can see what's in your crontab with the following command:

crontab -l

full_backup.sh

###
# cPanel Backup via Cron v1.2
###

###
#  Instructions
###
# Make sure CP_SCP_BACKUP_ROOT directory exists. It is "web_backup" by default.
# Within this directory you'll need a directory for each cPanel account you're
# backing up.
#
# I.e. if you're backing up "example.com" and "foobar.com" and you want them
# to be backed up in the default location of "web_backup", you'll need to
# execute the following commands at the recieving end:
# mkdir ~/web_backup
# mkdir ~/web_backup/example.com
# mkdir ~/web_backup/foobar.com
###

#cPanel info -- host that is being backed up
CP_USERNAME=username
CP_PASSWORD=password
CP_DOMAIN=yourdomain.com
CP_SKIN=x3 # plain "x" is probably the most common one

#SCP/FTP info -- host to receive the backup file
CP_SCP_USER=username
CP_SCP_PASS=password
CP_SCP_HOST=example-backup-host.com
CP_SCP_PORT=22

# see cPanel backup page for alternatives of transfer mode
CP_SCP_MODE=scp 

# target directory on remote host; do not include domain names in this one
CP_SCP_BACKUP_ROOT=web_backup%2F  # url encoded, e.g. "/" equals "%2F"

# creates the target path (directory and filename)
# if you make changes, remember to url encode slashes, i.e. "/" equals "%2F"
# if --year option is used, script creates a yearly filename, otherwise a monthly one
if [ "$1" = '--year' ]; then
	CP_SCP_RDIR=$CP_SCP_BACKUP_ROOT$CP_DOMAIN%2F$CP_DOMAIN-year-`date +%Y`.tar.gz
else
	CP_SCP_RDIR=$CP_SCP_BACKUP_ROOT$CP_DOMAIN%2F$CP_DOMAIN-month-`date +%m`.tar.gz
fi

# email address for sending notification after backup is completed
CP_EMAIL=webmaster@example.com
# initiate backup (the following command should be on one line)
curl --silent --insecure --user $CP_USERNAME:$CP_PASSWORD -d "dest=$CP_SCP_MODE&email=$CP_EMAIL&server=$CP_SCP_HOST&user=$CP_SCP_USER&pass=$CP_SCP_PASS&port=$CP_SCP_PORT&rdir=$CP_SCP_RDIR" https://$CP_DOMAIN:2083/frontend/$CP_SKIN/backup/dofullbackup.html > /dev/null

# gets curl return code
CURL_EXIT=$?

# curl encountered an error
if [ $CURL_EXIT -gt 0 ]; then
	echo curl returned exit status $CURL_EXIT - see curl manual pages for more details
	exit 69
fi

# everything ok
exit 0

Put the file, say, in your account root and make the file executable (e.g. with command "chmod 700 full_backup.sh").

Let me know in the comments how it works for you. Thanks!

Monthly Archives: April 2008

TextEdit - You Hate It Or You Hate It With Passion

22-Apr-08

OS X's very own TextEdit. I thought it would prove (some day) to be a valuable program. But no. Years went by and nothing changed. It's as crap as it was when I first used it. The final bit was when I copy pasted something from two places into one TextEdit document. Two texts were […]

Shortcut to "Paste: Unformatted text" In Word

18-Apr-08

I hate copying something, say, from web, and trying to paste it in Word (or TextEdit, shudders.. more of that later). Web text always comes with weird web formatting. And I wanted just text. To get the text, and not the formatting, I have to click through Edit -> Paste Special -> Unformatted text -> […]

Cron & Leopard: Leopard Killed Cron, Use Lingon Instead of Cronnix

18-Apr-08

I've been a happy user of both OS X 10.5 Leopard, and cron (via Cronnix). Up until now. The console said, each time the cron was supposed to be initiated: 14.4.2008 14.4. 22:45:00 com.apple.launchd[1] (0x10e220.cron[41090]) Could not setup Mach task special port 9: (os/kern) no access Apparently, in Leopard cron is being replaced with launchd, […]

cPanel Email Forward to Several Addresses

15-Apr-08

I'm not sure in how many different uses this has, but cPanel allows email forwarding to several addresses (by using several forwards, from address A to address B, A to C, A to D, ...). For instance, I created an email address that forwards to my wife's inbox and to that of my own. 

WordPress Plugin: Blog Introduction

09-Apr-08

I needed an introductory text on a blog's front page. I also needed this text to be easy to edit. I couldn't find a suitable plugin so I made one.  Blog Introduction displays heading and contents of page with permalink ‘blog-introduction’ on the home page (front page) right before the posts. Hide the ‘blog-introduction’ page by making […]