Magento清空数据库log和var缓存文件脚本

magento清空log与var脚本
magento清空log与var脚本

Magento的数据库,用久了,很多log表会变的非常巨大,数据库体积也越来越大,这会严重影响到数据库的运行。

此脚本可以自动清空数据库log表。如果你的log表有用,可以按照实际情况修改此脚本。

将文件上传至你的magento文件根目录,然后用如下地址执行清空命令:

清空数据库log表:http://你网站的网址/clear-magento.php?clean=log

清空magento缓存文件:http://你网站的网址/clear-magento.php?clean=var

如果你的数据库log表比较大,那么使用如上命令清log可能会失败,或者用去很长时间,你可以手动清空,在脚本代码中的log表有:

‘dataflow_batch_export’,
‘dataflow_batch_import’,
‘log_customer’,
‘log_quote’,
‘log_summary’,
‘log_summary_type’,
‘log_url’,
‘log_url_info’,
‘log_visitor’,
‘log_visitor_info’,
‘log_visitor_online’,
‘index_event’,
‘report_event’,
‘report_compared_product_index’,
‘report_viewed_product_index’,
‘catalog_compare_item’,
‘catalogindex_aggregation’,
‘catalogindex_aggregation_tag’,
‘catalogindex_aggregation_to_tag’,
‘catalogsearch_query’,
‘catalogsearch_fulltext’,
‘catalogsearch_result’

清空var缓存文件一般不会花费太多时间,缓存文件在脚本中路径分别为:

‘downloader/.cache/*’,
‘downloader/pearlib/cache/*’,
‘downloader/pearlib/download/*’,
‘var/cache/’,
‘var/locks/’,
‘var/log/’,
‘var/report/’,
‘var/session/’,
‘var/tmp/’

你也可以手动去清除。

 

脚本内容如下,请自己复制黏贴到 clear-magento.php 文件中:

<?php
$xml = simplexml_load_file('./app/etc/local.xml', NULL, LIBXML_NOCDATA);

$db['host'] = $xml->global->resources->default_setup->connection->host;
$db['name'] = $xml->global->resources->default_setup->connection->dbname;
$db['user'] = $xml->global->resources->default_setup->connection->username;
$db['pass'] = $xml->global->resources->default_setup->connection->password;
$db['pref'] = $xml->global->resources->db->table_prefix;

if($_GET['clean'] == 'log') clean_log_tables();
if($_GET['clean'] == 'var') clean_var_directory();

function clean_log_tables() {
	global $db;

	$tables = array(
		'dataflow_batch_export',
		'dataflow_batch_import',
		'log_customer',
		'log_quote',
		'log_summary',
		'log_summary_type',
		'log_url',
		'log_url_info',
		'log_visitor',
		'log_visitor_info',
		'log_visitor_online',
		'index_event',
		'report_event',
		'report_compared_product_index',
		'report_viewed_product_index',
		'catalog_compare_item',
		'catalogindex_aggregation',
		'catalogindex_aggregation_tag',
		'catalogindex_aggregation_to_tag',
		'catalogsearch_query',
		'catalogsearch_fulltext',
		'catalogsearch_result'
	);

	mysql_connect($db['host'], $db['user'], $db['pass']) or die(mysql_error());
	mysql_select_db($db['name']) or die(mysql_error());

	foreach($tables as $v => $k) {
		@mysql_query('TRUNCATE `'.$db['pref'].$k.'`');
		echo $db['pref'].$k.' cleared<br />';
	}
}

function clean_var_directory() {
	$dirs = array(
		'downloader/.cache/*',
		'downloader/pearlib/cache/*',
		'downloader/pearlib/download/*',
		'var/cache/',
		'var/locks/',
		'var/log/',
		'var/report/',
		'var/session/',
		'var/tmp/'
	);

	foreach($dirs as $v => $k) {
		exec('rm -rf '.$k);
		echo $k.' cleared<br />';
	}
}

此代码来自互联网,稍作修改,原处已经无法考证,可参考:http://www.crucialwebhost.com/kb/magneto-log-and-cache-maintenance-script/
转载表明出处:www.hellokeykey.com

《Magento清空数据库log和var缓存文件脚本》有一个想法

评论已关闭。