Calculate age from birthday with MySQL

By Nisse Pettersson at May 20, 2010 11:15
Filed Under: Code
Share on Facebook

The e-date service i'm running has no statistics on the average age of it's members. logo-mysql-110x57This is something I feel is needed and so I made my own.  This query will handle the leap year. Replace [COLUMN] with the field with birthdate and [TABLE] with the table name.

SELECT EXTRACT( YEAR
FROM (
FROM_DAYS( DATEDIFF( NOW( ) , [COLUMN]) ) ) )
 +0 AS age
FROM [TABLE]

This will give you the age of all users and if you just need the average age use the following query.

SELECT AVG( EXTRACT( YEAR
FROM (
FROM_DAYS( DATEDIFF( NOW( ) , [COLUMN]) ) ) )
) +0 AS age
FROM [TABLE]

 

Technorati-taggar:


Change location of My documents with regedit

By Nisse Pettersson at May 17, 2010 11:18
Filed Under: Technical
Share on Facebook

This is something that i've came across alot of times and I think it's about time I wrote a blog post on this. It's a verry easy fix but just a tip. Use a policy if you have the ability. This is for Windows XP

In regedit change the following key.
HKEY_LOCAL_MACHINE / Software / Microsoft / Windows / Current Version / Explorer / DocFolderPaths
There is a key with the path to the folder. Change that and reboot.

That should do the trick!



How to Vyatta firewall

By Nisse Pettersson at March 16, 2010 11:13
Filed Under: Technical
Share on Facebook

When ever i'm in need of a software firewall / router I use Vyatta.  A advanced firewall and still rather easy to setup. I'm going to have my latest setup as an example. I'm setting up a development enviroment for deployment of Windows7 and WindowsXP. I'm using the latest version of VmWare vsphere 4.

Setup

Download the ISO from the download section. Then you need to create a new virtual machine and give it some diskspace and ram. I gave it 200mb of disk and 128mb of RAM. This is up to you and what type of load is expected. Remember to add two network interfaces and set the type to e1000. One for outside and one for inside.

Start the machine and mount the ISO you downloaded.

When the system boots you will be greeted with the text

vyatta login:

Logon as root and with vyatta as password.

We now need to install vyatta on the diskdrive instead of the ISO. So type the following.

install-system

 Follow the instructions on the screen. Now the fun parts starts. Now we need to go in configuration mode(ie. enable in Cisco CLI) Write the following.

vyatta@vyatta:~$ configure
[edit]
vyatta@vyatta# set interfaces ethernet eth0 address 192.168.1.81/24
[edit]
vyatta@vyatta# commit
[edit]
vyatta@vyatta# exit
exit
vyatta@vyatta:~$

This will set the first network interface(eth0) to have address 192.168.1.81 with netmask 255.255.255.0
Then commit will save this to the running configuration. Exit will leave the configuration mode.
We now need to set up the inside interface. Use the syntax above but change the IP and eth0 to eth1.
Vyatta firewall should now respond to pings from computers on the same network and in the same subnet.

NAT

set service nat rule 1 outbound-interface eth0
set service nat rule 1 source address 10.1.1.0/24
set service nat rule 1 type masquerade

This will is a generic NAT route that sends all traffic that's outbound(not in the same subnet) to the outbound-interface. And we tell it that outside interface is eth0. This will only apply if the source network is 10.1.1.0/24(our inside network)
I like PuTTy and SSH in general so I will add a second NAT rule for SSH.

set service nat rule 2
set service nat rule 2 type destination
set service nat rule 2 inbound-interface eth0
set service nat rule 2 protocol tcp
set service nat rule 2 destination address 0.0.0.0/0
set service nat rule 2 destination port ssh
set service nat rule 2 source address 0.0.0.0/0
set service nat rule 2 inside-address address 10.1.1.1

Don't forget to save you changes to the running configuration.

commit

 

Then you need to save this to the statup config, this is done with the following command.

save
Saing configuration to '/opt/vyatta/etc/config/config.boot' ...
Done

 

You now have a vyatta firewall running as a router. There is a lot of things you can do with this so I suggest you read the documentation as I do. Will return to this topic when I have something interesting to write about.

Technorati-taggar: ,,,


How to connect Outlook 2003 to a domino mail account

By Nisse Pettersson at February 22, 2010 09:42
Filed Under: Technical
Share on Facebook

I'm not a big fan of Notes and I wish to use Outlook at my new job. So I did some google search and found a connector and a registry hack that you need to do before installation.

Step 1.
Modify the registry
[HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\O utlook\Options\General]
"UnknownRowsSortOrder"=dword:00000001

Step 2.
Go to http://www.microsoft.com/downloads/details.aspx?FamilyId=8EBBBA59-5F17-4E52-8980-C4F0DFA92D65&displaylang=en
download and install.

Step 3.
Start your Notes client and then start outlook. Create  a new account and choose "Additional Server Types". Then choose "Microsoft Office Outlook Connector for IBM Lotus Domino". Click finish and then you are ready to use Outlook instead of Notes



Ringtones to your Iphone

By Nisse Pettersson at February 19, 2010 08:53
Filed Under: Technical
Share on Facebook

I'm one of those who got a new Iphone for my self as a present when I got a new job. The thing is that i'm a fan of spotify and not of Itunes so I don't pay for my mp3 songs. The issue here is that you can't take a song from your itunes library and make it the ringtone. You need to buy that song from Itunes.

I found a webbsite called audiko.net where you can make your own songs or download songs already made by other users.

Download the songs form audiko.net and then open them in Itunes. Sync with your Iphone and voilá!



Clear all connections to a Micrsoft SQL Server(MSSQL)

By Nisse Pettersson at February 17, 2010 04:32
Filed Under: Technical
Share on Facebook

Sometimes you need to clear allt he connections to the database in order to make som changes to it. I guess that there is a lot of way doing this but this one suites me.

USE master;
GO
ALTER DATABASE [databaseName]
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO
ALTER DATABASE [databaseName]
SET MULTI_USER;
GO

 

Found it on this site



Progressbar in C# with backgroundworker

By Nisse Pettersson at February 11, 2010 12:20
Filed Under: Code
Share on Facebook

I'm developing a application where there are some time consuming database activity and I need to show the user that the application is running and not crashed. You would also want the GUI thread to be free so the user can move the window and navigate the menus.

I found a blog with a simple example of how you can use a backgroundworker for this. *



Facebook build their own PHP compiler and server

By Nisse Pettersson at February 03, 2010 02:11
Filed Under: General
Share on Facebook

I'm one of those who use Facebook more or less daily and I'm always amazed about the speed and availability of facebook. They are using PHP. If Facebok is running PHP on their site it says alot of PHP and the power of this small and simple language. Combine this with cache it's one of the best languages for the web.

I read an article on their developerblog about how they have changed the way they serve all users. Facebook have deloped a PHP compiler and named it HipHop. Simple way of explaining this is that it compiles PHP to C++ and then runned as a webserver.

Go read more about this

Technorati-taggar: ,,


facebook



Inside a botnet. Pushdo.

By Nisse Pettersson at February 02, 2010 05:25
Filed Under:
Share on Facebook

I found an interesting article on an simple but very functional botnet on Trend Micro's website. Bellow is the links for all parts.

Part 1
Part 2
Part 3
Part 4
Part 5

They are quite short so you should read them all.



Incremental Linux backup to FTP-server

By Nisse Pettersson at January 30, 2010 04:16
Filed Under: Technical
Share on Facebook

I’m running a Macedonian e-date site (www.ergeni.com.mk) as a small project just for fun and it’s running on Ubuntu Machine. I need backup offcourse and found a nice script. I’m not the author of this script and i’m not interested in taking credit for it. It works fine and is fast. Just edit the variables and run the script.

#!/bin/sh
# System + MySQL backup script
# Full backup day - Sun (rest of the day do incremental backup)
# Copyright (c) 2005-2006 nixCraft <http://www.cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# Automatically generated by http://bash.cyberciti.biz/backup/wizard-ftp-script.php
# ---------------------------------------------------------------------
### System Setup ###
DIRS="/home /etc /var/www"
BACKUP=/tmp/backup.$$
NOW=$(date +"%y-%m-%d")
INCFILE="/root/tar-inc-backup.dat"
DAY=$(date +"%a")
FULLBACKUP="Sun"
### MySQL Setup ###
MUSER="mysql-user"
MPASS="mysql-password"
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
### FTP server Setup ###
FTPD="/ergeni"
FTPU="ftp-username"
FTPP="ftp-password"
FTPS="ftp-hostname"
NCFTP="$(which ncftpput)"
### Other stuff ###
EMAILID="nisse@nissesblog.se"
### Start Backup for file system ###
[ ! -d $BACKUP ] && mkdir -p $BACKUP || :
### See if we want to make a full backup ###
if [ "$DAY" == "$FULLBACKUP" ]; then
  FTPD="/home/vivek/full"
  FILE="fs-full-$NOW.tar.gz"
  tar -zcvf $BACKUP/$FILE $DIRS
else
  i=$(date +"%Hh%Mm%Ss")
  FILE="fs-i-$NOW-$i.tar.gz"
  tar -g $INCFILE -zcvf $BACKUP/$FILE $DIRS
fi
### Start MySQL Backup ###
# Get all databases name
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
 FILE=$BACKUP/mysql-$db.$NOW-$(date +"%T").gz
 $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done
### Dump backup using FTP ###
#Start FTP backup using ncftp
ncftp -u"$FTPU" -p"$FTPP" $FTPS<<EOF
mkdir $FTPD
mkdir $FTPD/$NOW
cd $FTPD/$NOW
lcd $BACKUP
mput *
quit
EOF
### Find out if ftp backup failed or not ###
if [ "$?" == "0" ]; then
 rm -f $BACKUP/*
else
 T=/tmp/backup.fail
 echo "Date: $(date)">$T
 echo "Hostname: $(hostname)" >>$T
 echo "Backup failed" >>$T
 mail  -s "BACKUP FAILED" "$EMAILID" <$T
 rm -f $T
fi

 

Then you need to add this job to crontab. Run the following command.

crontab –e

 

Add a new line to run the file at midnight.

@midnight /path/to/script.sh >/dev/null 2>&1

 

And there you go, a fully working backup system. To restor all you need to do is to extract the files from the ftp server to the same location.
Download the document below and edit.

backup-job.txt (1,97 kb)