Rails - restoring a database from Production.log

We recently lost a database, and I want to recover data from the Production.log file.

Each request is logged as follows:

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

But some materials for publication in the database were in session. In the request, I have a session identifier, and I also have all the session files from the server.

In any case, can I open a session file from this session ID and get its contents?

+4
source share
3 answers

It is probably best to load the session file into a hash - using the session identifier as the key - and then view all the log files in chronological order and analyze the relevant information for each session and change your database with it.

  • I think you are starting with an old database backup? Be sure to do this in a separate Rails environment. do not do this in production; Create and use a separate recovery environment / DB.

  • think of some sanity checks that you can run in the database afterwards to make sure the state of the records makes sense

Go forward:

  • make sure you make regular backups in the future (for example, using mysqldump if you are using MySQL).

  • be sure to configure your database for master / slave replication

hope this helps - good luck!

+3
source

Have you tried using Marshal # load ? I'm not sure how you create these session files, but it is entirely possible that Rails just uses marshal.

+2
source

The client definitely had the same problem a few weeks ago. I came up with the following solution:

  • reproduce the last backup you have (in our case, it was one year old)
  • write a small parser that moves all requests from production to a temporary database (I chose mongodb for this): I used the rake task and "eval" to create the hash.
  • play data in the following order
    • play in the first creation of the object, if it does not already exist.
    • Find the latest update (by date) and play it.

Here is a regex for scanning production.log:

file = File.open("location_of_your_production.log", "rb") contents = file.read contents.scan(/(Started POST \"(.*?)\" for (.*?) at (.*?)\n.*?Parameters: \{(.*?)\}\n.*?Completed (.*?) in (.*?)ms)/m).each do |x| # now you can collect all the important data. # do the same for GET requests as well, if you need it. end 

In my case, the temporary database accelerated the process of parsing the log file, so the above steps could be taken. Of course, everything that was not sent over production.log will be lost. In addition, updates to the objects will send all the information, it may be different in your case. I could also recreate the loading of images, since the images were sent base64 encoded in the production.log file.

Good luck

+2
source

Source: https://habr.com/ru/post/1276527/


All Articles