Jun 04

Here is my script to backup a certain MySql db and certain directory files

FYI: this script only can working on Unix/Linux environtment

Example, We will backup one mysql db & directory files under /var/www and transfer it to remote ftp to another server and save it on incremental folder by date

db : test

user : root

pass : passwordrootmysql

Remote Ftp Server (Vsftpd):

ip : 10.10.1.3

user : usertest

pass : usertestpassword

Here is my script

#!/bin/bash

# Shell script to backup MySql database

# By Adityo

MyUSER=”root”     # USERNAME

MyPASS=”passwordrootmysql”       # PASSWORD

MyHOST=”localhost”          # Hostname

# Linux bin paths, change this if it can’t be autodetected via which command

MYSQL=”$(which mysql)”

MYSQLDUMP=”$(which mysqldump)”

CHOWN=”$(which chown)”

CHMOD=”$(which chmod)”

GZIP=”$(which gzip)”

# Get data in dd-mm-yyyy format

NOW=”$(date +”%d-%m-%Y”)”

# Backup Dest directory, change this if you have someother location

DEST=”/backup/mysqlscript”

# Main directory inside the backup Dest directory (DEST) where backup will be stored

MBD=”$DEST/mysql/$NOW”

# Get hostname

HOST=”$(hostname)”

# File to store current backup file

FILE=””

# Store list of databases

DBS=””

# DO NOT BACKUP these databases , you can choose another db that you did not want to backup and split it with space in this example i exclude the mysql , information_schema and backup database an di only backup the test db

IGGY=”mysql information_schema backup”

[ ! -d $MBD ] && mkdir -p $MBD || :

# Only root can access it!

$CHOWN 0.0 -R $DEST

$CHMOD 0600 $DEST

# Get all database list first

DBS=”$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse ‘show databases’)”

for db in $DBS

do

skipdb=-1

if [ “$IGGY” != “” ];

then

for i in $IGGY

do

[ “$db” == “$i” ] && skipdb=1 || :

done

fi

if [ “$skipdb” == “-1″ ] ; then

FILE=”$MBD/$db.$HOST.$NOW.sql.gz”

# do all inone job in pipe,

# connect to mysql using mysqldump for select mysql database

# and pipe it out to gz file in backup dir 🙂

$MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS $db –lock-tables=false  | $GZIP -9 > $FILE

fi

done

### Start backup files on didirectory /var/www

mkdir -p /backup/mysqlscript/data/$NOW

cd /backup/mysqlscript/data/$NOW

FILE=”blogbinusian-file-www-$NOW.tar.gz”

tar -cvf $FILE /var/www

### FTP server Setup ###

FTPD=”//incremental”

FTPU=”usertest”

FTPP=”usertestpassword”

FTPS=”10.10.1.3″

NCFTP=”$(which ncftpput)”

### Dump backup using FTP ###

#Start FTP backup using ncftp and transfer file to remote server

ncftp -u”$FTPU” -p”$FTPP” $FTPS<<EOF # connect to target ftp service

mkdir $FTPD/$NOW/      # it will create directory /home/usertest/incremental/$NOW

cd    $FTPD/$NOW/        #  it will changes directory /home/usertest/incremental/$NOW

lcd /backup/mysqlscript/mysql/$NOW  # it will goes to source server directory /backup/mysqlscript/mysql/$NOW

mput *  # upload all files on /backup/mysqlscript/mysql/$NOW

lcd /backup/mysqlscript/data/$NOW # it will goes to source server directory /backup/mysqlscript/data/$NOW

mput *  # upload all files on /backup/mysqlscript/mysql/$NOW

quit # stop

EOF

May 27

Just today one of my friend are pm me and ask how to make his tomcat service at /etc/init.d/tomcat to run when the server reboot. At the first time i just tell him to use chkconfig because the os that he use was CentOS

What is chkconfig ?

Chkconfig is a basic system utility. It updates and queries runlevelinformation for system services. Chkconfig manipulates the numerous symbolic links in /etc/rc*.d, to relieve system administrators of some of the drudgery of manually editing the symbolic links.

chkconfig –list [name]
chkconfig –add name
chkconfig –del name
chkconfig [–level levels] name <on|off|reset>
chkconfig [–level levels] name

But when my friend use chkconfig there were error :

# chkconfig –levels 235 tomcat on
service tomcat does not support chkconfig

okay my friend ask me what is the 235 means , 235 is runlevel that the script starts.
For instance, if we called this script /etc/init.d/new-service and ran chkconfig new-service on, it would be active in runlevels 2,3 and 5.

0-6 are runlevels. Typically for Redhat based distributions:
0 – Halt the system
1 – Single-user mode
2 – Multi-user mode (without networking)
3 – Multi-user mode
5 – Multi-user mode, graphical login
6 – Reboot the system

you can check your service run level by using chkconfig –list:

# chkconfig –list
courier-authlib  0:off   1:off   2:on    3:on    4:on    5:on    6:off
courier-imap      0:off   1:off   2:on    3:on    4:on    5:on    6:off
crond                     0:off   1:off   2:on    3:on    4:on    5:on    6:off
httpd                      0:off   1:off   2:on    3:on    4:on    5:on    6:off
iptables                 0:off   1:off   2:on    3:on    4:on    5:on    6:off
ldap                         0:off   1:off   2:off   3:off   4:off     5:off   6:off
lm_sensors          0:off   1:off   2:on    3:on    4:on    5:on    6:off
lsws                         0:off   1:off   2:on    3:on    4:off   5:on    6:off
mcstrans               0:off   1:off   2:on    3:on    4:on    5:on    6:off
messagebus          0:off   1:off   2:off   3:on    4:on    5:on    6:off
mysqld                   0:off   1:off   2:off   3:off   4:off   5:off   6:off
named                    0:off   1:off   2:off   3:off   4:off   5:off   6:off
netconsole           0:off   1:off   2:off   3:off   4:off   5:off   6:off
netfs                        0:off   1:off   2:off   3:off   4:off   5:off   6:off
netplugd               0:off   1:off   2:off   3:off   4:off   5:off   6:off
network                0:off   1:off   2:on    3:on   4:on   5:on   6:off
portmap               0:off   1:off   2:off   3:off   4:off   5:off   6:off
rdisc                       0:off   1:off   2:off   3:off   4:off   5:off   6:off
restorecond        0:off   1:off   2:on    3:on    4:on  5:on    6:off
snmpd                   0:off   1:off   2:off   3:off   4:off   5:off   6:off
snmptrapd          0:off   1:off   2:off   3:off   4:off   5:off   6:off
sshd                       0:off   1:off   2:on    3:on    4:on    5:on    6:off
syslog                   0:off   1:off   2:on    3:on    4:on    5:on    6:off
winbind               0:off   1:off   2:off   3:off   4:off   5:off   6:off
xinetd                  0:off   1:off   2:on    3:on    4:on    5:on    6:off
about the service tomcat does not support chkconfig error , after some searching it turn’s out. In order to support chkconfig, an init script must:
1.  Be located in /etc/rc.d/init.d (which /etc/init.d is a symlink to)
2.  Have a commented out line that contains “chkconfig: <default  runlevels for this service> <start priority> <stop priority>”
3.  Have a commented out line that contains “description: <a description of the service>”
4.  Upon successful service startup, place a lock file in  /var/lock/subsys that matches the name of the service script.  Upon  successful service shutdown, the lockfile must be removed.

okay i ask my friend to add this on the tomcat script
#!/bin/sh
#
# Tomcat Server
#
# chkconfig: 345 96 30
# description: Java servlet container

and after some reboot itis working 🙂 , the tomcat script are executed on reboot time, here is the detail script

#!/bin/sh
#
# Tomcat Server
#
# chkconfig: 345 96 30
# description: Java servlet container

JAVA_HOME=/opt/jdk

PATH=${JAVA_HOME}/bin:${PATH}

TOMCAT_START=/opt/tomcat/bin/startup.sh

TOMCAT_STOP=/opt/tomcat/bin/shutdown.sh

export JAVA_HOME PATH

start()
{
if [ -x ${TOMCAT_START} ]; then
echo “Starting tomcat server…”
${TOMCAT_START} &
else
echo “Cannot start tomcat server”
fi
}

stop()
{
if [ -x ${TOMCAT_STOP} ]; then
echo “Stopping tomcat server…”
${TOMCAT_STOP} &
else
echo “Cannot stop tomcat server”
fi
}

restart()
{
stop
sleep 10
start
}

status()
{
echo “No status available for tomcat server”
}

case “$1” in
‘start’)
start
;;
‘stop’)
stop
;;
‘restart’)
restart
;;
‘status’)
status
;;
*)
echo “Please supply an argument [start|stop|restart]”
esac