Skip to content

Automatic cPanel Backup via Curl&Cron v1.1

[UPDATE2 2012-03-14] Use Automatic Dropbox backup instead.

[2008-06-23] UPDATE: Please see the updated version.

A few months back I posted a shell script for backing up your cPanel account automatically using cron and curl. I updated the file a bit, and now it creates files with monthly names (for me, at least).

On January you get month-01, then month-02 and so on. So you're automatically maintaining only 12 files (depending on your cron, of course) as they are being rewritten next year.

I also added an option to create yearly backups as well as monthly ones.

I use a crontab file like this:


MAILTO="webmaster@example.com"

 

5 4 28 * * /home/yourdomain/full_backup.sh
5 4 31 12 * /home/yourdomain/full_backup.sh --year

And the full backup shell script is like this:


###
# cPanel Backup via Cron v1.1
###

#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.com
CP_SCP_PORT=22
CP_SCP_MODE=scp # see cPanel backup page for alternatives
# target path (directory and filename)
# if --year option is used, create a yearly filename, otherwise a monthly one
if [ "$1" = '--year' ]; then
CP_SCP_RDIR=web_backup%2F$CP_DOMAIN%2F$CP_DOMAIN-year-`date +%Y`.tar.gz # url encoded, e.g. "/" equals "%2F"
else
CP_SCP_RDIR=web_backup%2F$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
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

Hope it's of some use.

If you happen to know how to login with curl (or do the things this script does) without typing out the remote host's password in clear text, please share your knowledge in the comments. Thanks!

3 Comments

  1. Jeff wrote:

    Keep getting error with this script:

    Copying backup...Timeout ...
    [a fatal error or timeout occurred while processing this directive]Retrying Copying backup...Timeout ...
    [a fatal error or timeout occurred while processing this directive]Retrying Copying backup...Timeout ...
    [a fatal error or timeout occurred while processing this directive]Unable to scp /home/veggie08/backup-6.6.2008_10-19-17_veggie08.tar.gz (please check the host [ip addy] and user [username] and password)

    = = =

    But the ip, username and pwd are correct in the script. What could be producing the error? The port #??

    Posted on 06-Jun-08 at 7:44 pm | Permalink
  2. jsruok wrote:

    Hmm.. don't know what the problem is.. Wish I could help. Let's see.

    Do you have a "web_backup" directory at your host that should recieve the backup? (I guess I shouldn't use it in the script hard coded.)

    If you try to do the same thing manually via cPanel (with the credentials you inserted in the script) everything works?

    The command between "# initiate backup" and "# gets curl return code" is on one line?

    Does the version 1.0 of this script work for you?

    Posted on 10-Jun-08 at 10:39 am | Permalink
  3. jsruok wrote:

    More precicely, does the final remote dir exist? For instance, if you're backing up example.com, I guess you need the dir web_backup and example.com within that dir, i.e. web_backup/example.com/.

    Thanks for your comments, Jeff. I learned I need to include some sort of instructions in this script.

    Posted on 10-Jun-08 at 10:47 am | Permalink

2 Trackbacks/Pingbacks

  1. [...] [UPDATE 2008-05-20] Please see the updated version for this script [...]

  2. Cpanel backup maken mbv Crontab. on 13-Jun-08 at 11:49 am

    [...] dmv een crontab file in combinatie met Curl, zoals Jussi Ruokmaki het gedaan [...]