<?php
/*
dv at josheli.com

What it does: 
It will backup all accounts under a reseller.
I use this script on my home WinXP machine to backup all my sites on the Vox servers.

How it works:
Reads all accounts (domain and username specifically) from the listaccts page in WHM
Creates filenames like "bkup_mydomain_com_11_11_03.tar.gz"
Invokes wget to call the getbackup routine of cpanel for each account, using the account's username and the reseller's password

Caveats:
WGET must be installed. Wget for Windows: 
http://xoomer.virgilio.it/hherold/

This script doesn't require cron, but i use it. Cron for Windows: 
http://www.kalab.com/freeware/cron/cron.htm 

Instructions:
1. Install Wget if needed
2. Create a "backup" (or whatever) directory somewhere
3. Place this file in that "backup" directory
4. Modify "config" variables below, to suit
5. From the command prompt:
    >cd /path/to/backup
    >php backup_all.php
6. ([Or] Optionally) Set up a cron job to call this script

Notes: 
*wget 1.91 has the openSSL libraries back, so if you want, you could download
your backups over SSL
*the user and password are still passed through the url, so they're visible by pretty
much anyone, if they're looking.
*This script could easily be run from the server (but why backup a site on its own server); running it from another server is a pretty good option though.
*I have a cable internet connection. It takes ~17 minutes to download ~ 114mb of tar.gz backups
*Remember that you will be using the sub-accounts' bandwidth
*My firewall needed to be configured to allow PHP to access the internet.
*I'm using the PHP 5 cli, which doesn't change the current working directory, thus
the backups are downloaded to wherever this file is.
*Other options: If you just want to backup the public files (i.e. html files/images, and not
scripts, mail, etc.), I would suggest you just use wget by itself, with its myriad of options.

*/

/*** BEGIN CONFIG ***/

$reseller 'www.mydomain.com';//your reseller url
$whmUser 'username';//your web host manager user name
$whmPass 'password';//your web host manager password
$wget 'C:\\dev\\omnigrab\\wget\\wget.exe';//path to wget executable (maybe just 'wget' if wget is already in your path); double slashes for windows
$sleep 5;//how many seconds to pause between downloads (be nice to your shared server friends :) )
$bkupReseller true;//do you want to backup the reseller account also? (it's not in the listaccts page)

/*** END CONFIG ***/


set_time_limit(0);
//get the WHM 'list accounts' page
$page2 = @file_get_contents("http://$whmUser:$whmPass@$reseller:2086/scripts2/listaccts?viewall=1");

//get each accounts table row
$int2 preg_match_all("/<tr class=(?:tdshade2|tdshade1)>(.*?)<\/tr>/is"$page2$matches);

if(
$int2 && is_array($matches[1])) { 
    foreach(
$matches[1] as $match) {        
        
$account explode('</td><td>',$match);
        
$account[0] = strip_tags(trim($account[0]));//domain
        
$account[2] = strip_tags(trim($account[2]));//username
        
        
$fileName 'bkup_'.str_replace('.','_',$account[0]).'_'.date('n_j_y');

        
//this is all one line
        
exec("$wget -nH --cut-dirs=1 --http-user=$account[2] --http-passwd=$whmPass http://$account[0]:2082/getbackup/$fileName.tar.gz");
        
        
sleep($sleep);
    }
}
if(
$bkupReseller){
    
//this is all one line
    
exec("$wget -nH --cut-dirs=1 --http-user=$whmUser --http-passwd=$whmPass http://$reseller:2082/getbackup/$fileName.tar.gz");
}