Archive for the ‘Linux’ Category

MySQL 5 Replication

Monday, January 14th, 2008

I recently setup MySQL 5 replication between two RHEL servers. I made a small writeup on what I had to do to get things working. In my configuration I am loading the initial database from the master, as opposed to manually copying it (via SCP). I’m assuming you have two working MySQL servers. Both with the mysql root password set, and “skip-networking” etc. commented in the configuration. I also assume open communication (iptables or otherwise) between servers. Remember you can test communication like this: telnet your-server 3306

Note: The slave server has to be equal or greater than the master version.

Do the following on the Master:

1) Insert this into your /etc/my.cnf (this is the most common location for this file, yours may be elsewhere):

[mysqld]
log-bin=mysql-bin
server-id=1

2) Open a mysql connection:

mysql -u root -p
mysql> GRANT ALL ON replicationdb.* TO slave_user@'%' IDENTIFIED BY 'slave_password';
mysql> FLUSH PRIVILEGES;
mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO slave_user@'%' IDENTIFIED BY 'slave_password';
mysql> GRANT RELOAD ON *.* TO slave_user@'%' IDENTIFIED BY 'slave_password';
mysql> QUIT;

3) Replace “replicationdb” with the database you want to replicate. Use * for all. Replace “slave_password” with a password.

4) Assuming your init scripts are setup correctly, restart the MySQL service:
/etc/init.d/mysql restart
If you don’t have an init script in place, use this:
mysqladmin shutdown; ./bin/safe_mysqld&

Do the following on the Slave:

1) Insert this into your /etc/my.cnf (this is the most common location for this file, yours may be elsewhere):


[mysqld]
log-bin=slave-relay-bin
# Each server must have a unique ID.
server-id=2
# The hostname or IP of your master.
master-host=192.168.5.200
# This user should have the necessary privileges on the database(s).
master-user=slave_user
# The password generated for the slave user.
master-password=slave_password
# The database to be replicated. 
# By default all databases will be replicated. 
# Omit this to replicate everything, including the "mysql" database (users and privileges). 
# Use multiple lines for multiple databases.
replicate-do-db=replicationdb

2) Assuming your init scripts are setup correctly, restart the MySQL service:
/etc/init.d/mysql restart
If you don’t have an init script in place, use this:
mysqladmin shutdown; ./bin/safe_mysqld&

3) Open a mysql connection:

mysql -u root -p
mysql> LOAD DATA FROM MASTER;
mysql> QUIT;

4) To check status of the master:
mysql> SHOW MASTER STATUS;

5) To check status of the slave:
mysql> SHOW SLAVE STATUS;

Unix Home Directories with winbind

Tuesday, September 11th, 2007

At work we have some Unix machines that authenticate with the Win2k3 domain. Problem is, when new users are created on the domain, Unix home directories are not created. You could set PAM to created the home directory upon first SSH login. However if the user logs in via SFTP or SMB the home directory would not be created. Below is a script I wrote (runs in cron) to create home directories for new users. Basically it checks the contents of “/home” against “wbinfo -u”.

#!/bin/sh
# Steve Horbachuk 2007
# Creates home directories for users that don't already have them

# Remove these:
# Administrator
# Guest
# krbtgt
# SUPPORT_388945a0
# IUSR_SERVER
# IWAM_SERVER
# stu
# fac

WBINFO=/usr/bin/wbinfo
SED=/bin/sed
LOG=/var/log/my-mkhomedir

# check if winbind is working
$WBINFO -u > /dev/null
if ! [ $? = "0" ]; then
echo "Get user list failed!"
echo "Is winbind running?"
exit 1
fi

LIST=`$WBINFO -u | $SED -e '/Administrator/d;/Guest/d;/krbtgt/d;/SUPPORT_388945a0/d;/IUSR_/d;/IWAM_/d;/fac/d;/stu/d'`

for USER in $LIST; do
if [ ! -d /home/${USER} ]; then
echo `date` Created ${USER} >> $LOG
mkdir /home/${USER}
cp -R /etc/skel/* /home/${USER}
chown -R ${USER} /home/${USER}
chgrp -R 'Domain Users' /home/${USER}
chmod 711 /home/${USER}
fi
done
exit 0

Kernel Packages

Tuesday, September 4th, 2007

I had to recompile my Debian Etch (4.0) kernel to include the Layer7 filter (and ipp2p) source patch as well as include Netfilter support.

Here are the prebuilt kernel packages:
http://stevehorbachuk.com/kernels

You can add then to a Debian system by doing this:

dpkg -i linux-image-2.6.22.5-custom2_2.6.22.5-custom2-10.00.Custom_i386.deb
dpkg -i linux-headers-2.6.22.5-custom2_2.6.22.5-custom2-10.00.Custom_i386.deb

This will automatically add this custom kernel to /boot/grub/menu.lst.

pcns221lnx.bin & Red Hat Enterprise Linux 4

Monday, September 3rd, 2007

pcns221lnx.bin is the APC Network Shutdown installer for Linux. It took me a while to figure out how to install this software on Red Hat Enterprise Linux 4, so I figured I’d drop a note here.

PCNS is poorly maintained and is a few years old now. Additionally Red Hat ships with GCJ java, which is shit. Upon execution it would throw cryptic error messages such as:

Preparing wizard…
Searching for JVM…
Starting the wizard…
Error: You need Swing 1.1 or 1.2 in your classpath to run this program.

And:

Preparing wizard…
Searching for JVM…
Starting the wizard…
See errors.log for other possible causes.out of disk space?

The biggest problem I had was that the installation documentation does not mention that the installer requires an X Server! Thanks a lot APC!

Here’s what I did to install it:

ssh steve@server

su
Change to root

rpm -e java-1.4.2-gcj-compat-1.4.2.0-27jpp
rpm -e java-1.4.2-gcj-compat-devel-1.4.2.0-27jpp
Remove old Java packages

rm -rf /usr/local/bin/jvm
Clean up if needed

mkdir /usr/java
Make new installation directory

cd /usr/java
Change to that directory

wget -O jre.bin http://javadl.sun.com/webapps/download/AutoDL?BundleId=11284
Download the JRE binary installer

chmod a+x jre.bin
Change permissions to allow execution

./jre.bin
Execute the JRE installer and follow the prompts

export PATH=$PATH:”/usr/java/jre1.6.0_02/bin”
Include the JRE binaries in the PATH where “jre1.6.0_02″ is the JRE version you installed

vi /root/.bash_profile
Change line that begins with “PATH” to this “PATH=$PATH:$HOME/bin:/usr/java/jre1.6.0_02/bin” where “jre1.6.0_02″ is the JRE version you installed

which java && java -version
Check if everything is working

up2date -i xorg-x11 xterm
Install X11 (if not already installed)

Launch a SSH session with X-Win32:

su
Change to root

cd /usr/local/src
Change to our source/installer directory

wget ftp://ftp.relline.ru/pub/unix/apc/pcns-221/pcns221lnx.bin
Download the PCNS binary installer

chmod a+x pcns221lnx.bin
Change permissions to allow execution

./pcns221lnx.bin
Execute the PCNS installer and follow the prompts

PowerChute Network Shutdown

ps -ef | grep “jre”
Make sure the PCNS process is running

Backup Script

Wednesday, August 15th, 2007

I recently created a BASH script that creates a g-zipped tarball and writes it to a NAS. It also checks the free space available on the NAS and deletes previous backups until it has enough space to store a new backup. Maybe someone will find it useful or will be inspired to improve it.

Variables to modify:

DATA = What directories you want to backup.
LOGDIR = Where you want the backup logs to be stored.
BACKUPMNT = The mount point of your NAS or backup device.
BACKUPDIR = The directory on the mount to store the backup.

#!/bin/bash
# Steve Horbachuk 2007
# tars directories and writes to NAS
#

DATA="/home /root /etc /usr/local /var/www /var/log"
LOGDIR="/var/log/my-backup"
BACKUPMNT="/mnt/linkstation"
BACKUPDIR="/mnt/linkstation/server"
MYDATE=`date +%F`
LASTFILESIZE=0
LASTFILENAME=""

echo "Backup started : `date`" > $LOGDIR/backup_$MYDATE.log
echo "-------------------------------------------------" >> $LOGDIR/backup_$MYDATE.log

#find the last file in the backup folder
#and store information about it.
for FILE in $BACKUPDIR/*; do
if [ -f $FILE ]; then
FILEINFO=(`ls -lk $FILE`)
LASTFILENAME=${FILEINFO[8]}
LASTFILESIZE=${FILEINFO[4]}
fi
done

#delete files until there is enough space to hold a file
#the same size as the last backup.
for FILE in $BACKUPDIR/*; do
DISKINFO=(`df -k | grep $BACKUPMNT`)
DISKFREE=${DISKINFO[3]}
if [ -f $FILE ]; then
if [[ "$LASTFILESIZE" -gt "$DISKFREE" ]]; then
rm -vf $FILE >> $LOGDIR/backup_$MYDATE.log
fi
fi
done

tar -czf $BACKUPDIR/backup_$MYDATE.tar.gz --exclude='*.iso' --exclude='*/tmp/*' $DATA >> $LOGDIR/backup_$MYDATE.log

echo "-------------------------------------------------" >> $LOGDIR/backup_$MYDATE.log
echo "Backup finished : `date`" >> $LOGDIR/backup_$MYDATE.log

Here is the mount in /etc/fstab:
//10.0.0.50/share /mnt/linkstation cifs rw,guest 0 0

Where “//10.0.0.50/share” is the SMB share on the NAS, and /mnt/linlkstation is the Linux mount point.