Author Archive

Linux Simple Benchmark with gnu time

This is used to show the runtime length of an executable in linux. It also shows the memory usage.

Download and compile GNU Time
http://directory.fsf.org/project/time/

wget http://ftp.gnu.org/pub/gnu/time/time-1.7.tar.gz
tar -xvzf time-1.7.tar.gz
cd time-1.7
./configure
make
make install
ln -s `pwd`/time /usr/local/bin/time

Add an alias to ~/.bashrc

alias gtime='/usr/local/bin/time -f "User: %U -- System: %S -- Elapsed: %E -- CPU: %P -- Max Res Memory: %MkB"'

Use the command to run a benchmark

gtime ./program

The output looks something like this:

User: 40.59 -- System: 0.08 -- Elapsed: 0:40.89 -- CPU: 99% -- Max Res Memory: 312944kB

Comments

Create and Email a patch with Git

Create a patch of a single commit.
git format-patch f98358d9b7af7e7429fec40b2da88feb5fafe6cf -1

Email the patch as an attachment.
echo "My Message Body" | mutt -s "my subject line" -a 0001-Added-database-build-infrastructure.patch -- recipient@email-server.com

Apply the patch.
git apply 0001-Added-database-build-infrastructure.patch

Comments

Keeping the Time Current on a Linux Server


apt-get install rdate
rdate -s time-a.nist.gov
crontab -e
34 3 * * * /usr/bin/rdate -s time-a.nist.gov

See http://www.pool.ntp.org/zone/@ if the above ntp server doesn’t work for you.

Comments

Backup a folder

I usually keep this script in the
“/usr/bin/archive” file and make it executable.

#!/bin/bash

FOLDER=$1
NOW=$(date +"%Y-%m-%d")

OUTPUT="tar -cjf $FOLDER-$NOW.tar.bz $FOLDER"
echo $OUTPUT
$OUTPUT

Usage:

$ archive folder_name

Will create a file “folder_name-2010-07-18.tar.bz” in the current directory. Then you can rm -rf the folder and copy the archive to your archived location.

This goes well with “/usr/bin/archive_all”

#!/bin/bash

for file in *;
do
if [ -d "$file" ]; then
archive "$file";
fi

done;

This will create archives of all the subdirectories in the current directory. This is good for archiving old projects that don’t need to be uncompressed anymore.

Comments

Programming Considerations

Performance (the universal currency of software)

Maintainability
– modularity

Resources
– Network
– Memory

Financial Cost
– Programmer Time
– Hardware Cost
– Licenses Cost
– Electricity Cost

Human Resources
– Programmer Time

Time to Market

Correctness

Simplicity

User Friendliness (usability)

Stability

Scalability

Feature Set

Security

Business Integration
– Coordination and Alignment with Domain Knowledge Experts
– Marketing
– Product Management

Feasibility

Comments

Keep an Ubuntu server’s time up to date

Install rdate

apt-get install rdate

Edit the cron table
crontab -e

Add this entry

30 5 * * * /usr/bin/rdate -s time-a.nist.gov

Comments

Croned MySQL Database Backup Script and Transfer to an Offsite Location

This bash script regularly (from a cron) backs up a production database to an offsite server into a date-stamped compressed (gzip) format.

It is intended to be run from the production server. You need to generate a public key (ssh-keygen) for the production server and add it to the offsite server’s authorized keys file (/home/username/.ssh/authorized_keys). Be sure to chmod 600 the public and private key after creating them.

Also make sure that you use the same username for the key generation, the connection string key location and the cron entry.


#
# http://codingsnippets.com/croned-mysql-database-backup-script
#
# This bash script regularly (from a cron) backs up a production
# database to an offsite server into a date-stamped compressed
# (gzip) format.
#
# It is intended to be run from the production server.
#
#
# Please report any comments, bugs or improvements at
# http://codingsnippets.com/croned-mysql-database-backup-script
# and please leave this message intact so others can
# do the same, improve the script, or download the
# most current version. Thanks.
#

LOCALFILE_PATH="/home/www/backups"
FILENAME="livedb-`date "+%Y-%m-%d"`.sql.gz"
SSH="$(which ssh)"
GZIP="$(which gzip)"
SCP="$(which scp)"
MYSQL_DUMP="$(which mysqldump)"

DB_HOSTNAME="localhost"
DB_USERNAME="USERNAME"
DB_PASSWORD="password"
DATABASE="database_name"

#Offsite location
SSH_PORT="22"
SSH_HOSTNAME="yourhostname.com"
SSH_USERNAME="yourusername"
SSH_KEY_LOCATION="~/.ssh/id_rsa"
BACKUP_LOCATION="/home/user/backups"

echo $FILENAME
$MYSQL_DUMP -u$DB_USERNAME -p$DB_PASSWORD -h$DB_HOSTNAME $DATABASE | $GZIP -9 > $LOCALFILE_PATH/$FILENAME
$SCP -i $SSH_KEY_LOCATION -P $SSH_PORT $LOCALFILE_PATH/$FILENAME $SSH_USERNAME@$SSH_HOSTNAME:$BACKUP_LOCATION/$FILENAME

Comments (1)

Drop All Tables in a MySQL Database

This BASH Script drops all tables in a MySQL Database without having to drop the database.

This is courtesy of http://www.cyberciti.biz/faq/how-do-i-empty-mysql-database/


#!/bin/bash
MUSER="$1"
MPASS="$2"
MDB="$3"

# Detect paths
MYSQL=$(which mysql)
AWK=$(which awk)
GREP=$(which grep)

if [ $# -ne 3 ]
then
echo "Usage: $0 {MySQL-User-Name} {MySQL-User-Password} {MySQL-Database-Name}"
echo "Drops all tables from a MySQL"
exit 1
fi

TABLES=$($MYSQL -u $MUSER -p$MPASS $MDB -e 'show tables' | $AWK '{ print $1}' | $GREP -v '^Tables' )

for t in $TABLES
do
echo "Deleting $t table from $MDB database..."
$MYSQL -u $MUSER -p$MPASS $MDB -e "drop table $t"
done

Comments

Securing Subversion (SVN) Under HTTPS with Apache

This configuration is used to set up a svn repository under https for apache.

Contents of vhost file

<VirtualHost *:443>
    ServerAdmin admin@example.com
    ServerName svn.example.com

    SSLEngine on
    SSLCertificateFile /websites/_certs/example.com.crt
    SSLCertificateKeyFile /websites/_certs/example.com.key

    <Location />
        SSLRequireSSL
        DAV svn
        #SVNPath /repositories/repo_name
        SVNParentPath /repositories

        AuthzSVNAccessFile /repositories/svn-access-policy

        AuthType Basic
        AuthName "Restricted Area"
        AuthUserFile /repositories/.htpasswd
        Require valid-user
    </Location>
</VirtualHost>

<VirtualHost *:80>
    ServerName svn.example.com
    RedirectMatch (.*) https://svn.example.com$1
</VirtualHost>

Contents of /repositories/svn-access-policy

[groups]
administrators = user1, user2

[repo_name:/]
@administrators = rw

Comments (1)

Subversion (SVN) Backup BASH Script

This bash script performs an svnadmin hotcopy, tar and gunzips the resulting copy, then transfers the file to a remote server via scp with a datestamped filename.  Enjoy.


#
# http://codingsnippets.com/subversion-svn-backup-bash-script
#
# This script performs an svnadmin hotcopy, tar and
# gunzips the resulting copy, then transfers the file
# to a remote server via scp with a datestamped filename.
#
#
# Please report any comments, bugs or improvements at
# http://codingsnippets.com/subversion-svn-backup-bash-script
# and please leave this message intact so others can
# do the same, improve the script, or download the
# most current version. Thanks.
#

### Change these variables ###

LOCALFILE_PATH="/home/www/backups"
REPO_LOCATION="/repos/myrepo"
TARGET_SCP_LOCATION="username@your.remote.server.com:/home/username/backups/"
TARGET_SCP_PORT="22"
SCP_ID="~/.ssh/id_dsa"

### You don't need to change these ###

FILENAME="repo-`date "+%Y-%m-%d"`.tar.gz"
SCP="$(which scp)"
SVNADMIN="$(which svnadmin)"
TAR="$(which tar)"
TEMP_REPO_DIR="$LOCALFILE_PATH/temp_repo"
RM="$(which rm)"
MKDIR="$(which mkdir)"

### Commands ###

$MKDIR $TEMP_REPO_DIR
$SVNADMIN hotcopy $REPO_LOCATION $TEMP_REPO_DIR
$TAR -cvzf $LOCALFILE_PATH/$FILENAME $TEMP_REPO_DIR
$SCP -i $SCP_ID -P $TARGET_SCP_PORT $LOCALFILE_PATH/$FILENAME $TARGET_SCP_LOCATION/$FILENAME
$RM -rf $TEMP_REPO_DIR
$RM $LOCALFILE_PATH/$FILENAME

Comments