Exemple #1
0
function setBackup()
{
    global $dbname, $dbh;
    global $PARAM, $SUBS, $MSG, $MONTHS;
    if (!is_dir(getAdmSetting('BACKUP_DIR'))) {
        MkDir(getAdmSetting('BACKUP_DIR'), 0777);
    }
    if ($PARAM['upload'] == 1) {
        global $bckFile, $bckFile_name;
        if ($bckFile_name == '') {
            $SUBS['ERROR'] = $MSG[20108];
            $SUBS['BACKUP_ERROR'] = fileParse('_admin_error.htmlt');
        } else {
            if (!($UPLOAD = @file($bckFile))) {
                setLogAndStatus("Reading", $bckFile, 0, "setBackup()", 'READ_UPLOAD');
            }
            $file = date('d F Y H_i_s');
            $filename = getAdmSetting('BACKUP_DIR') . "/{$file}.sql";
            $upload = '## ' . $MSG[20109] . date(' d F Y H:i:s') . "\n";
            $upload .= "## {$MSG['20110']} {$bckFile_name}\n";
            $upload .= join('', $UPLOAD);
            if (!($fp = fopen($filename, 'w'))) {
                setLogAndStatus("Opening", $filename, 0, "setBackup()", 'OPEN_FILE');
            }
            fwrite($fp, $upload);
            fclose($fp);
            $SUBS['COMMAND'] = $PARAM['cmd'] . "&err=20050";
            printPage('_admin_done.htmlt');
            return;
        }
    }
    //export database backup
    if ($PARAM['export'] == 1) {
        $file = date('d F Y H_i_s');
        $filename = getAdmSetting('BACKUP_DIR') . "/{$file}.sql";
        if (!($fp = fopen($filename, 'w'))) {
            setLogAndStatus("Opening", 0, $filename, "setBackup()", 'OPEN_FILE');
        }
        //write comments if any
        if ($PARAM['bckComments'] != '') {
            $comments = '##' . ereg_replace("\n", "\n##", $PARAM['bckComments']) . "\n";
            fwrite($fp, $comments);
        }
        if (!($res = db_list_tables($dbname, $dbh))) {
            setLogAndStatus("db_list_tables()", 0, $dbname, "setBackup()", 'LIST_TABLES');
        }
        $num_tables = db_num_rows($res);
        $i = 0;
        while ($i < $num_tables) {
            $table = db_tablename($res, $i);
            $fields = db_list_fields($dbname, $table, $dbh);
            $columns = db_num_fields($fields);
            $tablelist = '';
            for ($j = 0; $j < $columns; $j++) {
                if ($columns - $j == 1) {
                    $tablelist .= db_field_name($fields, $j);
                } else {
                    $tablelist .= db_field_name($fields, $j) . ',';
                }
            }
            $schema = "REPLACE INTO {$table} ({$tablelist}) VALUES (";
            $query = "SELECT * FROM {$dbname}.{$table}";
            $result = runQuery($query, 'setBackup()', 'SELECT_TABLES');
            while ($row = db_fetch_row($result)) {
                $schema_insert = '';
                for ($j = 0; $j < $columns; $j++) {
                    if (!isset($row[$j])) {
                        $schema_insert .= ' NULL,';
                    } else {
                        $schema_insert .= ' ' . dbQuote($row[$j]) . ',';
                    }
                }
                $schema_insert = $schema . ereg_replace(',$', '', $schema_insert);
                $schema_insert .= ");\r\n";
                fwrite($fp, $schema_insert);
            }
            $i++;
        }
        fclose($fp);
        // the ZIP thing --------------------
        $fp = fopen($filename, "rb");
        $data = fread($fp, filesize($filename));
        fclose($fp);
        $name = array(baseName($filename));
        $data = array($data);
        $content = makezip($name, $data);
        $fp = fopen('./zip/' . basename($filename) . '.ZIP', "wb");
        fputs($fp, $content);
        fclose($fp);
        // the ZIP thing --------------------
        $SUBS['COMMAND'] = $PARAM['cmd'] . "&err=20052";
        printPage('_admin_done.htmlt');
        return;
    }
    //prepare for import or delete
    $backups = opendir(getAdmSetting('BACKUP_DIR'));
    while (($file = readdir($backups)) != false) {
        if (!is_dir($file)) {
            $BCKUPS[eregi_replace('[^a-z0-9]', '_', $file)] = getAdmSetting('BACKUP_DIR') . "/{$file}";
        }
    }
    closedir($backups);
    reset($PARAM);
    while (list($k, $v) = each($PARAM)) {
        if (ereg('^bck_(.*)$', $k, $R)) {
            $BACKUPS[] = $R[1];
        }
    }
    reset($PARAM);
    //delete backups
    if ($PARAM['delete'] == 1) {
        if (count($BACKUPS) == 0) {
            $SUBS['COMMAND'] = $PARAM['cmd'] . "&err=20008";
            printPage('_admin_done.htmlt');
            return;
        }
        for ($i = 0; $i < count($BACKUPS); $i++) {
            if (!@unlink($BCKUPS[$BACKUPS[$i]])) {
                setLogAndStatus("Deleting", $BCKUPS[$BACKUPS[$i]], "setBackup()", 'DEL_BACKUP');
            }
        }
        $SUBS['COMMAND'] = $PARAM['cmd'] . "&err=20054";
        printPage('_admin_done.htmlt');
        return;
    }
    //import database backup
    if ($PARAM['import'] == 1) {
        if (count($BACKUPS) > 1) {
            $SUBS['COMMAND'] = $PARAM['cmd'] . "&err=20053";
            printPage('_admin_done.htmlt');
            return;
        }
        if (count($BACKUPS) == 0) {
            $SUBS['COMMAND'] = $PARAM['cmd'] . "&err=20008";
            printPage('_admin_done.htmlt');
            return;
        }
        //get backup file
        $file = fread(fopen($BCKUPS[$BACKUPS[0]], 'r'), filesize($BCKUPS[$BACKUPS[0]]));
        ////---- [Mrasnika's] Edition 21.03.2002
        split_sql_file($BACKUP, $file);
        //reset tables
        if (!($res = db_list_tables($dbname, $dbh))) {
            setLogAndStatus("db_list_tables()", 1, $dbname, "databaseBackup()", 'LIST_TABLES_2');
        }
        $num_tables = db_num_rows($res);
        $i = 0;
        while ($i < $num_tables) {
            $table = db_tablename($res, $i);
            $query = "DELETE FROM {$dbname}.{$table}";
            $result = runQuery($query, 'setBackup()', 'RESET_TABLES');
            $i++;
        }
        //fill tables
        while (list($k, $query) = each($BACKUP)) {
            if (!ereg('^#', $query)) {
                if (!($result = db_query($query, $dbh))) {
                    setLogAndStatus($query, db_errno($dbh), db_error($dbh), "databaseBackup()", 'RESTORE_DB');
                    $SUBS['COMMAND'] = $PARAM['cmd'] . "&err=20055";
                    printPage('_admin_done.htmlt');
                    return;
                }
            }
        }
        $SUBS['COMMAND'] = $PARAM['cmd'] . "&err=20056";
        printPage('_admin_done.htmlt');
        return;
    }
    $backups = opendir(getAdmSetting('BACKUP_DIR'));
    $last = 0;
    while (($file = readdir($backups)) != false) {
        if (!is_dir($file)) {
            $date = stat(getAdmSetting('BACKUP_DIR') . "/{$file}");
            if ($last < $date[9]) {
                $month = intval(date('m'));
                $SUBS['LAST'] = $MSG[20051] . date(' d ', $date[9]) . $MONTHS[$month] . date(' Y H.i.s', $date[9]);
            }
            $SUBS['SIZE'] = sprintf('%0.2f KB', $date[7] / 1024);
            $SUBS['NAME'] = eregi_replace('_', ':', $file);
            $SUBS['CHECK'] = eregi_replace('[^a-z0-9]', '_', $file);
            //checkbox name
            $SUBS['WHERE'] = getAdmSetting('BACKUP_DIR') . "/{$file}";
            if (!($BACKUP = @file(getAdmSetting('BACKUP_DIR') . "/{$file}"))) {
                setLogAndStatus("Reading", 0, getAdmSetting('BACKUP_DIR') . "/{$file}", "setBackup()", 'READ_FILE');
            }
            $comments = '';
            //get comments from the beginning of the file
            for ($i = 0; $i < count($BACKUP); $i++) {
                if (eregi('^##(.*)$', $BACKUP[$i], $R)) {
                    $comments .= $R[1];
                }
            }
            if ($comments != '') {
                $SUBS['COMMENTS'] = ' &nbsp; ' . ereg_replace("\n", '<BR> &nbsp; ', htmlEncode($comments));
                $SUBS['COMMENTS'] = ereg_replace('<BR> &nbsp; $', '', $SUBS['COMMENTS']);
            } else {
                $SUBS['COMMENTS'] = '';
            }
            $SUBS['BACKUPS'] .= fileParse('_admin_backup_row.htmlt');
        }
    }
    closedir($backups);
    if ($PARAM['err'] != '') {
        $SUBS['ERROR'] = $MSG[$PARAM['err']];
        $SUBS['BACKUP_ERROR'] = fileParse('_admin_error.htmlt');
    }
    printPage('_admin_backup.htmlt');
}
	<table width="100%" border="0" cellspacing="2" cellpadding="3" class="text">
		<tr>
			<td align="center" colspan="2">Choose operation and table:</td>
		</tr>
		<tr class="table">
			<td align="right" width="50%">
				<select name="tbl_op">
					<option value="2">Backup structure and content</option>
					<option value="0">Backup structure only</option>
					<option value="1">Backup content only</option>
				</select>
			</td>
			<td align="left" width="50%">
				<select name="tbl">
				<?php 
$tbls = db_list_tables();
##Draw aviable tables in Database
while ($tbl = mysql_fetch_row($tbls)) {
    echo "<option value=\"{$tbl['0']}\">{$tbl['0']}</option>";
}
?>
	
				</select>
			</td>
		</tr>
		<tr class="table">
			<td colspan="2" align="center" width="50%">
				<input type="radio" name="savetype" value="server" id="table_savetype_server" checked="checked" style="vertical-align: middle" /><label for="table_savetype_server">Save to server</label>&nbsp;
				<input type="radio" name="savetype" value="client" id="table_savetype_client" style="vertical-align: middle" /><label for="table_savetype_client">Save to your PC</label>&nbsp;
				<input type="radio" name="savetype" value="show" id="table_savetype_show" style="vertical-align: middle" /><label for="table_savetype_show">Show on the screen</label>
			</td>
Exemple #3
0
function getTablesBackupTools($status_text)
{
    $sChooseOperationC = _t('_adm_dbtools_Choose_operation_and_table');
    $sBackup1C = _t('_adm_dbtools_Backup_structure_content');
    $sBackup2C = _t('_adm_dbtools_Backup_structure');
    $sBackup3C = _t('_adm_dbtools_Backup_content');
    $sSaveOption1C = _t('_adm_dbtools_Save_to_server');
    $sSaveOption2C = _t('_adm_dbtools_Save_to_your_PC');
    $sSaveOption3C = _t('_adm_dbtools_Show_on_the_screen');
    $sBackupTableC = _t('_adm_dbtools_Backup_table');
    $sActionC = _t('_Action');
    // All tables of Database
    $aTablesOptions = array();
    $tbls = db_list_tables();
    while ($tbl = mysql_fetch_array($tbls)) {
        $aTablesOptions[$tbl['0']] = $tbl['0'];
    }
    $sStatusText = ($status_text and isset($_POST['TablesBackup'])) ? $status_text : '';
    $aForm = array('form_attrs' => array('name' => 'TablesBackupTools_form', 'action' => $GLOBALS['site']['url_admin'] . 'db.php', 'method' => 'post'), 'inputs' => array('TablesBackup' => array('type' => 'hidden', 'name' => 'TablesBackup', 'value' => 'YES'), 'tbl_op' => array('type' => 'select', 'name' => 'tbl_op', 'caption' => $sChooseOperationC, 'values' => array(2 => $sBackup1C, 0 => $sBackup2C, 1 => $sBackup3C)), 'tbl' => array('type' => 'select', 'name' => 'tbl', 'caption' => '', 'values' => $aTablesOptions), 'savetype' => array('type' => 'radio_set', 'name' => 'savetype', 'caption' => $sActionC, 'values' => array('server' => $sSaveOption1C, 'client' => $sSaveOption2C, 'show' => $sSaveOption3C)), 'tables_backup_tool' => array('type' => 'submit', 'name' => 'tables_backup_tool', 'caption' => '', 'value' => $sBackupTableC)));
    $oForm = new BxTemplFormView($aForm);
    return $GLOBALS['oAdmTemplate']->parseHtmlByName('design_box_content.html', array('content' => $oForm->getCode() . $sStatusText));
    //return DesignBoxContent(_t('_adm_dbtools_Tables_backup_tools'), '<div style="margin:9px;">' . $oForm->getCode() . '</div>' . $sStatusText, 1);
}
/**
 * do daily backup
 *
 * @param null
 * @return null
 */
function backup_handle_on_daily()
{
    set_time_limit(0);
    $start_time = time();
    if (!ConfigOptions::getValue('backup_enabled')) {
        return true;
    }
    // if
    // check if backup path exists and if it's writable
    recursive_mkdir(BACKUP_PATH, 0777, WORK_PATH);
    if (!is_dir(BACKUP_PATH) || !folder_is_writable(BACKUP_PATH)) {
        backup_module_log_error('Backup path (' . BACKUP_PATH . ') does not exists or it is not writable', BACKUP_MODULE_SEND_ERROR_EMAIL);
        return false;
    }
    // if
    $htaccess = BACKUP_PATH . '/.htaccess';
    if (!is_file($htaccess)) {
        file_put_contents($htaccess, 'Deny from all');
    }
    $folder_name = "backup " . date('Y-m-d H-i') . " GMT";
    $backup_dir = BACKUP_PATH . '/' . $folder_name;
    // check if backup already exists
    if (is_dir($backup_dir)) {
        backup_module_log_error("Backup already exists ({$folder_name})", BACKUP_MODULE_SEND_ERROR_EMAIL);
        return false;
    }
    // if
    // try to create backup directory
    if (!recursive_mkdir($backup_dir, 0777, WORK_PATH)) {
        backup_module_log_error("Could not create backup folder ({$backup_dir})", BACKUP_MODULE_SEND_ERROR_EMAIL);
        return false;
    }
    // if
    chmod($backup_dir, 0777);
    // backup database (all tables that starts with TABLE_PREFIX)
    $tables = db_list_tables(TABLE_PREFIX);
    if (is_foreachable($tables)) {
        $result = db_dump_tables($tables, $backup_dir . '/database.sql');
        if (is_error($result)) {
            safe_delete_dir($backup_dir, BACKUP_PATH);
            backup_module_log_error($result->getMessage(), BACKUP_MODULE_SEND_ERROR_EMAIL);
            return false;
        }
        // if
    } else {
        safe_delete_dir($backup_dir, BACKUP_PATH);
        backup_module_log_error("Database specified in config.php file does not have exportable tables. Check your config settings", BACKUP_MODULE_SEND_ERROR_EMAIL);
        return false;
    }
    // backup uploads
    $errors = array();
    $result = backup_module_copy_dir(UPLOAD_PATH, $backup_dir . '/upload', true, $errors);
    if (!$result) {
        safe_delete_dir($backup_dir, BACKUP_PATH);
        backup_module_log_error($errors, BACKUP_MODULE_SEND_ERROR_EMAIL);
        return false;
    }
    // if
    // backup project icons
    $errors = array();
    $result = backup_module_copy_dir(PUBLIC_PATH . '/projects_icons', $backup_dir . '/projects_icons', true, $errors);
    if (!$result) {
        safe_delete_dir($backup_dir, BACKUP_PATH);
        backup_module_log_error($errors, BACKUP_MODULE_SEND_ERROR_EMAIL);
        return false;
    }
    // if
    // backup avatars
    $errors = array();
    $result = backup_module_copy_dir(PUBLIC_PATH . '/avatars', $backup_dir . '/avatars', true, $errors);
    if (!$result) {
        safe_delete_dir($backup_dir, BACKUP_PATH);
        backup_module_log_error($errors, BACKUP_MODULE_SEND_ERROR_EMAIL);
        return false;
    }
    // if
    // backup logos
    $errors = array();
    $result = backup_module_copy_dir(PUBLIC_PATH . '/logos', $backup_dir . '/logos', true, $errors);
    if (!$result) {
        safe_delete_dir($backup_dir, BACKUP_PATH);
        backup_module_log_error($errors, BACKUP_MODULE_SEND_ERROR_EMAIL);
        return false;
    }
    // if
    $app =& application();
    $checksum = backup_module_calculate_checksum($folder_name);
    $backup_note = "<?php \n/* \n";
    $backup_note .= 'Backup is created with activeCollab v' . $app->version . ' on ' . date(DATETIME_MYSQL, $start_time) . "\n\n";
    $backup_note .= "To restore system using this backup, visit this page: \n" . ROOT_URL . '/restore.php?backup=' . urlencode($folder_name) . '&checksum=' . $checksum;
    $backup_note .= "\n*/\n?>";
    if (!file_put_contents($backup_dir . '/restore_instructions.php', $backup_note)) {
        safe_delete_dir($backup_dir, BACKUP_PATH);
        backup_module_log_error("Could not create restore instructions for backup.", BACKUP_MODULE_SEND_ERROR_EMAIL);
        return false;
    }
    // if
    // remove old backups
    $how_many_backups = ConfigOptions::getValue('backup_how_many_backups');
    $how_many_backups = (int) $how_many_backups <= 0 ? 5 : $how_many_backups;
    $folders_in_backup_directory = backup_module_get_backups(BACKUP_PATH);
    if (count($folders_in_backup_directory) > $how_many_backups) {
        $old_backups = array_splice($folders_in_backup_directory, -(count($folders_in_backup_directory) - $how_many_backups));
        foreach ($old_backups as $old_backup) {
            safe_delete_dir($old_backup['path'], BACKUP_PATH);
        }
        // foreach
    }
    // if
    log_message('Daily backup created', LOG_LEVEL_INFO, 'backup');
}
Exemple #5
0
    // #
    // ##
    // ### start backup
    /*
    phpMyBackup v.0.4 Beta - Documentation
        Homepage: http://www.nm-service.de/phpmybackup
        Copyright (c) 2000-2001 by Holger Mauermann, mauermann@nm-service.de
    phpMyBackup is distributed in the hope that it will be useful for you, but
        WITHOUT ANY WARRANTY. This programm may be used freely as long as all credit
        and copyright information are left intact.
    */
    if ($writer->countWriters()) {
        $version = "0.4 beta";
        $cur_time = date("Y-m-d H:i");
        $writer->write("-- Dump created with 'phpMyBackup v." . $version . "' on " . $cur_time . "\r\n");
        $tables = db_list_tables(DBDATE);
        $num_tables = @db_num_rows($tables);
        $i = 0;
        while ($i < $num_tables) {
            $table = db_tablename($tables, $i);
            if (isset($_POST['prefix']) and strpos($table, DBPREF) === false) {
                $i++;
                continue;
            }
            get_def(DBDATE, $table, $writer);
            get_content(DBDATE, $table, $writer);
            $i++;
        }
        $writer->close();
    }
} else {