
Originally Posted by
zulhadi.rahmat51
Dear guys,
Since there's no backup service on cpanel for free hosting. Here's the script to backup your public_html, public_ftp and any target you want to backup. This is very simple script with simple step: copy to backup dir --> compress --> delete old one. You can modify and make it as you need.
Code:
<?php
define('DS', DIRECTORY_SEPARATOR); // ==== SLASH
/* *********** EDIT HERE ***************** */
define('BKUP_DIR', "/home/xxxx/_BACKUP"); // ===== YOUR BACKUP WILL BE HERE
define('WEBROOT_DIR', "/home/xxxx/public_html"); // ===== YOUR WEB ROOT DIRECTORY
define('FTPROOT_DIR', "/home/xxxx/public_ftp");// ===== YOUR FTP ROOT DIRECTORY
/* *************************************** */
// =========== COPY RECURSIVE ===========
function copy_r( $path, $dest ) {
if( is_dir($path) ){
@mkdir( $dest );
$objects = scandir($path);
if( sizeof($objects) > 0 ){
foreach( $objects as $file ) {
if( $file == "." || $file == ".." )
continue;
// go on
if( is_dir( $path.DS.$file ) ) {
copy_r( $path.DS.$file, $dest.DS.$file );
}
else{
copy( $path.DS.$file, $dest.DS.$file );
}
}
} return true;
} elseif( is_file($path) ) {
return copy($path, $dest);
} else {
return false;
}
}
// =========== DELETE RECURSIVE ==========
function del_r($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir."/".$object) == "dir") del_r($dir."/".$object); else unlink($dir."/".$object);
}
}
reset($objects);
rmdir($dir);
}
}
// ========== COMPRESS RECURSIVE =========
function comp_r($source, $destination) {
if (!extension_loaded('zip') || !file_exists($source)) {
return false;
}
$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
return false;
}
$source = str_replace('\\', '/', realpath($source));
if (is_dir($source) === true) {
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) {
$file = str_replace('\\', '/', realpath($file));
if (is_dir($file) === true) {
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
}
else if (is_file($file) === true) {
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}
}
else if (is_file($source) === true) {
$zip->addFromString(basename($source), file_get_contents($source));
}
return $zip->close();
}
// =========== BACKUP ===========
function bkup($src, $bkup_name){
copy_r($src, BKUP_DIR.DS.$bkup_name);
comp_r(BKUP_DIR.DS.$bkup_name, BKUP_DIR.DS.$bkup_name.".zip");
del_r(BKUP_DIR.DS.$bkup_name);
}
// =============== ACTION =================
/* ********* Create Backup Here ******** */
$now = date(Ymd);
bkup(WEBROOT_DIR, "WEB_".$now);
bkup(FTPROOT_DIR, "FTP_".$now);
?>
For other backup you can modify the "bkup" function. Actually this only for file backup, I'm on working for MySQL backup script. You can run it via cronjob, as what I did. Just set your cronjob every month, six month, every year or whenever you want.
Code:
30 03 1 * * php -f /PATH/TO/SCRIPT/backup.php
You are very welcome to modify, develope and improve this script to better one. Dont hesitate to share it also.
See u around.
_ZR_
---------- Post added at 04:45 AM ---------- Previous post was at 04:29 AM ----------
PS: I'm also working on adding mysql backup script and auto send to other server via FTP

Hi guys.. Thanx 4 your feedback.. And after a long long time, here is this the script for MySQL Backup, you can customize your database backup at "bkup_db" function.
Code:
// =========== DB BACKUP ============
function bkup_db($db_host,$db_user,$db_pass,$db_name,$tables = '*'){
$link = mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db_name,$link);
// GET ALL TABLES
if($tables == '*'){
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result)){
$tables[] = $row[0];
}
}
else{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
// LOOPING
foreach($tables as $table){
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++){
while($row = mysql_fetch_row($result)) {
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++) {
$row[$j] = addslashes($row[$j]);
$row[$j] = @ereg_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}
//SAVE SQL FILE
$handle = fopen(BKUP_DIR.DS.'DB_'.$db_name.'_'.date(Ymd).'.sql','w+');
fwrite($handle,$return);
fclose($handle);
}
$db_user = "XXXXX" //==== YOUR MYSQL USERNAME
$db_pass = "XXXXX" // ===== YOUR MYSQL PASSWORD
bkup_db('localhost', $db_user, $db_pass, 'database_1'); === // BACKUP ALL TABLE ON database_1
bkup_db('localhost', $db_user, $db_pass, 'database_2'); === // BACKUP ALL TABLE ON database_2
bkup_db('localhost', $db_user, $db_pass, 'database_2', 'table_1'); === // BACKUP table_1 ON database_2
//================================================================================================================
Put the script together on the previous script, so you have a complete backup script. Then, find the backup at your BACKUP_DIR.
CU.
---------- Post added at 04:04 AM ---------- Previous post was at 04:02 AM ----------
Hi guys.. Thanx 4 your feedback.. And after a long long time. Here is this the script for MySQL Backup, you can customize your database backup at "bkup_db" function.
Code:
// =========== DB BACKUP ============
function bkup_db($db_host,$db_user,$db_pass,$db_name,$tables = '*'){
$link = mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db_name,$link);
// GET ALL TABLES
if($tables == '*'){
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result)){
$tables[] = $row[0];
}
}
else{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
// LOOPING
foreach($tables as $table){
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++){
while($row = mysql_fetch_row($result)) {
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++) {
$row[$j] = addslashes($row[$j]);
$row[$j] = @ereg_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}
//SAVE SQL FILE
$handle = fopen(BKUP_DIR.DS.'DB_'.$db_name.'_'.date(Ymd).'.sql','w+');
fwrite($handle,$return);
fclose($handle);
}
$db_user = "XXXXX" //==== YOUR MYSQL USERNAME
$db_pass = "XXXXX" // ===== YOUR MYSQL PASSWORD
bkup_db('localhost', $db_user, $db_pass, 'database_1'); === // BACKUP ALL TABLE ON database_1
bkup_db('localhost', $db_user, $db_pass, 'database_2'); === // BACKUP ALL TABLE ON database_2
bkup_db('localhost', $db_user, $db_pass, 'database_2', 'table_1'); === // BACKUP table_1 ON database_2
//================================================================================================================
Put the script together on the previous script, so you have a complete backup script. Then, find the backup at your BACKUP_DIR.
CU.