{"id":53,"date":"2008-04-19T13:21:02","date_gmt":"2008-04-19T18:21:02","guid":{"rendered":"http:\/\/home.iseeancestors.com\/blog\/?p=53"},"modified":"2008-04-19T14:21:56","modified_gmt":"2008-04-19T19:21:56","slug":"apache-log-archives","status":"publish","type":"post","link":"https:\/\/home.iseeancestors.com\/blog\/2008\/04\/19\/apache-log-archives\/","title":{"rendered":"Apache Log Archives"},"content":{"rendered":"<p>If you maintain a website at a shared-hosting provider, you may not have control over how your Apache log files are archived.\u00a0 These archives may also be overwritten, so if you&#8217;re interesting in keeping statistics for later analysis, you&#8217;ll have to archive your log files yourself.\u00a0 Here I provide a simple PHP script you can run to save your own log files.<\/p>\n<p><!--more-->Some hosting providers will archive your log files by copying them to a &#8220;logs&#8221; directory.\u00a0 Under that directory are subdirectories for each of your domains, like www.example.com and www.sample.com. Inside these directories are your archived log files.\u00a0 But, depending on how your provider does it, these files may eventually be overwritten.\u00a0 For example, a\u00a0file naming scheme is:<\/p>\n<p>www.example.com.1<br \/>\nwww.example.com.2.gz<br \/>\nwww.example.com.3.gz<br \/>\n&#8230;<br \/>\nwww.example.com.7.gz<br \/>\nagent_log.1<br \/>\nagent_log.2.gz<br \/>\nagent_log.3.gz<br \/>\n&#8230;<br \/>\nagent_log.7.gz<br \/>\nerror_log.1<br \/>\nerror_log.2.gz<br \/>\nerror_log.3.gz<br \/>\n&#8230;<br \/>\nerror_log.7.gz<br \/>\nreferrer_log.1<br \/>\nreferrer_log.2.gz<br \/>\nreferrer_log.3.gz<br \/>\n&#8230;<br \/>\nreferrer_log.7.gz<\/p>\n<p>About each day, your hosting provider rotates and renames these files so that by week&#8217;s end, the earliest files are lost forever.\u00a0 To prevent this, run the following PHP script daily.\u00a0 Change the $pathroot variable to the document root path used by your own hosting provider.<\/p>\n<p><code>&lt;?php<br \/>\n$pathroot = \"\/your\/document\/root\/path\/\";<br \/>\n$pathsrc = \"logs\/\";<br \/>\n$pathdest = \"logs_archive\/\";<br \/>\n$filesrc = \"*.gz\";<br \/>\n$sites = array(\"www.example.com\",<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"www.sample.com\");<br \/>\nfunction CopyLogFiles($site)<br \/>\n{<br \/>\n\u00a0 global $pathroot, $pathsrc, $pathdest;<br \/>\n\u00a0 $path = $pathroot . $pathsrc . $site;<br \/>\n\u00a0 $directory = dir($path);<br \/>\n\u00a0 $entries = array();<br \/>\n\u00a0 while (false !== ($entry = $directory-&gt;read([\/efn_note])<br \/>\n\u00a0\u00a0\u00a0\u00a0 if (!is_dir($entry[\/efn_note]<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $entries[] = $entry;<br \/>\n\u00a0 foreach($entries AS $entry)<br \/>\n\u00a0 {<br \/>\n\u00a0\u00a0\u00a0\u00a0 if (strpos($entry, \".gz\") !== false)<br \/>\n\u00a0\u00a0\u00a0\u00a0 {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $source\u00a0 = $directory-&gt;path . \"\/\";<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $source\u00a0.= $entry;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $modified = date(\"Ymd-His\",<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0filemtime($source[\/efn_note];<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $entry = preg_replace(\"\/.\\d.\/\", \".\",<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0$entry);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $dest = $pathroot . $pathdest . $site;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $dest .= \"\/\" . $modified . \"-\";<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $dest\u00a0.= $entry;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 copy($source, $dest);<br \/>\n\u00a0\u00a0\u00a0\u00a0 }<br \/>\n\u00a0 }<br \/>\n\u00a0 $directory-&gt;close();<br \/>\n}<br \/>\nforeach($sites AS $site)<br \/>\n\u00a0 CopyLogFiles($site);<br \/>\n?&gt;\u00a0<\/code><\/p>\n<p>This script copies archived log files from your &#8220;log&#8221; directory to your own &#8220;logs_archive&#8221; directory.\u00a0 It names each log file with the date and time it was last modified and removes the .1, .2, .3 numbering scheme.\u00a0 Since the original .1 file may be renamed to .2 on the following day, the script will overwrite the same file in your logs_archive directory.\u00a0 Since files are copied and renamed using the date and time they where last modified, this overwrite is harmless.<\/p>\n<p>Now your logs_archive directory will accumulate your log files.\u00a0 Instead of www.example.com.2.gz however, the files will be named like 20080419-152034-www.example.com.gz.\u00a0 In this example the file was\u00a0last modified on April 19, 2008 at 15:20:34.\u00a0 The next day when your hosting provider renames www.example.com.2.gz\u00a0to www.example.com.3.gz, the script will copy www.example.com.3.gz\u00a0to 20080419-152034-www.example.com.gz again because it has the same modification date and time.<\/p>\n<p>To run the script daily, set up a crontab job. For instance you could run it every day at 20 minutes past midnight: 20, 0, *, *, *, \/usr\/bin\/php savelogs.php<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you maintain a website at a shared-hosting provider, you may not have control over how your Apache log files are archived.\u00a0 These archives may also be overwritten, so if you&#8217;re interesting in keeping statistics for later analysis, you&#8217;ll have to archive your log files yourself.\u00a0 Here I provide a simple PHP script you can &hellip; <a href=\"https:\/\/home.iseeancestors.com\/blog\/2008\/04\/19\/apache-log-archives\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Apache Log Archives&#8221;<\/span><\/a><\/p>\n","protected":false},"author":65,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-53","post","type-post","status-publish","format-standard","hentry","category-software"],"_links":{"self":[{"href":"https:\/\/home.iseeancestors.com\/blog\/wp-json\/wp\/v2\/posts\/53","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/home.iseeancestors.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/home.iseeancestors.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/home.iseeancestors.com\/blog\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/home.iseeancestors.com\/blog\/wp-json\/wp\/v2\/comments?post=53"}],"version-history":[{"count":0,"href":"https:\/\/home.iseeancestors.com\/blog\/wp-json\/wp\/v2\/posts\/53\/revisions"}],"wp:attachment":[{"href":"https:\/\/home.iseeancestors.com\/blog\/wp-json\/wp\/v2\/media?parent=53"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/home.iseeancestors.com\/blog\/wp-json\/wp\/v2\/categories?post=53"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/home.iseeancestors.com\/blog\/wp-json\/wp\/v2\/tags?post=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}