zulhadi.rahmat51
New Member
- Messages
- 2
- Reaction score
- 1
- Points
- 0
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.
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.
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 :smile:
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 :smile: