MDADM (Linux Software RAID) Cheatsheet (BASH)

You should always partition disks rather than using the disk itself. Different manufacturers and models can have very slightly different capacities. So if you try to replace a 4TB WD drive with (say) a 4TB Hitachi, you may find you can't add it to the array. Creating a partition of a set size tends to address this, though on slightly larger disks it may leave a few bytes unused

After any changes, remember to skip down to the bottom and save the array config so it'll persist after reboot!

Read more…

Apache - retrieve file from S3 if it does not exist on disk (Misc)

On hosted systems with limited disk space, you might occasionally want to archive files out to Amazon S3 (or some other filer). This would usually result in URLs changing, though, so the ideal situation would be to have Apache simply proxy the request through to your filer if it cannot find the file on disk

This snippet checks for the file within the document root and if it does not exist, sends a request our to an S3 bucket

Read more…

Inserting new rows based upon a mix of static values and results from another query (MySQL)

Directly copying data from one table to another in MySQL is pretty straightforward, but what if you want to insert new rows based upon a query for something else?

As an example, assume we have the following tables in a database:

tbl_colours
-----------------------
|   id   |   colour   |
-----------------------
|   1    |   maroon   |
|   2    |   green    |
-----------------------

tbl_product_to_colour
-----------------------------
|   prodid   |   colourid   |
-----------------------------
|     1      |      1       |
|     2      |      1       |
|     3      |      2       |
|     4      |      1       |
-----------------------------

So products are mapped to a colour. However, you decide that you want to create a new colour - "Red" - and as Maroon is a shade of red, update the mapping table so that all entries mapped to maroon are also mapped to red.

You can quite easily do this via an external script, but it's actually no harder to do directly in MySQL. The first step being to insert into the colours table so that you've got the ID to use in the mapping table:

tbl_colours
-----------------------
|   id   |   colour   |
-----------------------
|   1    |   maroon   |
|   2    |   green    |
|   3    |   red      |
-----------------------

Read more…

Listing active ports without netstat (BASH)

There are sometimes occassions where you may want to list all active ports without relying on inbuilt tools (for example because you suspect netstat and lsof have been tampered with). This snippet will generate output detailing the local IP and ports being used - note that it will also include client ports, so will include any connections your system is making upstream

Read more…