12 December 2006

Killing System Processes in Ruby

So I have recently had the need to create system process and kill them at times. I have never done this in ruby and it turned out to be fairly easy. In fact, like many things in ruby it was much easier than I was trying to make it. The real problem I was having was trying to figure out what the pid of the system process was. Process.fork returned the pid of the child ruby process that was forked, not the system call that was made inside the fork block, or so I thought. It turns out if you fork and then exec, the pid returned from the fork is the pid of the exec’ed process. This is of course how it should work, I just wanted it to be more tricky than that for some reason. Here is the little class I wrote to test it out.

class ExternalProcesses
def initialize
@pids = []
end

def run_command(cmd)
pid = Process.fork do
exec(cmd)
end
Process.detach(pid)
@pids << pid =" @pids.pop" e =" ExternalProcesses.new">
During the ’sleep 10′ you can verify that this works as expected by opening another terminal and looking at your systems process list.

13 November 2006

Barn Raising

Grandpa left a couple of presure treated 4×4 posts at our house a couple of months ago. I kept looking at them thinking it was a waste to have them just sitting on the ground. What could I do with them? The best thing I could think of was to build a shed. So it was off to Home Depot… A few hundred dollars later and a quite a few hours (spread over a few months) later and I almost have a shed to put my gardening stuff in.

So I really didn’t/don’t know how to build a shed. I just started asking questions to people who I thought might know something about it. I learned a lot from talking to them and then started hammering nails into some boards. It is going pretty well. The real problem is that I don’t have any time to work on it during the day, and it gets dark by 5:00 now. So Saturday is the only real time I have to work on it. Well, I will persevere and hopefully have it done before Christmas!

09 November 2006

Backup Plan

So I have recently setup a backup plan for our mysql database. I am pretty happy with how it came out. I use Amazon S3 as the final storage device for all of the backups. A fairly simple ruby script (below) runs mysqldump and then pushes the result out to S3.

I wanted a weeks worth of daily backups, a months worth of weekly backups, and monthly backups for all of eternity. After figuring out how to use the ruby date object the rest was pretty easy. The ruby cookbook held the only decent documentation that I could find on how to actually use the date object. Thanks for the excellent documentation!

I also used some information from a great post on the Mission Data Blog. The current code I had opened a file read it into memory and then transferred that to S3. The mentioned blog post explained how to change Net::HTTP to stream a file.

Here is the interesting part of the code I wrote:

# create AWS connection
conn = S3::AWSAuthConnection.new(AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY,
USE_SSL)

today = Date.today
system("mysqldump --opt dbname |
bzip2 -c > /var/backups/dbname-#{today.to_s}.sql.bz2")

#put out today's backup
putfile conn, :bucket => "db-backup",
:key => "dbname-#{today.to_s}.sql.bz2",
:file => "/var/backups/dbname-#{today.to_s}.sql.bz2"

#remove old copies
if today.day % 7 == 1
delete conn, :bucket => "db-backup",
:key => "dbname-#{(today < "db-backup", :key => "dbname-#{(today - 7).to_s}.sql.bz2"
end

#remove old local copies
FileUtils.rm "/var/backups/dbname-#{(today - 7).to_s}.sql.bz2",
:force => true

If anyone has any interesting ideas on how to implement a better backup plan I would be very interested in hearing some more ideas.

07 November 2006

What the world needs now...

No, not love sweet love, but another blog! After fighting destiny for so many years, the oocha blog is officially started! I will put my first real post up soon. First it is off to the docs to figure out how this crazy contraption works.

paul