<p>We recently lost a database and I want to recover the data from de Production.log.</p>

<p>Every request is logged like this:</p>

<p>Processing ChamadosController#create (for XXX.XXX.XXX.40 at 2008-07-30 11:07:30) [POST]
  Session ID: 74c865cefa0fdd96b4e4422497b828f9
  Parameters: {"commit"=>"Gravar", "action"=>"create", "funcionario"=>"6" ... (all other parameters go here).</p>

<p>But some stuff to post on de database were in the session. In the request I have the Session ID, and I also have all the session files from the server.</p>

<p>Is there anyway I can, from this Session ID, open de session file and get it's contents?</p>

[{'answer_id': 41629, 'author': 'Jordi Bunster', 'author_id': 4272, 'author_profile': 'https://Stackoverflow.com/users/4272', 'pm_score': 2, 'selected': False, 'text': '<p>Have you tried using <a href="http://corelib.rubyonrails.org/classes/Marshal.html" rel="nofollow noreferrer">Marshal#load</a>? I\'m not sure how you\'re generating those session files, but it\'s quite possible Rails just uses Marshal.</p>\n'}, {'answer_id': 7816035, 'author': 'Tilo', 'author_id': 677684, 'author_profile': 'https://Stackoverflow.com/users/677684', 'pm_score': 2, 'selected': False, 'text': '<p>It\'s probably best to load the session file into a hash -- using the session-id as the key -- and then go through all the log files in chronological order, and parse out the relevant info for each session, and modify your database with it.</p>\n\n<ul>\n<li><p>I guess you\'re starting out with an old database backup? Make sure to do this in a separate Rails environment -- e.g. don\'t do this in production; create and use a separate "recovery" environment / DB.</p></li>\n<li><p>think about some sanity checks you can run on the database afterwards, to make sure that the state of the records makes sense</p></li>\n</ul>\n\n<p>Going forward:</p>\n\n<ul>\n<li><p>make sure that you do regular backups going forward (e.g. with mysqldump if you use MySQL).</p></li>\n<li><p>make sure to set up your database for master/slave replication</p></li>\n</ul>\n\n<p>hope this helps -- good luck!</p>\n'}, {'answer_id': 7872075, 'author': 'parasew', 'author_id': 298982, 'author_profile': 'https://Stackoverflow.com/users/298982', 'pm_score': 2, 'selected': False, 'text': '<p>A client exactly had the same problem a few weeks ago. I came up with the following solution:</p>\n\n<ol>\n<li>play back the latest backup you have (in our case it was one year\nold)</li>\n<li>write a small parser that moves all the requests from production in a temporary database (i chose mongodb for that): i used a rake task and "eval" to create the hash.</li>\n<li>play back the data in the following order\n<ol>\n<li>play in the first create of an object, if it does not already exist.</li>\n<li>find the last update (by date) and play it back.</li>\n</ol></li>\n</ol>\n\n<p>here is the regex for scanning the production.log:</p>\n\n<pre><code>file = File.open("location_of_your_production.log", "rb")\ncontents = file.read\ncontents.scan(/(Started POST \\"(.*?)\\" for (.*?) at (.*?)\\n.*?Parameters: \\{(.*?)\\}\\n.*?Completed (.*?) in (.*?)ms)/m).each do |x|\n  # now you can collect all the important data.\n  # do the same for GET requests as well, if you need it.\nend\n</code></pre>\n\n<p>In my case, the temporary database speeded up the process of the logfile parsing, so the above noted steps could be taken. Of course, everything that was not sent over production.log will be lost. Also, updates of the objects would send the whole information, it might be different in your case. I could also recreate the image uploads, since the images were sent base64 encoded in the production.log.</p>\n\n<p>good luck!</p>\n'}]
['https://Stackoverflow.com/questions/41498', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4384/']