Log & Cache Maintenance Script


/ Published in: PHP
Save to your folder(s)

Save the file to the directory where Magento resides.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. # To use run with a url query string of clean=var (files) or clean=log (db tables) to clean.
  4. $xml = simplexml_load_file('./app/etc/local.xml', NULL, LIBXML_NOCDATA);
  5.  
  6. $db['host'] = $xml->global->resources->default_setup->connection->host;
  7. $db['name'] = $xml->global->resources->default_setup->connection->dbname;
  8. $db['user'] = $xml->global->resources->default_setup->connection->username;
  9. $db['pass'] = $xml->global->resources->default_setup->connection->password;
  10. $db['pref'] = $xml->global->resources->db->table_prefix;
  11.  
  12. if($_GET['clean'] == 'log') clean_log_tables();
  13. if($_GET['clean'] == 'var') clean_var_directory();
  14.  
  15. if(empty($_GET)) echo "To use run with a url query string of clean=var (files) or clean=log (db tables) to clean.";
  16.  
  17. function clean_log_tables() {
  18. global $db;
  19.  
  20. $tables = array(
  21. 'dataflow_batch_export',
  22. 'dataflow_batch_import',
  23. 'log_customer',
  24. 'log_quote',
  25. 'log_summary',
  26. 'log_summary_type',
  27. 'log_url',
  28. 'log_url_info',
  29. 'log_visitor',
  30. 'log_visitor_info',
  31. 'log_visitor_online',
  32. 'report_event'
  33. );
  34.  
  35. mysql_connect($db['host'], $db['user'], $db['pass']) or die(mysql_error());
  36. mysql_select_db($db['name']) or die(mysql_error());
  37.  
  38. foreach($tables as $v => $k) {
  39. mysql_query('TRUNCATE `'.$db['pref'].$k.'`') or die(mysql_error());
  40. }
  41.  
  42. echo "All log tables have been cleaned.";
  43. }
  44.  
  45. function clean_var_directory() {
  46. $dirs = array(
  47. 'downloader/pearlib/cache/*',
  48. 'downloader/pearlib/download/*',
  49. 'var/cache/',
  50. 'var/log/',
  51. 'var/report/',
  52. 'var/session/',
  53. 'var/tmp/'
  54. );
  55.  
  56. foreach($dirs as $v => $k) {
  57. exec('rm -rf '.$k);
  58. }
  59.  
  60. echo "All log files have been cleaned.";
  61. }
  62. ?>

URL: http://www.crucialwebhost.com/kb/article/log-cache-maintenance-script/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.