Skip to content

Instantly share code, notes, and snippets.

@bmarini
bmarini / backup-sites
Created January 4, 2009 20:51
A quick and dirty website backup script
#!/bin/bash
# a quick and dirty backup script.
# if you have a websites folder and some databases to backup
# this script might be a good starting point.
# this script assumes you have user named admin.
BASE_DIR='/'
TARGET_DIR='sites'
BACKUP_TIME=`date +%Y%m%d%H%I%S`
BACKUP_FILENAME="${BACKUP_TIME}-${TARGET_DIR}"
module Rack
# Renders a valid robots.txt according to http://www.robotstxt.org
# This is an example of over-engineering. But it's simple example of
# how you might test your middleware.
class RobotsTxt
def initialize(app)
@app = app
end
def call(env)
@bmarini
bmarini / bash_login.sh
Last active September 5, 2015 00:04
My current bash_login
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
# rbenv support
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
@bmarini
bmarini / active-record-demo.rb
Created November 30, 2010 23:20
Simple usage of AR outside of rails
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
ActiveRecord::Schema.define do
create_table :authors do |t|
t.string :name, :null => false
end
add_index :authors, :name, :unique
@bmarini
bmarini / modular-classes.rb
Created December 29, 2010 23:21
Rails 3 inspired modularity
# Making a large class modular
# * Put core functionality into Base module inside your class
# * Split other functionality into modules and include them at the bottom of the
# your class
# * Now it is easy to reuse or recreate custom versions of the class by creating
# new classes, including only some modules, swapping modules out for others, etc.
class MainClass
module Base
attr_accessor :name
def initialize(name)
@bmarini
bmarini / bash-one-liners.sh
Created May 5, 2011 22:05
Bash one liners
# Start a job from an SSH session that will continue to run after logging out
nohup ./myprogram > foo.out 2> foo.err < /dev/null &
# Clear memcached
echo flush_all | nc localhost 11211
@bmarini
bmarini / ruby-prof-scaffold.rb
Created June 14, 2011 18:08
ruby-prof scaffold code
require 'ruby-prof'
# RubyProf.measure_mode = RubyProf::PROCESS_TIME
# RubyProf.measure_mode = RubyProf::WALL_TIME
# RubyProf.measure_mode = RubyProf::CPU_TIME
# RubyProf.measure_mode = RubyProf::ALLOCATIONS
# RubyProf.measure_mode = RubyProf::MEMORY
# RubyProf.measure_mode = RubyProf::GC_RUNS
# RubyProf.measure_mode = RubyProf::GC_TIME
result = RubyProf.profile do
@bmarini
bmarini / default.vcl.pl
Created June 30, 2011 18:01
A good varnish config for a Rails app
# https://www.varnish-cache.org/docs/2.1/tutorial/vcl.html
# https://www.varnish-cache.org/trac/wiki/VCLExamples
# Summary
# 1. Varnish will poll the backend at /health_check to make sure it is
# healthy. If the backend goes down, varnish will server stale content
# from the cache for up to 1 hour.
# 2. Varnish will pass X-Forwarded-For headers through to the backend
# 3. Varnish will remove cookies from urls that match static content file
# extensions (jpg, gif, ...)
@bmarini
bmarini / local-replication.sh
Created August 25, 2011 01:18
Local Replica Set
mkdir ~/tmp_mongo_data1
mkdir ~/tmp_mongo_data2
mkdir ~/tmp_mongo_data3
# In separate terminals
mongod --dbpath ~/tmp_mongo_data1 --port 5000 --replSet ben
mongod --dbpath ~/tmp_mongo_data2 --port 5001 --replSet ben
mongod --dbpath ~/tmp_mongo_data3 --port 5002 --replSet ben
# Then setup replication in a 4th terminal
@geemus
geemus / addendum.markdown
Created September 1, 2011 20:26
API - Assumptions Probably Incorrect