Skip to content

Instantly share code, notes, and snippets.

@bminer
bminer / ebs-backup.php
Created Mar 8, 2012
Simple PHP script to backup EC2 EBS volumes. Nice to include as a daily Cron job.
View ebs-backup.php
#! /usr/bin/env php
<?php
//Don't forget to setup credentials for the AWS API.
require_once('/usr/lib/aws-php-sdk-1.3.1/sdk.class.php');
$ec2 = new AmazonEC2();
$volumes = array(
'vol-xxxxxxxx' => '-- Your snapshot description here --',
'vol-xxxxxxxx' => '-- Your snapshot description here --',
'vol-xxxxxxxx' => '-- Your snapshot description here --'
@bminer
bminer / exportDatabaseToCSV.php
Created Jun 20, 2012
Quick code to export an entire database schema to RFC4180-compliant CSV files and wrap them in a Gzip-compressed tarball.
View exportDatabaseToCSV.php
<?php
/* exportDatabaseToCSV
Author: Blake C. Miner
$db - must be a PDO connection
$database - string, the name of the schema to be backed up
$filename - string, the name of the *.tar file to be generated
*/
function exportDatabaseToCSV($db, $database, $filename) {
//Delete files
if(file_exists($filename) )
@bminer
bminer / variableHash.js
Created Jan 23, 2013
Generate a variable-length hash of `data` in Node.js
View variableHash.js
var crypto = require("crypto");
/* variableHash - Generate a variable-length hash of `data`.
Similar to the answer here: http://crypto.stackexchange.com/a/3559/4829
If you want a b-bit hash of the message m, then use the first b bits of AES-CTR(SHA256(m)).
Rather than using the suggested algorithm in the stackexchange answer above, I developed
my own.
I decided to derive AES256 initialization vector and key from the output of SHA256(data).
@bminer
bminer / changeTypeAttr.js
Created Aug 31, 2012
Cross-browser solution for changing the 'type' attribute of an `<input/>` tag.
View changeTypeAttr.js
/* x is the <input/> element
type is the type you want to change it to.
jQuery is required and assumed to be the "$" variable */
function changeType(x, type) {
if(x.prop('type') == type)
return x; //That was easy.
try {
return x.prop('type', type); //Stupid IE security will not allow this
} catch(e) {
//Try re-creating the element (yep... this sucks)
@bminer
bminer / reactHandlerUtils.js
Last active Oct 11, 2018
Utility to make React `onChange` event handlers more enjoyable to write
View reactHandlerUtils.js
/* Returns a function `(value, merge) => {...}` that calls
`component.setState(...)` to update the component's state with the new value
for `key`.
A simple example:
const func = updateKey(component, "counter");
func(23);
which is equivalent to:
View isqrt.go
package main
import (
"fmt"
"strings"
)
// odds computes the square root by adding odd numbers together until n is
// reached.
// Cost: 4 commands per loop; O(n) loops
@bminer
bminer / express_sessions_socket.io.js
Created Sep 13, 2011
Accessing/Manipulating Session Data in Socket.IO (Express + Sessions + Socket.IO)
View express_sessions_socket.io.js
var express = require('express');
var RedisStore = require('connect-redis')(express);
express.socketio = require('socket.io');
var app = express.createServer();
app.configure(function() {
app.use(express.cookieParser() );
app.sessionStore = new express.session.MemoryStore(); //Not recommended for production use
app.use(express.session({
@bminer
bminer / range_sensor.py
Created May 21, 2018
Python Script for Raspberry Pi + HC-SR04 sensor to measure water level in a sump pit
View range_sensor.py
#!/usr/bin/python
# Measure distance to water level in sump pit using
# HC-SR04 ultrasonic ranging module
# Sensor info:
# 2cm - 4m. range with ~3mm. accuracy, 15 degree measuring angle
# 5VDC, 15mA working current
# Suggestion in spec to connect ground before 5VDC line when hot plugging
# Measure surface area no less than 0.5 m^2 (really?)