-
ARM assembler in Raspberry Pi – Chapter 18
In this chapter we will delve a bit more into the stack.
-
How to create and configure EC2 instances for Rails hosting with CentOS using Ansible
-
Check progress of a mysql database import
If you’ve ever had to do a huge mysql import, you’ll probably understand the pain of not being able to know how long it will take to complete.
At work we use the backup gem to store daily snapshots of our databases, the main one being several gigabytes in size. This gem basically does a
mysqldump
with configurable options and takes care of maintaining a number of old snapshots, compressing the data and sending notifications on completion and failure of backup jobs.When the time comes to restore one of those backups, you are basically in the situation in which you simply have to run a
mysql
command with the exportedsql
file as input, which can take ages to complete depending on the size of the file and the speed of the system.The command used to import the database snapshot from the backup gem may look like this:
What this command does is
untar
the gzipped file and sending it as an input to amysql
command to the database you want to restore (passing it throughzcat
before to gunzip it).And then the waiting game begins.
There is a way, though, to get an estimate of the amount of work already done, which may be a big help for the impatiens like myself. You only need to make use of the good
proc
filesystem on Linux.The first thing you need to do is find out the
tar
process that you just started:This last command assumes that no other processes will have that string on their invocation command lines.
We are really interested in the
pid
of the process, which we can get with some unix commands and pipes, appending them to the last command:This will basically get the last line of the process list output (with
tail
), separate it in fields using the space as a delimiter and getting the first one (cut
command). Note that depending on your OS and theps
command output you may have to tweak this.After we have the
pid
of the tar process, we can see what it is doing on theproc
filesystem. The information we are interested in is the file descriptors it has open, which will be in the folder/proc/pid/fd
. If we list the files in that folder, we will get an output similar to this one:The important one for our purposes is the number
3
in this case, which is the file descriptor for the filetar
is unpacking.We can get this number using a similar strategy:
With that number, we can now check the file
/proc/pid/fdinfo/fd_id
, which will contain something like this:The useful part of this list is the
pos
field. This field is telling us in which position of the file the process is now on. Sincetar
processes the files sequentially, having this position means we know how much percentage of the filetar
has processed so far.Now the only thing we need to do is check the original file size of the
tar
file and divide both numbers to get the percentage done.To get the
pos
field we can use some more unix commands:To get the original file size, we can use the
stat
command:Finally we can use
bc
to get the percentage by just dividing both values:To put it all together in a nice script, you can use this one as a template:
I developed this article and script following the tips in this stack overflow answer: http://stackoverflow.com/questions/5748565/how-to-see-progress-of-csv-upload-in-mysql/14851765#14851765
-
ARM assembler in Raspberry Pi – Chapter 17
In chapter 10 we saw the basics to call a function. In this chapter we will cover more topics related to functions.
-
Create a temporary zip file to send as response in Rails
We have been doing a painful migration from Rails 2 to Rails 3 for several months at work, and refactoring some code the other day I had to do something in a non straightforward way, so I thought I’d share that.
Basically we had an action that would group several files into a zip file and return those zipped files to the user as a response. In the old code, a randomly named file was created on the
/tmp
folder of the hosting machine, being used as the zip file for the rubyzip gem, and then returned in the controller response as an attachment.During the migration, we’ve replaced all those bespoken temp file generation for proper Tempfile objects. This was just another one of those replacements to do. But it turned out not to be that simple.
My initial thought was that something like this would do the trick:
But it did not. The reason for that is that the
open
method, when used with theZip::File::CREATE
flag, expects the file either not to exist or to be already a zip file (that is, have the correct zip structure data on it). None of those 2 cases is ours, so the method didn’t work.So as a solution, you have to open the temporary file using the
Zip::OutputStream
class and initialize it so it’s converted to an empty zip file, and after that you can open it the usual way. Here’s a full simple example on how to achieve this: