Maintenance Script

Last modified by Crucial on Fri, October 2, 2009 14:05
Source|Old Revisions  |  Back To Group

This is an old revision of the document!


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

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

Adjust the database variables at the top accordingly.

If your tables have a prefix, don’t forget the trailing underscore!

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