+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13
Like Tree2Likes

Thread: Backup script for freehosting

  1. #1
    zulhadi.rahmat51 is offline x10Hosting Member zulhadi.rahmat51 is an unknown quantity at this point
    Join Date
    Oct 2011
    Posts
    2

    Backup script for freehosting

    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

  2. #2
    jfrancis79 is offline x10Hosting Member jfrancis79 is an unknown quantity at this point
    Join Date
    Jul 2011
    Posts
    83

    Re: Backup script for freehosting

    Is there a demo and what are the requirements?
    dinomirt96 likes this.

  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Backup script for freehosting

    The recursion can also be handled by the SPL classes RecursiveDirectoryIterator and RecursiveIteratorIterator.

    PHP Code:
    function copy_r($path$destination) {
        
    # ensure paths end in "/"
        
    $path preg_replace('%/?$%''/'$path);
        
    $destination preg_replace('%/?$%''/'$destination);
        
        
    $dir = new RecursiveDirectoryIterator($path);
        
    $dir->setFlags(FilesystemIterator::SKIP_DOTS);
        
    $files = new RecursiveIteratorIterator($dirRecursiveIteratorIterator::SELF_FIRST);
        
    $pathLen strlen($path);
        
        foreach (
    $files as $from => $file) {
            
    $to substr_replace($from$destination0$pathLen);
            if (
    $file->isDir()) {
                
    mkdir($to);
            } else {
                
    copy($from$to);
            }
        }
    }

    function 
    del_r($base) {
        
    $dir = new RecursiveDirectoryIterator($base);
        
    $dir->setFlags(FilesystemIterator::SKIP_DOTS);
        
    $files = new RecursiveIteratorIterator($dirRecursiveIteratorIterator::CHILD_FIRST);
        foreach (
    $files as $path => $file) {
            if (
    $file->isDir()) {
               
    rmdir($path);
            } else {
               
    unlink($path);
            }
        }

    Better would be to use rsync, though as far as I know it isn't supported on the X10 free hosts. csync looks to be a viable alternative, as are lftp and ncftp. All would run on the backup server and fetch the files from the site to be backed up. Unlike rsync and the backup script, I don't believe these will compress the backup for transfer.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  4. #4
    pb1242883 is offline x10Hosting Member pb1242883 is an unknown quantity at this point
    Join Date
    Mar 2011
    Location
    India
    Posts
    13

    Re: Backup script for freehosting

    wow buddy nice share i will use it Thank you for sharing
    karimirt47 likes this.

  5. #5
    jfrancis79 is offline x10Hosting Member jfrancis79 is an unknown quantity at this point
    Join Date
    Jul 2011
    Posts
    83

    Re: Backup script for freehosting

    Thank you for sharing.

  6. #6
    studentsofaliah52 is offline x10Hosting Member studentsofaliah52 is an unknown quantity at this point
    Join Date
    May 2011
    Location
    your root!
    Posts
    2

    Re: Backup script for freehosting

    Thanks so much for sharing!!!

  7. #7
    secretlosserx21 is offline x10Hosting Member secretlosserx21 is an unknown quantity at this point
    Join Date
    Dec 2011
    Posts
    1

    Re: Backup script for freehosting

    tnx for thix

  8. #8
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Backup script for freehosting

    Enough with the contentless posts. If you guys want to thank someone, click the "Like" link on the post. Otherwise, it's just noise.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  9. #9
    wownoypi is offline x10Hosting Member wownoypi is an unknown quantity at this point
    Join Date
    Jan 2012
    Posts
    1

    Re: Backup script for freehosting

    thanks for this/.. this is usefull

  10. #10
    alfred4w is offline x10Hosting Member alfred4w is an unknown quantity at this point
    Join Date
    Dec 2011
    Posts
    3

    Re: Backup script for freehosting

    Quote Originally Posted by zulhadi.rahmat51 View Post
    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
    very nice help me a lot

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. backup script
    By xiaomao in forum Programming Help
    Replies: 1
    Last Post: 08-07-2010, 01:21 PM
  2. Wordpress backup not working (Datablase backup script)
    By Danielx386 in forum Free Hosting
    Replies: 3
    Last Post: 10-08-2009, 07:11 PM
  3. Cron daily backup script
    By taha116 in forum Scripts & 3rd Party Apps
    Replies: 8
    Last Post: 06-30-2009, 05:37 PM
  4. Error: REMOTE_ADDR not set, PHP Backup Script
    By TechAsh in forum Programming Help
    Replies: 3
    Last Post: 03-08-2008, 02:47 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers