#!/usr/local/bin/php -n
<?php
//VERSION=1.2
//User backup script, allowing DA Users to create backups via a cronjob.

require_once('httpsocket.php');

/*******************************
**  Set your variables here
*******************************/

$username = "youruserame";
$password = "yourpassword";

// you must specify one of your domain names. (Any one of them will be fine)
$domain = "yourdomain.com";

// These usually dont require modification
// if you use https to connect to DA, set $ssl = 1;
$host = "localhost";
$ssl = 0;
$port = 2222;

/*******************************
**  Install Instructions
*******************************

1) Place this file somewhere under your home directory.
A good place might be:
/home/username/backup.php

From the File Manager in DA, that would be:
/backup.php

Also download this file into the same directory, to be included by backup.php:
https://files.directadmin.com/services/all/httpsocket/httpsocket.php


2) Chmod the file to 700.  This is very important for security.

3) Edit the above variables.
Most likely, you''ll only need to change the username and password.
If your system uses ssl to connect to DA (https) then you'll need to set $ssl = 1;

4) Create the cronjob to execute:

/home/username/backup.php

as often as you wish. Usually once per day is the most you'll want to run it.
To create the cronjob, see:
http://www.site-helper.com/misc.html#cron

Usually a very early morning hour, say 5am is best as the load is usually lowest then.

Note that we do not need to add /usr/local/bin/php in front of this php script because of the
#!/usr/local/bin/php -n
line at the very top.

5) check your 'username' account.  Any emails will be delivered there if it doesn't work.
If needed, you can test manually via ssh by running:

/home/username/bacukp.php



6) Optional:  If you wish to have an ftp upload along with the bacukp, create a
cronjob to run about an hour later than the backup, and use the command:

/usr/bin/ncftpput -f /home/username/ftp.conf -V -t 25 -m "/ftp/path/" "/home/username/backups/backup-`date +%b-%e-%Y`-1.tar.gz"

and in the ftp.conf, put:

host 1.2.3.4
user ftpuser
pass ftppass

Then chmod the ftp.conf to 700.
Note this assumes you've only got 1 bacukp per day since it uses -1.tar.gz and doesn't take into account possible -2.tar.gz, etc.

********************************
*******************************/

$sock = newSock();
$c=0;
$sock->query('/CMD_API_SITE_BACKUP',
	array(
		'action' => 'backup',
		'domain' => $domain,
		'form_version' => '4',
		'select'.$c++ => 'domain',
		'select'.$c++ => 'subdomain',
		'select'.$c++ => 'email',
		'select'.$c++ => 'email_data',
		'select'.$c++ => 'forwarder',
		'select'.$c++ => 'autoresponder',
		'select'.$c++ => 'vacation',
		'select'.$c++ => 'list',
		'select'.$c++ => 'emailsettings',
		'select'.$c++ => 'ftp',
		'select'.$c++ => 'ftpsettings',
		'select'.$c++ => 'database',
		'select'.$c++ => 'database_data',
	));

$result = $sock->fetch_parsed_body();

if ( $result['error'] != "0" )
{
	echo "Error issuing backup creation: ".$result['details']."\n";
	exit(1);
}

//all should be fine.
exit(0);

function newSock()
{
        global $host;
        global $port;
        global $ssl;
        global $username;
	global $password;

        $tsock = new HTTPSocket;
        $tsock->set_method('POST');
        $tsock->set_login($username, $password);

        if ($ssl)
        {
                $tsock->connect("ssl://$host", $port);
        }
        else
        {
                $tsock->connect("$host", $port);
        }

        return $tsock;
}
?>