Senin, 17 Desember 2012

cPanel FTP Full Remote Backup PHP Script

cPanel FTP Full Remote Backup PHP Script
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, cari bugs belajar 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 Code:
<?php/**
 * cPanel FTP Full Remote Backup
 * by ditatompel < ditatompel [at] gmail [dot] com >
 *
 * @version 1.00
 * @author Christian Ditaputratama <ditatompel@gmail.com>
 *
 * 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'] = 'my-mail@my-domain.co.id'// 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 cPanelfputs($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 */?>

penjelasan untuk konfigurasi variable
$config['domain']
isi dengan nama base anda, misalnya website anda adalah http://www.devilzc0de.or.id/ maka
PHP Code:
$config['domain'] = 'devilzc0de.or.id'

$config['cpanel_skin']
Isi dengan skin cPanel yang sedang aktif.
untuk mengetahui skin apa yang sedang aktif, Cukup login ke cPanel dan sedikit menegok ke URL tersebut. ketawa
Misal URL di browser setelah login ke cPanel adalah http://devilzc0de.or.id:2082/frontend/rvmaroon/index.html?post_login=blahblahblah
Contoh lihat gambar :
[Image: cpanel-skin.png]
maka nilai variable
PHP Code:
$config['cpanel_skin'] = 'rvmaroon'
Jika http://devilzc0de.or.id:2082/frontend/x3/index.html?post_login=blahblahblah
maka nilai variable :
PHP Code:
$config['cpanel_skin'] = 'x3'


$config['remote_ftp_host']
Isi dengan nama host / IP address (bagi yang memiliki VPS) FTP server anda.
misalnya FTP server tujuan backup adalah devilzc0de.go.id, maka
PHP Code:
$config['remote_ftp_host'] = 'ftp.devilzc0de.go.id'

$config['ftp_backup_dir']
Isi dengan full path ke folder backup anda, atau defaultnya gunakan root dir ( "/" )
Default :
PHP Code:
$config['ftp_backup_dir'] = '/'

$config['ftp_mode']
Mode FTP yang ingin digunakan
"ftp" untuk active mode, "passiveftp" untuk passive mode
default :
PHP Code:
$config['ftp_mode'] = 'ftp'

$config['ftp_port']
Port remote FTP server yang digunakan
Default :
PHP Code:
$config['ftp_port'] = '21'

$config['secure_mode']
set value "1" (tanpa tanda petik ) untuk Secure connection ( Membutuhkan SSL )
default :
PHP Code:
$config['secure_mode'] = 0

$config['report_to']
isi dengan alamat email yang nantinya digunakan untuk menerima report jika proses backup telah selesai.

$config['debug_mode']
Set TRUE jika anda menginginkan hasil generate website muncul ( jika anda menggunakan fitur cronjob, hasil generate akan muncul juga pada cron log )
Default :
PHP Code:
$config['debug_mode'] = FALSE


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)


PHP Code:
$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)

PHP Code:
$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=email_saya@mail.com&server=ftp.devilzc0de.go.id&user=ftp_username&pass=ftp_password&port=21&rdir=/&submit=Generate Backup

PHP Code:
// 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; 

PHP Code:
if (!$socket) {
    echo 
"Failed to open domain socket connection!\n";
    
// jika gagal melakukan login ke cPanel, jangan eksekusi code selanjutnya
    
exit;

PHP Code:
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=email_saya@mail.com&server=ftp.devilzc0de.go.id&user=ftp_username&pass=ftp_password&port=21&rdir=/&submit=Generate Backup dengan Basic Authorization format base64_encode(username:password)
----------------- end of concept -----------------


Eksekusi script tersebut, maka anda akan menerima email begitu proses generate backup telah selesai.
Jangan lupa cek pada remote FTP server anda apakan proses backup telah berjalan dengan baik.
[Image: ssh-remote-ftp-backup.png]
Anda dapat menggunakan fitur Unix cronjob untuk eksekusi script ini dengan periode tertentu. ketawa

download scriptnya ada di :
Code:
http://ls-la.ditatompel.crayoncreative.net/scripts/php/cpanel-ftp-backup-1.0.php
Silahkan dikembangkan sendiri menurut kreativitas teman-teman sekalian.. ketawa


Quote:NOTE :
SECURITY : This script contains your cPanel and remote FTP password, KEEP SECURE ACCESS TO THIS FILE ! Use at your own risk!

Regards,
DitatompeL

Tidak ada komentar:

Posting Komentar