Konstantin Vlasenko

An engineer is someone who can make for a dollar what any fool could make for two. – Alan Kay

Backup you on-prem/local #PostgreSQL database to cloud #AWS #RDS snapshot

backup for $0.026 + $0.125 per GB-month

Total costs of the backup operation: $0.026 + $0.125 per GB-month

Script below will do the following steps:

  1. create PostgresSQL AWS RDS t1.micro instance
  2. wait till instance started by using aws rds describe-db-instances
  3. get AWS RDS instance address
  4. copy database
  5. create AWS RDS instance snapshot and terminate instance by using aws rds delete-db-instance
  6. send notification by using AWS SNS service

Prerequisites:

#!/bin/bash
SNSTOPIC=arn:aws:sns:us-east-1:000000000000:MYTOPIC
RDSINSTANCE=MYBACKUP
_now=$(date +"%d%m%Y")
BACKUPNAME="$RDSINSTANCE$_now"
DATABASE=MYDB
DBADMIN=admin
export PGPASSWORD=MYPASSWORD

# create RDS instance
aws rds create-db-instance --db-instance-identifier $RDSINSTANCE --allocated-storage 5 --db-instance-class db.t1.micro --no-multi-az --engine postgres --master-username $DBADMIN --master-user-password $PGPASSWORD --db-name $DATABASE --backup-retention-period 0

# wait till instance started
while [ `aws rds describe-db-instances --db-instance-identifier $RDSINSTANCE | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["DBInstances"][0]["DBInstanceStatus"]'` != "available" ]; do sleep 10; done

# get instance address
AWSHOST=`aws rds describe-db-instances --db-instance-identifier $RDSINSTANCE | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["DBInstances"][0]["Endpoint"]["Address"]'`

# copy database
pg_dump -U postgres dbname=$DATABASE | psql --host=$AWSHOST --username=$DBADMIN --dbname=$DATABASE

# terminate instance and create snapshot
aws rds delete-db-instance --db-instance-identifier $RDSINSTANCE --final-db-snapshot-identifier $BACKUPNAME

# send notification
aws sns publish --topic-arn $SNSTOPIC --subject 'AWS RDS BACKUP' --message 'Done'

Doctrine 1.x log all queries

To log all queries we need to use Event Listeners

class QueryDebuggerListener extends Doctrine_EventListener
{
    public function preStmtExecute(Doctrine_Event $event)
    {
        $q = $event->getQuery();
        $params = $event->getParams();

        while (sizeof($params) > 0) {
            $param = array_shift($params); 

            if (!is_numeric($param)) {
                $param = sprintf("'%s'", $param);
            }   

            $q = substr_replace($q, $param, strpos($q, '?'), 1); 
        }   
        error_log($q);
    }
}
$queryDbg = new QueryDebuggerListener();
$dbh = new PDO($dsn, $user, $password);
$conn = Doctrine_Manager::connection($dbh);
$conn->addListener($queryDbg);

Restore #AWS RDS instance from a database snapshot by using AWS CLI

The AWS Command Line Interface is a unified tool to manage your AWS services.
I am using AWS RDS PostgreSQL for my application. I don’t need to run this application 24/7/365. So I have the ability to stop periodically my instances to save some money in my pocket. Nobody wants to do repeated tasks manually. So am I.
Below is the bash script which I use to restore db instance from a snapshot and wait till availability status:

aws rds restore-db-instance-from-db-snapshot --db-instance-identifier myDBinstanseName --db-snapshot-identifier myDBinstanseName-final-snapshot --db-instance-class db.t1.micro --no-multi-az
while [ `aws rds describe-db-instances --db-instance-identifier myDBinstanseName| python -c 'import json,sys;obj=json.load(sys.stdin);print obj["DBInstances"][0]["DBInstanceStatus"]'` != "available" ]; do sleep 10; done

Note: This script is part of bigger automation. So I don’t run it manually:)

How to relaunch CloudInit script

sudo rm /var/lib/cloud/sem/*
sudo /etc/init.d/cloud-init start

#Apache: avoid using .htaccess files completely

.htaccess files are read on every request.

You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance.

http://httpd.apache.org/docs/2.4/howto/htaccess.html

Completely remove files from Git repository

git filter-branch --index-filter 'git rm -r --cached --ignore-unmatch <FOLDER/FILE>' HEAD
rm -rf .git/refs/original/ && git reflog expire --all &&  git gc --aggressive --prune

Find region from within EC2 instance by #PowerShell

(ConvertFrom-Json (Invoke-WebRequest -Uri http://169.254.169.254/latest/dynamic/instance-identity/document -UseBasicParsing).Content).region

2013 in review

The WordPress.com stats helper monkeys prepared a 2013 annual report for this blog.

Here’s an excerpt:

The concert hall at the Sydney Opera House holds 2,700 people. This blog was viewed about 18,000 times in 2013. If it were a concert at Sydney Opera House, it would take about 7 sold-out performances for that many people to see it.

Click here to see the complete report.

Migrating from Bootstrap 2.x to 3.0

Bootstrap 3 is not backwards compatible with v2.x.

http://getbootstrap.com/getting-started/#migration

Follow

Get every new post delivered to your Inbox.