Better Way to Copy a Large Quantity of Data Over a Network Without Using ‘scp’

Better Way to Copy a Large Quantity of Data Over a Network Without Using ‘scp’

If you ever need to copy a large amount of data over a network (especially if its a huge number of small files) you can pipe a tar command through a ssh connection, and because tar copies whole blocks at a time, it will be far, far, faster than using SCP.

To execute it, simply:

$ tar czf - <files> | ssh user@host "cd /wherever; tar xvzf -"

 

How to Speed Up Importing or Recovering a Large MySQL Database

How to Speed Up Importing or Recovering a Large MySQL Database

I recently had to import a 30GB MySQL database from a backup of a client's production database. My development workstation really struggled with the hundred's of thousands of INSERT queries, and the import either took an unacceptably long time or failed outright.

Fortunately, there is a sure-fire way to increase the import though (in my case a 100 fold increase in speed).

Simply open Terminal and type:

> mysql -uXXX -pXXX

...and replace the X's with an appropriate username and password and then paste this at the MySQL prompt:

CREATE DATABASE my_database;
USE my_database;
set global net_buffer_length=1000000;
set global max_allowed_packet=1000000000;
SET autocommit=0;
SET unique_checks=0;
SET foreign_key_checks=0;
SOURCE /some/path/database_file.sql;
COMMIT;
SET autocommit=1;
SET unique_checks=1;
SET foreign_key_checks=1;

A few notes: 'set global net_buffer_length' and 'set global max_allowed_packet' only apply if your source filename and path are over a network. Also simply omit the 'CREATE DATABASE my_database;' line if you already imported the blank schema, or the database already exists.

Using Ruby’s Metaprogramming to Initialize an Object From a Hash

Using Ruby’s Metaprogramming to Initialize an Object From a Hash

Consider the code:

class A
  attr_accessor :b, :c, :d, :e, :h, :i, :x
end

Now imagine that you want to initialize each instance variable to the one that has the same name in the hash.. Imagine all the repetitive and crappy code that would generate.

But this is Ruby and with Ruby there is *nearly* always a better way.  Instead, meta-program it, and mix-it-in.

module constructed_from_hash
 def initialize(h)
  h.each { |k, v| send("#{k}=", v) }
 end
end

class A
 include constructed_from_hash
 attr_accessor :b, :c, :d, :e, :h, :i, :x
end

Nice, elegant and clean. Just the way Ruby code is supposed to be. AND this code will now scale, as more accessors are added to the object over time, the constructor too, wont need reprogramming. If you don't need to do this often, you can pull just the constructor out of the module and put it directly into the class, but this way provides the most flexibility.

Header image taken from Examining Dwemthy’s Array composite pattern. An interesting read in it's own right. check it out.

Monitoring the Progress of a Very Large MySQL Database Import

Monitoring the Progress of a Very Large MySQL Database Import

Previously I have mentioned an awesome little command-line tool called 'pv'. Recently, I was trying to restore a clients of legacy database on my development machine, which was a staggering 30GB .sql file. I was having quite a few problems trying to wrestle this beast, the worst of which being that I had no idea how much longer the import would take, or if it had locked up. Luckily, PV comes to the rescue.

Normally, when importing a MySQL dump file, you can just type:

mysql -uxxx -pxxx dbname < /sqlfile.sql

...to import directly from the file. However, you can pv the file and pipe it into the mysql executable like:

pv sqlfile.sql | mysql -uxxx -pxxxx dbname

And you will get an awesome progress bar about how completed the task is. You can also use it in the reverse. You'd be surprised how useful it can be.

Tv-Renamer – Trust Me, It Just Works!

Tv-Renamer – Trust Me, It Just Works!

If like me, you consume a great deal of Television; and have always wanted a tool to help re-organise and use a single file/directory naming convention for all your media files - here it is.

TVRenamer is a Java GUI utility to rename TV episodes from TV listings. Basically, it will take an ugly filename like Lost.S06E05.DD51.720p.WEB-DL.AVC-FUSiON.mkv and rename it to Lost [6x05] Lighthouse.mkv

It has a whole lot of features which I won't bother to go into (as if rapid, reliable auto-renaming/sorting wasn't enough!) but I really encourage everyone to check this out. It really is amazing.

Hands-down, the BEST way to Install MySQL on Mac OSX Mavericks

Hands-down, the BEST way to Install MySQL on Mac OSX Mavericks

I recently had some database woes. I needed to restore a MySQL database of a clients existing website that was dozens of gigabytes in size. I had a great deal of trouble trying to import that data, but that's a story for another time. This was about getting MySQL installed in the first place (for a development environment).

At first, I tried using Homebrew - because its awesome and I like it. But sadly, for one reason or another the default configuration just wasn't working for me. It was simple and blind, but too well hidden and I didn't feel like there was enough "control" (like getting setting/getting the default root password for example). If you're going to automate something, then automate it - but don't ask me to run stuff to secure my install when your supposed to be automating it for me.  Bah humbug!

However, the wonderful folks at macminivault.com solved all my problems. It was simply perfection. Exactly the right balance of automation and control.

Just open up Terminal and paste this into it:

bash <(curl -Ls http://git.io/eUx7rg)

It will tell you or prompt you for the rest. And don't forget to get the text file containing the root password, before you mistakenly delete it.

Simplicity itself.

(comic property of http://www.brainstuck.com/)

How to Fix ‘Requirements installation failed’ When Installing RVM Ruby on OSX Mavericks

How to Fix ‘Requirements installation failed’ When Installing RVM Ruby on OSX Mavericks

Installing Ruby with RVM on Mac is a cinch, simply execute:

\curl -sSL https://get.rvm.io | bash -s stable --ruby

But recently, while trying to install RVM instead of the default Mavericks Ruby, the script that normally just "works" fails with the ominous message:

: Requirements installation failed with status: 1.

It turns out to be something funky with Mavericks and the Homebrew step of the installation. Luckily, despite the failure of the script, you can simply install Hombrew manually to solve the problem. Installing Homebrew is a triviality, simply execute the following command in Terminal:

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

It will first install the XCode Command Line Tools (don't worry if you already have it, just hit "install" and let it do it's thing) and then once the Xcode dialog disappears, hit [any] key in Terminal and it will auto-download and auto-install itself. Once its all finished (and it can take a few minutes, just be patient), simply re-execute the command to install RVM Ruby.

Showing Hidden Files in OSX Finder

Just like when using Windows, sometimes it is necessary to make special hidden system files visible to Finder.  There is no preference for it, but with a simple Terminal command, things can be made visible very easily, on a privileged user account.

Simply open the 'Terminal' application, and at the prompt, type:

  1. defaults write com.apple.finder AppleShowAllFiles TRUE

and then:

  1. killall Finder

This will cause all Finder windows to close and then reopen with the hidden files, visible and identifiable with a ghost-like appearance. You should be able to interact with them normally now.

Screen Shot 2013-11-28 at 11.00.54 pm

Syncing Google Contacts with iMessage (and Contacts) on OSX Mavericks

You may remember earlier in the year I took issue with the super secret hidden checkbox in the Mountain Lion's version of Contacts which magically syncs your Google contacts with OSX Contacts and Messages. As it seems, Apple have finally fixed the SSL problem stopping you from syncing through the Google option in System Preference's Internet Accounts setup.

Getting the sync to work now is as trivial as adding a Google account to your Internet Accounts list, log in and turn on the items you want to sync. Screen Shot 2013-11-26 at 9.46.42 pmHurray! Easy peasy.

It's beyond me that it wasn't like this before.

 

Pipe Viewer (pv) in Mac OSX

 

Update:
Forget what I said.  Install Homebrew (really, do it now) and use HomeBrew to install it.


Pipe Viewer (pv) - is a terminal-based tool for monitoring the progress of data through a pipeline. It can be inserted into any normal pipeline between two processes to give a visual indication of how quickly data is passing through, how long it has taken, how near to completion it is, and an estimate of how long it will be until completion. However, it isn't included by default in Mac OSX.

The good news is that there are several ports of pv, you just need to go and grab one:

  • HomeBrew: Run "brew install pv" to get the latest version.
  • MacPorts: Run "port install pv" to get the latest version.
  • Or (recommended) install the Rudix pv port (a simple package installer)

Pv allows you to get a really awesome progress of your terminal commands to see how things are going; especially useful for long operations (such as cp or tar etc) so you know everything is ticking over time (and perhaps even giving an ETA for completion):

13.2GiB 1:33:17 [3.57MiB/s] [================================>                ] 67% ETA 0:44:4

I highly recommend this for anyone doing long, large or complicated terminal commands.  It's outstanding! To learn more about using Pipe Viewer, this is a great resource.