Akom's Tech Ruminations

Various tech outbursts - code and solutions to practical problems
Linux

Backing up cPanel 11 hosted account with wget and dav/rsync

Posted by Admin • Thursday, August 21. 2014 • Category: Linux

I want to back up my hosting account regularly by retrieving everything onto my box somewhere else (my home server). By regularly, I mean every day. I want this to happen automatically. cPanel makes that hard to do, but there is always a way to script things.

First, let's break down what we want to back up:
  1. Files
  2. Databases and email forwarders
Files are fairly easy. Obviously we don't want to use the full backup functionality of cPanel because we'd be transferring your entire storage space each and every time, even if nothing has changed. In order to do it efficiently, you have a few options:
  1. FTP account using recursive wget (create an FTP account with required access and teach an ftp client of your choice to recursively transfer everything. Hopefully this client skips unchanged files)
  2. WebDAV using rsync (this is what I'm using). Just mount, back up, unmount
Databases and Email stuff is not as easy, as we do have to log into cPanel. The trick to logging in to cPanel is:
  1. Submit your login to the log-in page and save cookies
  2. Parse the resulting file, find the backup link which includes your session name in the URL and hit that
  3. Only accept .gz files (DB backups and email stuff), but avoid hitting /logout, and don't start spidering the entire website
So, how do we do this? Here is the plan


USER="your_username"
PASS="your_password"
DOMAIN="your_domain"
URL="https://$DOMAIN:2083/login/"

# Step 1: login to cpanel
wget -k --no-check-certificate --keep-session-cookies --save-cookies /tmp/my.backup.cookies.txt --post-data="user=$USER&pass=$PASS" $URL -O index.html


# Step 2: determine backup page URL
BACKUP_URL=$(grep 'backup/index.html' index.html | grep href | sed 's/^.*href="\([^"]*\)".*$/\1/' | tail -n 1)
echo "Backup URL is $BACKUP_URL"


# Step 3: get everything on that page except the file backup
wget  -e robots=off --load-cookies /tmp/my.backup.cookies.txt --no-check-certificate -r -X/logout -l1 -A.gz -R/ -Rlogout -Rindex.html -Rrobots.txt -R"backup-$DOMAIN*"  $BACKUP_URL
 
We save the front page first, because we can't do everything in one request. Once we have the backup page URL, we spider avoiding /logout in various forms (-X/logout) and anything that's more than one hop away from this page (-l1).

Other fun options:
Adding -nd -P cpanel will cause wget to ignore all local directories when saving files and instead pile everything it downloads to one directory called cpanel.

0 Trackbacks

  1. No Trackbacks

0 Comments

Display comments as (Linear | Threaded)
  1. No comments

Add Comment


You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.
Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
Markdown format allowed


Submitted comments will be subject to moderation before being displayed.