Example #1
0
function generateBackup($cid, $option)
{
    // ----------------------------------------------------------
    // Routine to generate recurse a folder structure and record
    // the files, their sizes and parent folders
    // ----------------------------------------------------------
    global $_CONFIG;
    # generate database backup if required
    if (!$_CONFIG['enable_db_backup']) {
        $backupDatabase = 0;
    } else {
        $backupDatabase = $_REQUEST['dbbackup'];
    }
    if ($_REQUEST[cron_access]) {
        $_CONFIG['backup_refresh'] = 0;
        $_REQUEST['dbbackup_drop'] = $_CONFIG['cron_sql_drop'];
        if ($_CONFIG[cron_btype] == '1') {
            $backupDatabase = 0;
        }
    }
    if (!is_writeable($_CONFIG['clonerPath'])) {
        E_print("Your backup directory " . $_CONFIG['clonerPath'] . " is not writeable or does not exists!");
        return;
    }
    if (!is_writeable($_CONFIG['backups_dir'])) {
        E_print("Required sql backup directory " . $_CONFIG['backups_dir'] . " is not writeable or does not exists!");
        return;
    }
    # generate the backup set filename
    #$urlinfo = parse_url($mosConfig_live_site);
    #$domainname = strtolower(str_replace('.','_',$urlinfo[host]));
    #$domainname= $_SERVER['HTTP_HOST'];
    $domainname = $_CONFIG['mosConfig_live_site'];
    if ($_CONFIG['mem'] && $_CONFIG['backup_refresh']) {
        $f_ext = '.tar';
        $_CONFIG['tarcompress'] = '';
    } elseif ($_CONFIG['backup_compress']) {
        $f_ext = '.tgz';
        $_CONFIG['tarcompress'] = 'z';
    } else {
        $f_ext = '.tar';
        $_CONFIG['tarcompress'] = '';
    }
    /*if($_CONFIG['backup_refresh']){
         $f_ext = ".tar";
        }
        elseif($_REQUEST[cron_dbonly]){
    	 $f_ext = ".tar";
    	}
    	else{
    	 $f_ext = ".tgz";
    	}*/
    if ($_REQUEST['bname'] == "") {
        if ($backupDatabase == 1) {
            if ($_REQUEST['dbbackup_drop']) {
                $filename1 = 'backup_' . date("Y-m-d_H-i") . '_' . $domainname . '-sql-drop' . $f_ext;
            } else {
                $filename1 = 'backup_' . date("Y-m-d_H-i") . '_' . $domainname . '-sql-nodrop' . $f_ext;
            }
        } else {
            $filename1 = 'backup_' . date("Y-m-d_H-i") . '_' . $domainname . '-nosql' . $f_ext;
        }
    } else {
        $filename1 = $_REQUEST['bname'] . $f_ext;
    }
    $sql_file = array();
    if ($backupDatabase == 1) {
        $tables = array();
        $tables[0] = 'all';
        $excltables = $_REQUEST['excltables'];
        $sql_file[] = doBackup($tables, 'sql', 'local', 'both', $_SERVER['HTTP_USER_AGENT'], $_CONFIG['backups_dir'], $databaseResult, 'database', $excltables, $_CONFIG['mysql_database']);
        $databaseResult = "<b>" . $_CONFIG['mysql_database'] . ":</b> " . $databaseResult;
        ############## ADD multiple databases #######################
        $databases_incl = $_REQUEST['databases_incl'];
        if (is_array($databases_incl)) {
            foreach ($databases_incl as $database_name) {
                if ($database_name != '') {
                    $excltables = "";
                    mysql_query("USE  {$database_name}");
                    $sql_file[] = doBackup($tables, 'sql', 'local', 'both', $_SERVER['HTTP_USER_AGENT'], $_CONFIG['backups_dir'], $databaseResult_incl, $database_name, $excltables, $database_name);
                    $databaseResult .= "<br /> <b>{$database_name}:</b> " . $databaseResult_incl;
                }
            }
            mysql_query("USE  " . $_CONFIG['mysql_database']);
        }
    } else {
        $databaseResult = LM_DATABASE_EXCLUDED;
    }
    $excluded = array();
    $data = "";
    $excluded[] = $_CONFIG['backups_dir'] . "/" . $filename1;
    if ($_REQUEST['cron_access']) {
        $data = $_CONFIG['cron_exclude'];
    } elseif ($_CONFIG[select_folders] == 1) {
        $data = $_REQUEST['exluded'];
    }
    ##### START THE EXCLUSION FILTER
    if (($fp = @fopen($_CONFIG['exfile'], "r")) && !$_REQUEST['cron_access']) {
        while (!feof($fp)) {
            $data .= fread($fp, 1024);
        }
        fclose($fp);
    }
    if (sizeof($_REQUEST[cid]) > 0) {
        $data .= implode("\r\n", $_REQUEST[cid]);
    }
    $_COOKIES = explode("\r\n", $data);
    if (is_array($_COOKIES)) {
        foreach ($_COOKIES as $key => $value) {
            if ($value != "") {
                $value = str_replace($_CONFIG['backup_path'], "", $value);
                if ($value[0] != '/') {
                    $value = '/' . $value;
                }
                if (substr($value, strlen($value) - 1, 1) == '/' && strlen($value) != 1) {
                    $value = substr($value, 0, strlen($value) - 1);
                }
                if ($value != "" && $value != '/administrator/backups') {
                    if (trim($value) != '/administrator') {
                        $excluded[] = str_replace("//", "/", $_CONFIG['backup_path'] . "/" . $value);
                    } else {
                        if ($handle = opendir($_CONFIG['backup_path'] . '/administrator')) {
                            while (false !== ($file = readdir($handle))) {
                                if ($file != "." && $file != ".." && $file != "backups") {
                                    $excluded[] = str_replace("//", "/", $_CONFIG['backup_path'] . "/administrator/" . $file);
                                }
                            }
                            closedir($handle);
                        }
                    }
                }
            }
        }
    }
    #### END EXCLUSION FILTER
    if (!$_CONFIG['add_backups_dir']) {
        # initialise list arrays, directories and files separately and array counters for them
        $d_arr = array();
        $d = 0;
        $f_arr = array();
        $f = 0;
        $s_arr = array();
        $s = 0;
        getBackupFiles($d_arr, $f_arr, $s_arr, $d, $f);
        $i = 0;
        while ($i < sizeof($f_arr)) {
            $excluded[] = $_CONFIG['clonerPath'] . "/" . $f_arr[$i];
            $i++;
        }
    }
    # obtain list of folders included in the backup
    $includeFolder = array();
    #print_r($excluded);exit;
    #$includeFolder = confirmBackup('nohtml');
    #initialise list arrays, directories and files separately and array counters for them
    $d_arr = array();
    $d = 0;
    $ds_arr = array();
    $f_arr = array();
    $f = 0;
    $s_arr = array();
    $s = 0;
    $mdir = 0;
    $perm_file = $_CONFIG['backups_dir'] . "/perm.txt";
    @unlink($perm_file);
    $fperm = fopen($perm_file, "w");
    for ($i = 0; $i < sizeof($excluded); $i++) {
        $excluded[$i] = str_replace("//", "/", $excluded[$i]);
    }
    #print_r($excluded);exit;
    # obtain the list of files by recursing the mambo file store
    recurseFiles($d_arr, $ds_arr, $f_arr, $s_arr, $d, $f, $s, $includeFolder, '', $excluded, $fperm);
    @fclose($fperm);
    @chmod($perm_file, 0777);
    # format total archive size
    $originalSize = getFileSizeText($s);
    # extend the file locations to include the full path
    for ($i = 0; $i < count($f_arr); $i++) {
        if (!file_exists($f_arr[$i])) {
            unset($f_arr[$i]);
        }
    }
    #if((file_exists($perm_file))&&(!in_array($perm_file, $f_arr)))
    # $f_arr[] = $perm_file;
    # add sql file to the backup
    if ($_REQUEST[cron_access]) {
        if (is_array($sql_file)) {
            foreach ($sql_file as $key => $sqlfile) {
                if ($backupDatabase == 1 && !in_array($_CONFIG['backups_dir'] . $sqlfile, $f_arr)) {
                    $f_arr[] = $sqlfile;
                }
            }
        }
    }
    $filename = $_CONFIG['clonerPath'] . '/' . $filename1;
    $f_arr = array_unique($f_arr);
    # create the Zip file from the fileset array
    @unlink($filename);
    $perm_lines = 0;
    $handle = @fopen($perm_file, "r");
    if ($handle) {
        while (!feof($handle)) {
            fgets($handle, 4096);
            $perm_lines++;
        }
        fclose($handle);
    }
    if (!$_CONFIG['mem']) {
        #### START TAR MANUAL MODE ############################################################
        if ($_CONFIG['backup_refresh']) {
            $f_arr = array($_CONFIG['backups_dir'] . "/index.html");
        }
        if ($_CONFIG['backup_refresh']) {
            $tar_type = "tar";
        } else {
            $tar_type = "tgz";
        }
        $tar_object = new Archive_Tar($filename, compression($tar_type));
        #$tar_object->setErrorHandling(PEAR_ERROR_PRINT);  // Optional error handling
        $return = $tar_object->addModify($f_arr, '', $_CONFIG['backup_path']);
        if ($return != 1) {
            echo "Backup failed using tar mode, error code {$return}!";
        }
        if ($_CONFIG['backup_refresh']) {
            echo "Starting the manual backup process!<br />";
            echo "Database backup: " . $databaseResult . "<br /><br />";
            if (file_exists($filename)) {
                echo "Backup {$filename} created, we may continue!<br />";
                #echo "Database backup: ".$databaseResult ."<br />";
                echo "<a href='index2.php?option=com_cloner&lines=" . $perm_lines . "&task=refresh&backup={$filename}&excl_manual={$excl_manual}'>Please click here to continue!</a>";
                return;
            } else {
                E_print("Backup failed, please check your tar server utility support!");
                return;
            }
        }
        #### END TAR MODE ############################################################
    } else {
        #### CREATE BACKUP USING SERVER UTILITIES
        $i = 0;
        chdir($_CONFIG['backup_path']);
        $excl_files = "";
        while ($i < sizeof($excluded)) {
            $file = $excluded[$i];
            $file = str_replace($_CONFIG['backup_path'], "", $file);
            $file = "##" . $file;
            $file = str_replace("##//", "", $file);
            $file = str_replace("##/", "", $file);
            $file = str_replace("##", "", $file);
            $excl_files .= "/" . $file . "* ";
            $i++;
        }
        chdir($_CONFIG['backup_path']);
        $excl_cmd = "";
        if ($fp = fopen($_CONFIG['exfile_tar'], "w")) {
            $i = 0;
            while ($i < sizeof($excluded)) {
                $file = $excluded[$i];
                $file = str_replace($_CONFIG['backup_path'], "", $file);
                $file = "##" . $file;
                $file = str_replace("##//", "", $file);
                $file = str_replace("##/", "", $file);
                $file = str_replace("##", "", $file);
                $excl_cmd .= " --exclude=./" . $file . " ";
                $excl_files = "./" . $file . "\r\n";
                fwrite($fp, $excl_files);
                $i++;
            }
            fclose($fp);
        }
        if ($_CONFIG['backup_refresh']) {
            ########## STARTING THE MANUAL BACKUP ######################################################
            $exclude = $excl_cmd . " -X " . $_CONFIG['exfile_tar'];
            $excl_manual = $_CONFIG['exfile_tar'] . "_manual";
            if ($fp = fopen($excl_manual, "w")) {
                fwrite($fp, $exclude);
                fclose($fp);
            }
            echo "Starting the manual backup process!<br />";
            echo "Database backup: " . $databaseResult . "<br /><br />";
            chdir($_CONFIG['backup_path']);
            #$filename = str_replace(".tgz",".tar",$filename);
            exec($_CONFIG[tarpath] . " {$exclude} -c" . $_CONFIG['tarcompress'] . "vf {$filename} ./administrator/backups/index.html");
            exec($_CONFIG[tarpath] . " -" . $_CONFIG['tarcompress'] . "vf {$filename} --update ./administrator/backups/database-sql.sql");
            exec($_CONFIG[tarpath] . " -" . $_CONFIG['tarcompress'] . "vf {$filename} --update ./administrator/backups/htaccess.txt");
            exec($_CONFIG[tarpath] . " -" . $_CONFIG['tarcompress'] . "vf {$filename} --update ./administrator/backups/perm.txt");
            if (file_exists($filename)) {
                echo "Backup {$filename} created, we may continue!<br />";
                #echo "Database backup: ".$databaseResult."<br />" ;
                echo "<a href='index2.php?option=com_cloner&lines=" . $perm_lines . "&task=refresh&backup={$filename}&excl_manual={$excl_manual}'>Please click here to continue!</a>";
                return;
            } else {
                E_print("Backup failed, please check your tar server utility support!");
                return;
            }
            return;
        }
        if ($_REQUEST[cron_dbonly] != 1) {
            exec($_CONFIG[tarpath] . " {$excl_cmd} " . " -X " . $_CONFIG['exfile_tar'] . "  -chv" . $_CONFIG['tarcompress'] . "f {$filename} ./");
        } else {
            exec($_CONFIG[tarpath] . "  -" . $_CONFIG['tarcompress'] . "cvf {$filename} ./administrator/backups/database-sql.sql");
            if (is_array($databases_incl)) {
                foreach ($databases_incl as $database_name) {
                    if ($database_name != "") {
                        exec($_CONFIG[tarpath] . "  -" . $_CONFIG['tarcompress'] . "vf {$filename} --update  ./administrator/backups/" . $database_name . "-sql.sql");
                    }
                }
            }
        }
        #######END
        chdir($_CONFIG['script_path']);
        #### END
    }
    if (!file_exists($filename)) {
        if ($_CONFIG[archive_type] == 1) {
            echo LM_MSG_BACK_12;
        } else {
            echo LM_MSG_BACK_13;
        }
        return;
    }
    # format the compressed size of the fileset
    $archiveSize = getFileSizeText(filesize($filename));
    # load presentation layer
    if ($option != 'nohtml') {
        HTML_cloner::generateBackup($filename1, $archiveSize, $originalSize, $mdir, $f, $databaseResult, $option);
    } else {
        logxx(HTML_cloner::generateBackup_text($filename1, $archiveSize, $originalSize, $mdir, $f, $databaseResult, $option));
    }
    if (is_array($databases_incl)) {
        foreach ($databases_incl as $database_name) {
            @unlink($_CONFIG['backups_dir'] . "/" . $database_name . "-sql.sql");
        }
    }
    @unlink($_CONFIG['backups_dir'] . "/database-sql.sql");
    @unlink($sql_file);
    @unlink($perm_file);
    @unlink($htaccess);
    @unlink($_CONFIG['exfile']);
    @unlink($_CONFIG['exfile_tar']);
    return $filename1;
}