Current File : /home/inlingua/public_html/icentex/admin_cert/back_up/backup.php
<?php
ini_set("memory_limit","128M");
ini_set("max_execution_time",0);
// Report all errors
error_reporting(0);
include("../../connection.php"); 
/**
 * Define database parameters here
 */
define("DB_USER", $USERNAME);
define("DB_PASSWORD", $PASSWORD);
define("DB_NAME", $DATABASE);
define("DB_HOST", $SERVER);
define("OUTPUT_DIR", 'cache');
define("TABLES",
       'certificate_reasons,
       corporate_email_report,
       course_id,
       ingl_active_sessions,
       ingl_active_workshops,
       ingl_active_workshops_apply,
       ingl_admission,
       ingl_admission_analysis,
       ingl_billing_cat,
       ingl_billing_main,
       ingl_conveyance,
       ingl_conveyance_bands,
       ingl_conveyance_designation,
       ingl_conveyance_home_kms,
       ingl_conveyance_kms,
       ingl_conveyance_main,
       ingl_course,
       ingl_designation,
       ingl_holidays,
       ingl_houses,
       ingl_invoice_detail,
       ingl_invoice_main,
 ingl_invoice_payment,
 ingl_keypoint,
 ingl_modules,
 ingl_payment_gateway,
 ingl_recruitment_source,
 ingl_reschedule,
 ingl_session,
 ingl_student,
 ingl_terms_conditions,
 ingl_test,
 ingl_test_answer,
 ingl_test_category,
 ingl_test_instr,
 ingl_test_oral,
 ingl_user_add_qualification,
 ingl_user_contract_details,
 ingl_user_designation,
 ingl_user_discpline_action,
 ingl_user_document_receive,
 ingl_user_education,
 ingl_user_external_training,
 ingl_user_grievance,
 ingl_user_internal_training,
 ingl_user_timing,
 ingl_user_training_eligible,
 ingl_video_doc,
 ingl_workshops,
 ingl_workshop_doc,
 ingl_workshop_documents,
 ingl_workshop_feedback,
 ingl_workshop_videos,leaves,level_1,level_2,level_3,level_4,level_5,level_private,messages,rates,
     sales_enquiry_source,
     sales_login,
     taxes,
     timesheet,
     timesheet_block,
     timesheet_location,
     timesheet_user,
     trainer_resource_video'
       );
 
/**
 * Instantiate Backup_Database and perform backup
 */
$backupDatabase = new Backup_Database(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$status = $backupDatabase->backupTables(TABLES, OUTPUT_DIR) ? 'OK' : 'KO';
echo "<br /><br /><br />Backup result: ".$status;
 
/**
 * The Backup_Database class
 */
class Backup_Database {
    /**
     * Host where database is located
     */
    var $host = '';
 
    /**
     * Username used to connect to database
     */
    var $username = '';
 
    /**
     * Password used to connect to database
     */
    var $passwd = '';
 
    /**
     * Database to backup
     */
    var $dbName = '';
 
    /**
     * Database charset
     */
    var $charset = '';
 
    /**
     * Constructor initializes database
     */
    function Backup_Database($host, $username, $passwd, $dbName, $charset = 'utf8')
    {
        $this->host     = $host;
        $this->username = $username;
        $this->passwd   = $passwd;
        $this->dbName   = $dbName;
        $this->charset  = $charset;
 
        $this->initializeDatabase();
    }
 
    protected function initializeDatabase()
    {
        $conn = mysql_connect($this->host, $this->username, $this->passwd);
        mysql_select_db($this->dbName, $conn);
        if (! mysql_set_charset ($this->charset, $conn))
        {
            mysqli_query($conn,'SET NAMES '.$this->charset);
        }
    }
 
    /**
     * Backup the whole database or just some tables
     * Use '*' for whole database or 'table1 table2 table3...'
     * @param string $tables
     */
    public function backupTables($tables = '*', $outputDir = '.')
    {
        try
        {
            /**
            * Tables to export
            */
            if($tables == '*')
            {
                $tables = array();
                $result = mysqli_query($conn,'SHOW TABLES');
                while($row = mysql_fetch_row($result))
                {
                    $tables[] = $row[0];
                }
            }
            else
            {
                $tables = is_array($tables) ? $tables : explode(',',$tables);
            }
 
            $sql = 'CREATE DATABASE IF NOT EXISTS '.$this->dbName.";\n\n";
            $sql .= 'USE '.$this->dbName.";\n\n";
 
            /**
            * Iterate tables
            */
            foreach($tables as $table)
            {
                echo "Backing up ".$table." table...";
 
                $result = mysqli_query($conn,'SELECT * FROM '.$table);
                $numFields = mysql_num_fields($result);
 
                $sql .= 'DROP TABLE IF EXISTS '.$table.';';
                $row2 = mysql_fetch_row(mysqli_query($conn,'SHOW CREATE TABLE '.$table));
                $sql.= "\n\n".$row2[1].";\n\n";
 
                for ($i = 0; $i < $numFields; $i++)
                {
                    while($row = mysql_fetch_row($result))
                    {
                        $sql .= 'INSERT INTO '.$table.' VALUES(';
                        for($j=0; $j<$numFields; $j++)
                        {
                            $row[$j] = addslashes($row[$j]);
                            $row[$j] = ereg_replace("\n","\\n",$row[$j]);
                            if (isset($row[$j]))
                            {
                                $sql .= '"'.$row[$j].'"' ;
                            }
                            else
                            {
                                $sql.= '""';
                            }
 
                            if ($j < ($numFields-1))
                            {
                                $sql .= ',';
                            }
                        }
 
                        $sql.= ");\n";
                    }
                }
 
                $sql.="\n\n\n";
 
                echo " OK" . "<br />";
            }
        }
        catch (Exception $e)
        {
            var_dump($e->getMessage());
            return false;
        }
 
        return $this->saveFile($sql, $outputDir);
    }
 
    /**
     * Save SQL to file
     * @param string $sql
     */
    protected function saveFile(&$sql, $outputDir = '.')
    {
        if (!$sql) return false;
 
        try
        {
            $file_save=$outputDir.'/db-backup-'.$this->dbName.'-'.date("Ymd-His", time()).'.sql';
            //$file_save1='db-backup-'.$this->dbName.'-'.date("Ymd-His", time()).'.sql';
            touch($file_save);
            chmod($file_save, 0777);
            $handle = fopen($file_save,'w+');
            
            fwrite($handle, $sql);
            fclose($handle);
            require('zip_fun.php');
            require 'tar.php';
            $files_to_zip[] =$file_save;
            $tar_object = new Archive_Tar("inlingua_back_up.tar");
	    $tar_object->createModify($files_to_zip, "inlingua_back_up");
	    echo"<a href='inlingua_back_up.tar'>Download</a>";		
        }
        catch (Exception $e)
        {
            var_dump($e->getMessage());
            return false;
        }
 
        return true;
    }
}

?>