Maintenance Script

Last modified by callmedpit on Wed, August 11, 2010 18:35
Source|Old Revisions  |  Back To Group

NOTE: For the most recent tutorial, please refer to the following: http://www.crucialwebhost.com/kb/article/log-cache-maintenance-script/

Create a file called cleanup.php and add the following code to it:

  1. <?php
  2. $xml = simplexml_load_file('./app/etc/local.xml', NULL, LIBXML_NOCDATA);
  3.  
  4. $db['host'] = $xml->global->resources->default_setup->connection->host;
  5. $db['name'] = $xml->global->resources->default_setup->connection->dbname;
  6. $db['user'] = $xml->global->resources->default_setup->connection->username;
  7. $db['pass'] = $xml->global->resources->default_setup->connection->password;
  8. $db['pref'] = $xml->global->resources->db->table_prefix;
  9.  
  10. if($_GET['clean'] == 'log') clean_log_tables();
  11. if($_GET['clean'] == 'var') clean_var_directory();
  12.  
  13. function clean_log_tables() {
  14.     global $db;
  15.    
  16.     $tables = array(
  17.         'dataflow_batch_export',
  18.         'dataflow_batch_import',
  19.         'log_customer',
  20.         'log_quote',
  21.         'log_summary',
  22.         'log_summary_type',
  23.         'log_url',
  24.         'log_url_info',
  25.         'log_visitor',
  26.         'log_visitor_info',
  27.         'log_visitor_online',
  28.         'report_event'
  29.     );
  30.    
  31.     mysql_connect($db['host'], $db['user'], $db['pass']) or die(mysql_error());
  32.     mysql_select_db($db['name']) or die(mysql_error());
  33.    
  34.     foreach($tables as $v => $k) {
  35.         mysql_query('TRUNCATE `'.$db['pref'].$k.'`') or die(mysql_error());
  36.     }
  37. }
  38.  
  39. function clean_var_directory() {
  40.     $dirs = array(
  41.         'downloader/pearlib/cache/*',
  42.         'downloader/pearlib/download/*',
  43.         'var/cache/',
  44.         'var/log/',
  45.         'var/report/',
  46.         'var/session/',
  47.         'var/tmp/'
  48.     );
  49.    
  50.     foreach($dirs as $v => $k) {
  51.         exec('rm -rf '.$k);
  52.     }
  53. }
  54. ?>

Save the file to the directory where Magento resides.

Next, open up cPanel and click on the Cron Jobs icon.

Click on the Standard button.

For the Command to run, enter the following line of code, making sure to replace the domain name with your own:

curl -s -o /dev/null http://www.domain.com/cleanup.php?clean=log

I would recommend leaving the time settings as is, so your log tables will be cleared out daily at 3 AM.

Once you have this set at your preferred interval, click the Save Crontab button.

If you want to have your var directory cleared out automatically, add the following line of code to a new entry:

curl -s -o /dev/null http://www.domain.com/cleanup.php?clean=var

It would be best to clean this out every 2-4 weeks, so set the Day(s) column to 14 or 30. Click the Save Crontab button once you’re done.

This script may not work in all hosting environments, and I take no responsibility if you fubar your site up!




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs