Setelah proses pembuatan website selesai dibuat, maka tahap yang paling susah (menurut saya) adalah maintenance. Tahap maintenance inilah yang paling banyak memakan waktu para administrator / developer. Dari proses backup, pencarian bugs serta patch dan lain-lain.

Maka dari itu, beberapa minggu yang lalu saya buat simple php code untuk remote backup cPanel ke FTP server. Semoga dapat berguna bagi para developer dan administrator.

Berikut ini adalah source code nya :

<?php
/**
 * cPanel FTP Full Remote Backup
 * by ditatompel < ditatompel [at] gmail [dot] com >
 * 
 * @version 1.00
 * @author Christian Ditaputratama <[email protected]>
 * 
 * Periodic generate full cPanel backups,
 * optionally to a remote FTP server.(Use cron job)
 * 
 * Greetings for all members of devilzc0de.org, all Indonesian c0ders,
 * and all GNU Generation ;-)
 * Thanks to : 5ynL0rd who always inspire me, I glue you all my regards.
 * 
 * SECURITY : This script contains your cPanel and remote FTP
 * password, KEEP SECURE ACCESS TO THIS FILE !
 * Use at your own risk!
 *------------------------------------------------------------------------+
 * This program is free software; you can redistribute it and/or modify   |
 * it under the terms of the GNU General Public License version 2 as      |
 * published by the Free Software Foundation.                             |
 *                                                                        |
 * This program is distributed in the hope that it will be useful,        |
 * but WITHOUT ANY WARRANTY; without even the implied warranty of         |
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          |
 * GNU General Public License for more details.                           |
 *                                                                        |
 * You should have received a copy of the GNU General Public License      |
 * along with this program; if not, write to the                          |
 *   Free Software Foundation, Inc., 59 Temple Place, Suite 330,          |
 *   Boston, MA 02111-1307 USA                                            |
 *                                                                        |
 * Please submit changes of the script so other people can use            |
 * them as well. This script is free to use, don't abuse.                 |
 *------------------------------------------------------------------------+
 */

$config = array();

$config['domain'] = 'my-domain.co.id'; // domain name where you want to backup the file
$config['cpanel_user'] = 'my_username'; // your cPanel username
$config['cpanel_pass'] = 'my_password'; // your cPanel password
$config['cpanel_skin'] = 'rvskin'; // your cPanel skin you use. Eg: x3, rvskin, etc.

$config['remote_ftp_host'] = 'ftp.remote-host.com'; // Full hostname or IP address for FTP host
$config['ftp_backup_dir'] = '/path/to/backup/dir/'; // Remote dir (defaut = / )
$config['ftp_user'] = 'my-ftp-username'; // username for FTP account
$config['ftp_pass'] = 'my-ftp-password'; // password for FTP account
$config['ftp_mode'] = 'ftp'; // FTP mode ("ftp" for active, "passiveftp" for passive)
$config['ftp_port'] = '21'; // FTP Port (default = 21)

$config['secure_mode'] = 0; // Set to 1 for SSL (requires SSL support), otherwise will use standard HTTP
$config['report_to'] = '[email protected]'; // Email address to send results
$config['debug_mode'] = FALSE; // Set to true to have web page result appear in your cron log
// end configuration

$auth_string = base64_encode($config['cpanel_user'] . ":" . $config['cpanel_pass']);
$sock_exec_uri = "dest=" . $config['ftp_mode'] . 
    "&email=" . $config['report_to'] . 
    "&server=" . $config['remote_ftp_host'] . 
    "&user=" . $config['ftp_user'] .
    "&pass=" . $config['ftp_pass'] . 
    "&port=" . $config['ftp_port'] . 
    "&rdir=" . $config['ftp_backup_dir'] . 
    "&submit=Generate Backup";

$socket = fsockopen($config['domain'], 2082);
if ($config['secure_mode']) :
    $socket = fsockopen("ssl://" . $config['domain'], 2083);
endif;

if (!$socket) {
    echo "Failed to open domain socket connection!\n";
    exit;
}

// Make POST to cPanel
fputs($socket,"POST /frontend/" . $config['cpanel_skin'] . "/backup/dofullbackup.html?" . $sock_exec_uri . " HTTP/1.0\r\n");
fputs($socket,"Host: " . $config['domain'] . "\r\n");
fputs($socket,"Authorization: Basic " . $auth_string . "\r\n");
fputs($socket,"Connection: Close\r\n");
fputs($socket,"\r\n");

// Grab response even if we don't do anything with it.
while (!feof($socket)) {
    $resp = fgets($socket,4096);
    if ($config['debug_mode']) {
        echo $resp;
    }
}
fclose($socket);
/* EOF */
?>

The Concepts

melakukan Unix domain socket connection ke

http://host:port/frontend/[cpanel_skin]/backup/dofullbackup.html?dest=[mode]&email=[email]&server=[ftpserver]&user=[ftp_username]&pass=[ftp_password]&port=[ftp_port]&rdir=[target_dir]&submit=[Generate Backup] dengan Basic Authorization format base64_encode(username:password)

$auth_string = base64_encode($config['cpanel_user'] . ":" . $config['cpanel_pass']); 

Basic Authorization yang kita gunakan menggunakan fputs() ke dalam fungsi fsockopen()

Format : base64_encode(username:password)

$sock_exec_uri = "dest=" . $config['ftp_mode'] . 
    "&email=" . $config['report_to'] . 
    "&server=" . $config['remote_ftp_host'] . 
    "&user=" . $config['ftp_user'] .
    "&pass=" . $config['ftp_pass'] . 
    "&port=" . $config['ftp_port'] . 
    "&rdir=" . $config['ftp_backup_dir'] . 
    "&submit=Generate Backup";  

Target url variable yang akan kita eksekusi nanti;

format :

dest=ftp&[email protected]&server=ftp.devilzc0de.go.id&user=ftp_username&pass=ftp_password&port=21&rdir=/&submit=Generate Backup

// buka Unix domain socket connection untuk standart HTTP cPanel 
// default port standard HTTP cPanel adalah 2082
$socket = fsockopen($config['domain'], 2082);
if ($config['secure_mode']) :
    // gunakan protokol SSL jika $config['domain'] bernilai TRUE ( 1 )
    // standard secure connection cPanel adalah 2083
    $socket = fsockopen("ssl://" . $config['domain'], 2083);
endif; 
if (!$socket) {
    echo "Failed to open domain socket connection!\n";
    // jika gagal melakukan login ke cPanel, jangan eksekusi code selanjutnya
    exit;
} 
fputs($socket,"POST /frontend/" . $config['cpanel_skin'] . "/backup/dofullbackup.html?" . $sock_exec_uri . " HTTP/1.0\r\n");
fputs($socket,"Host: " . $config['domain'] . "\r\n");
fputs($socket,"Authorization: Basic " . $auth_string . "\r\n"); 

lakukan metode post ke cPanel ke url

contoh :

http://devilzc0de.or.id:2082/frontend/rvmaroon/backup/dofullbackup.html?dest=ftp&[email protected]&server=ftp.devilzc0de.go.id&user=ftp_username&pass=ftp_password&port=21&rdir=/&submit=Generate dengan Basic Authorization format base64_encode(username:password)

Eksekusi script tersebut, maka anda akan menerima email begitu proses generate backup telah selesai.

Jangan lupa cek pada remote FTP server anda apakah proses backup telah berjalan dengan baik.

Anda dapat menggunakan fitur Unix cronjob untuk eksekusi script ini dengan periode tertentu.

Silahkan dikembangkan sendiri menurut kreativitas teman-teman sekalian..

SECURITY : This script contains your cPanel and remote FTP password, KEEP SECURE ACCESS TO THIS FILE AND NEVER USE IT ON PUBLIC NETWORK! Use at your own risk!