Search Posts on Binpipe Blog

Installing Lsyncd to Create a Mirror Server on CentOS and Redhat

LSYNCD (Live SYNCing Daemon) is a flexible cross-platform synchronization tool. It uses rsync underneath it. lsync runs as a daemon and continuously runs a sync between the source and the mirror. No cron is required to maintain the sync.


Steps to deploy Lsyncd : (login as root and execute the following steps)

1. Create the password-less logging using ssh keys between the source(server1) & mirror(server2) servers.

# ssh-keygen

Press enter to create key without any passphrase.

A file /root/.ssh/id_rsa.pub will be created on server1

# cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys


Run the same command on server2.

A file /root/.ssh/id_rsa.pub will be created on server2

# cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys


Now open the authorized_keys file in server1 and append the contents in authorized_keys file of server2. Do the same from authorized_keys file in server2 to server1. (Now both the authorized_keys files should be the same with each others keys in them).

Login from both the servers so that the "known hosts" file is updated.

2. # yum install rsync

3. Install the lsyncd from dag wieers repository.   

Add a repo file dag.repo in the /etc/yum.repos.d/ directory with the following contents:

[dag]
name=Dag RPM Repository for Red Hat Enterprise Linux
baseurl=http://apt.sw.be/redhat/el5/en/i386/rpmforge     ## Change i386 to x86_64 if on a 64bit machine
gpgcheck=0
enabled=1

4. # yum --enablerepo=dag -y install lsyncd

5. Create a config file named /etc/lsyncd.conf and Put the following contents in it (change the target source directory & IP:/path according to your requirement):


 settings = {
   logfile    = "/var/log/lsyncd.log",
}

sync{default.rsync, source="/var/www/lsync", target="10.10.10.246:/var/www/lsync", rsyncOps="-rltvu"}


6. Create a log file :

# touch /var/log/lsyncd.log

7. Start the Services:
   # service lsyncd start
   # ckhconfig lsyncd on

That is it! You have got a live mirror server. Now if you want to mirror ''both-ways'' then you can install lsync in the server2 also and repeat the same steps.

Other Examples of lsyncd (Multiple Mirrors):

Below is the lsyncd.conf that duplicates /var/www/html on the master server to the 4 targets (mirrors) with the same path:

settings = {
   delay        = 1,
   maxProcesses = 5,
   statusFile   = "/tmp/lsyncd.status",
   logfile      = "/var/log/lsyncd.log",
}

targetlist = {
 "10.0.1.23:/var/www/html",
 "10.0.1.24:/var/www/html",
 "10.0.1.25:/var/www/html",
 "10.0.1.26:/var/www/html"
}

for _, server in ipairs(targetlist) do
  sync{ default.rsync,
    source="/var/www/html",
    rsyncOpts="-rltvupgo",
    target=server
  }
end
 
 
So use this powerful tool and leave comments if you liked it or need help. 
 

Installing Collabnet SVN on CentOS & Redhat


Collabnet SVN provides a good Web-GUI to do mundane SVN administration via webinterface. Here's a howto to get it installed on CentOS.

Prerequisites:

1)    Java 1.6 JRE/JDK must be installed.

2)    Python 2.4 or higher version must be installed.

3)    Download CollabNetSubversionEdge-2.2.1_linux (32/64 bit)

WARNING:
Note: Do not untar CollabNetSubversionEdge-2.2.1_linux package using root or sudo. This will cause the UID/GID on the files to carry the values from the collabnet build system instead of being reset.
1.      Install JAVA Runtime Environment package from Add/Remove Software (System>Administration>Add/Remove Software) by searching for Java/JRE.
OR
# yum install java-1.6.0-openjdk.x86_64
  2.      After Java installation is over, type the following command in Terminal window.
                    export JAVA_HOME=/usr/lib/jvm/jre-1.6.0-openjdk.x86_64
  And also add the above line to /etc/profile (at the bottom of the file)
  #sudo gedit /etc/profile
  OR
  #sudo vi /etc/profile
       Now you can check the installed java version by the following command.
      # java –version
      Probably this would be the output:
      java version "1.6.0_22"
       OpenJDK Runtime Environment (IcedTea6 1.10.4) (rhel-1.42.1.10.4.el6_2-x86_64)
       OpenJDK 64-Bit Server VM (build 20.0-b11, mixed mode) 

3.      Add any normal (non-root) user to etc/sudoers file or by the following command.

# visudo

And add the following line anywhere in the file.

username  ALL=(ALL)  ALL



4.      Now migrate to non-root (normal) user and select the folder where you want to install CollabNet Subversion Edge. I would recommend you to install the Subversion in the normal user folder. It can be Downloads/ Documents/ Desktop etc.

[root@localhost ]# su – username



[username@localhost]$ mkdir opt/subversion

Note: If you are not able to create folder “subversion” in “opt” directory, then you can create it through “root” or “sudo”.

[username@localhost]$ cd opt/subversion

[username@localhost]$ sudo chmod o+wx /opt/subversion/

Now move the downloaded CollabNetSubversionEdge-2.2.1-linux-x86_64.tar.gz package to opt/subversion.

5.      Now Untar the CollabNetSubversionEdge-2.2.1-linux-x86_64.tar.gz package by the following command.

[username@localhost subversion]$ tar zxf CollabNetSubversionEdge-2.2.1_linux-x86_64.tar.gz  

Note: The above command will create a folder named “csvn”.

[username@localhost subversion]$ cd csvn

Now we will install the application so that subversion will start automatically when the server restarts. For doing this type the following command.

Note: To set up the server so that it starts automatically when rebooted, you will need to have root/sudo access and follow these steps.

Edit the configuration. When a process is started at server startup it runs as root. You do not want the CSVN console to run as root, you want it to run as a specific user -- whatever your login user is.
  1. To do this, edit the file data/conf/csvn.conf.

$ vi data/conf/csvn.conf
  1. Scroll down a little bit until you see #RUN_AS_USER= . Uncomment it out and add your username.

Example:

RUN_AS_USER= username
  1. Save the changes.

[username@localhost csvn]$ sudo –E bin/csvn install

Now we will start the server. Be sure that you are not logged in as root.

[username@localhost csvn]$ bin/csvn start
This will take a few minutes and the script will loop until it sees that the server is running.
Note: If the server does not start, then try starting the server with this command:
 [username@localhost csvn]$ bin/csvn console
 6.      Add the following line to /etc/sysconfig/iptables
  # iptables -A INPUT –p tcp --dport 3343 –j ACCEPT
  Restart firewall to update the changes
  # service iptables restart
  Installation is done, now open the following link:
username: admin 
password: admin
  Note: If you are not able to see your CSVN server on the browser, then just disabled the your system Firewall by entering setup command in the terminal window.
# setup
Or 
Go to System>Administration>Firewall and disable it.
  Note: You can change the IP address of your subversion server with your machine IP address by editing 
 opt/subversion/csvn/data/conf/csvn.conf file (at line no. 62) by the following command.
$ sudo vi data/conf/csvn.conf
  OR
$ sudo gedit  data/conf/csvn.conf
Subversion Edge also starts an SSL-protected version using a self-signed SSL certificate.
You can access the SSL version on this URL:

Directory Hierarchy of CPanel

The following is the directory structure of cpanel. This is generic but can be useful for newbies.

Apache

/usr/local/apache
+ bin- apache binaries are stored here – httpd, apachectl, apxs
+ conf – configuration files – httpd.conf
+ cgi-bin
+ domlogs – domain log files are stored here
+ htdocs
+ include – header files
+ libexec – shared object (.so) files are stored here – libphp4.so,mod_rewrite.so
+ logs – apache logs – access_log, error_log, suexec_log
+ man – apache manual pages
+ proxy -
+ icons -
Cpanel script to restart apache – /scripts/restartsrv_httpd
Start httpd with ssl – /etc/init.d/httpd startssl

DNS – Named(Bind)

Program: /usr/sbin/named
Init Script: /etc/rc.d/init.d/named
/etc/named.conf
db records:/var/named/
/var/log/messages

Exim

Conf : /etc/exim.conf – exim main configuration file
/etc/localdomains – list of domains allowed to relay mail
Log : /var/log/exim_mainlog – incoming/outgoing mails are logged here
/var/log/exim_rejectlog – exim rejected mails are reported here
/var/log/exim_paniclog – exim errors are logged here
Mail queue: /var/spool/exim/input
Cpanel script to restart exim – /scripts/restartsrv_exim
Email forwarders and catchall address file – /etc/valiases/domainname.com
Email filters file – /etc/vfilters/domainname.com
POP user authentication file – /home/username/etc/domainname/passwd
catchall inbox – /home/username/mail/inbox
POP user inbox – /home/username/mail/domainname/popusername/inbox
POP user spambox – /home/username/mail/domainname/popusername/spam
Program : /usr/sbin/exim (suid – -rwsr-xr-x 1 root root )
Init Script: /etc/rc.d/init.d/exim

Mysql

Program : /usr/bin/mysql
Init Script : /etc/rc.d/init.d/mysql
Conf : /etc/my.cnf, /root/.my.cnf
Data directory – /var/lib/mysql – Where all databases are stored.
Database naming convention – username_dbname (eg: john_sales)
Permissions on databases – drwx 2 mysql mysql
Socket file – /var/lib/mysql/mysql.sock, /tmp/ mysql.sock

SSHD

Program :/usr/local/sbin/sshd
Init Script :/etc/rc.d/init.d/sshd
/etc/ssh/sshd_config
Log: /var/log/messages

ProFTPD

Program :/usr/sbin/proftpd
Init Script :/etc/rc.d/init.d/proftpd
Conf: /etc/proftpd.conf
Log: /var/log/messages, /var/log/xferlog
FTP accounts file – /etc/proftpd/username – all ftp accounts for the domain are listed here

Pure-FTPD

Program : /usr/sbin/pure-ftpd
Init Script :/etc/rc.d/init.d/pure-ftpd
Conf: /etc/pure-ftpd.conf
Anonymous ftp document root – /etc/pure-ftpd/ip-address

Perl

Program :/usr/bin/perl
Directory :/usr/lib/perl5/5.6.1/

PHP

Program :/usr/local/bin/php, /usr/bin/php
ini file: /usr/local/lib/php.ini – apache must be restarted after any change to this file

Cpanel

/usr/local/cpanel
+ 3rdparty/ – tools like fantastico, mailman files are located here
+ addons/ – AdvancedGuestBook, phpBB etc
+ base/ – phpmyadmin, squirrelmail, skins, webmail etc
+ bin/ – cpanel binaries
+ cgi-sys/ – cgi files like cgiemail, formmail.cgi, formmail.pl etc
+ logs/ – cpanel access log and error log
+ whostmgr/ – whm related files

WHM

/var/cpanel – whm files
+ bandwidth/ – rrd files of domains
+ username.accts – reseller accounts are listed in this files
+ packages – hosting packages are listed here
+ root.accts – root owned domains are listed here
+ suspended – suspended accounts are listed here
+ users/ – cpanel user file – theme, bwlimit, addon, parked, sub-domains all are listed in this files
+ zonetemplates/ – dns zone template files are taken from here

Important cpanel/whm files

/usr/local/apache/conf/httpd.conf – apache configuration file
/etc/exim.conf – mail server configuration file
/etc/named.conf – name server (named) configuration file
/etc/proftpd.conf – proftpd server configuration file
/etc/pure-ftpd.conf – pure-ftpd server configuration file
/etc/valiases/domainname – catchall and forwarders are set here
/etc/vfilters/domainname – email filters are set here
/etc/userdomains – all domains are listed here – addons, parked,subdomains along with their usernames
/etc/localdomains – exim related file – all domains should be listed here to be able to send mails
/var/cpanel/users/username – cpanel user file
/var/cpanel/cpanel.config – cpanel configuration file ( Tweak Settings )*
/etc/cpbackup-userskip.conf -
/etc/sysconfig/network – Networking Setup*
/etc/hosts -
/var/spool/exim -
/var/spool/cron -
/etc/resolv.conf – Networking Setup–> Resolver Configuration
/etc/nameserverips – Networking Setup–> Nameserver IPs ( FOr resellers togive their nameservers )
/var/cpanel/resellers – For addpkg, etc permissions for resellers.
/etc/chkserv.d – Main >> Service Configuration >> Service Manager *
/var/run/chkservd – Main >> Server Status >> Service Status *
/var/log/dcpumon – top log process
/root/cpanel3-skel – skel directory. Eg: public_ftp, public_html. (AccountFunctions–>Skeleton Directory )*
/etc/wwwacct.conf – account creation defaults file in WHM (Basic cPanel/WHMSetup)*
/etc/cpupdate.conf – Update Config *
/etc/cpbackup.conf – Configure Backup*
/etc/clamav.conf – clamav (antivirus configuration file )
/etc/my.cnf – mysql configuration file
/usr/local/Zend/etc/php.ini OR /usr/local/lib/php.ini – php configuration file
/etc/ips – ip addresses on the server (except the shared ip) (IP Functions–>Show IP Address Usage )*
/etc/ipaddrpool – ip addresses which are free
/etc/ips.dnsmaster – name server ips
/var/cpanel/Counters – To get the counter of each users.
/var/cpanel/bandwidth – To get bandwith usage of domains

Installing Spacewalk (1.5) with PostgreSQL (8.4) on CentOS 6.



SPACEWALK provides software content management, provisioning and monitoring capabilities. It will enable you to kickstart systems, as well as manage and deploy configuration files. Spacewalk's monitoring feature allows you to view monitoring status for your systems alongside their software update status. Spacewalk also has virtualization capabilities to enable you to provision, control, manage, and monitor virtual Xen guests.

It requires either Oracle or Postgresql as database. I used Postgresql because I believe in Open-source :)

Prerequisites need to be configured and only then spacewalk can be installed successfully on CentoOS6, You have to use about 120GB HDD and 4GB of RAM for the buildout, then you can reduce the RAM to 2GB for operation. a CentOS-6 as a minimal install setting with a DNS record for the system name is ideal.

The following steps can be used sourced from here:
 
yum upgrade -y
 
# Install some repos that make the install much easier 
 
 rpm -Uvh http://spacewalk.redhat.com/yum/1.5/RHEL/6/x86_64/spacewalk-repo-1.5-1.el6.noarch.rpm
 rpm -Uvh http://download.fedora.redhat.com/pub/epel/6/i386/epel-release-6-5.noarch.rpm
 rpm -ihv http://spacewalk.redhat.com/yum/1.5-client/RHEL/6/x86_64/spacewalk-client-repo-1.5-1.el6.noarch.rpm
 
# Get your GPG keys for redhat 
 
 yum install -y wget
 wget -O /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release http://www.redhat.com/security/37017186.txt
 rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
 
# Setup to use available java packages (paste into the command line) 
 
 cat > /etc/yum.repos.d/jpackage-generic.repo << EOF 
 
 [jpackage-generic]
 name=JPackage generic
 baseurl=http://mirrors.dotsrc.org/jpackage/5.0/generic/free/
 enabled=1
 gpgcheck=1
 gpgkey=http://www.jpackage.org/jpackage.asc
 EOF
 
# Work on PostgreSQL 8.4 or better 
 
 yum install -y 'postgresql-server > 8.4'
 chkconfig postgresql on
 service postgresql initdb
 service postgresql start
 
# Setup the PostgreSQL DB 
 
 su - postgres -c 'PGPASSWORD=spacepw; createdb spaceschema ; createlang plpgsql spaceschema ; yes $PGPASSWORD | createuser -P -sDR spaceuser'
 
#Setup the security in the file below adding the lines show _before_ the lines that mention 'all' users 
 
 vi /var/lib/pgsql/data/pg_hba.conf
 
# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD
 local   spaceschema     spaceuser                       md5
 host    spaceschema     spaceuser       127.0.0.1/8     md5
 host    spaceschema     spaceuser       ::1/128         md5
 
# Reload the server 
 
 service postgresql reload
 
# Test the connection (exit with \q) 
 
 PGPASSWORD=spacepw psql -a -U spaceuser spaceschema
 PGPASSWORD=spacepw psql -h localhost -a -U spaceuser spaceschema
 
# Make sure you don't have any conflicting packages
 yum remove cobbler-web
 vi /etc/yum.conf and make sure you exclude=cobbler-web
 
# Build the RPM that is missing from CentOS
# This means we need some tools for the build environment and
# specifically, some tools for the RPM (policycoreutils-python) 
 
 yum install rpm-build redhat-rpm-config make gcc policycoreutils-python
 wget ftp://ftp.redhat.com/pub/redhat/linux/enterprise/6Server/en/os/SRPMS/selinux-policy-3.7.19-93.el6.src.rpm
 rpm -ivv selinux-policy-3.7.19-93.el6.src.rpm
 cd ~/rpmbuild
 rpmbuild --rebuild ../selinux-policy-3.7.19-93.el6.src.rpm
 yum localinstall --nogpgcheck /root/rpmbuild/RPMS/noarch/*.rpm
 
# Install Spacewalk 
 
 yum install spacewalk-postgresql
 
Now that spacewalk is installed we will discuss the usage of space walk in upcoming posts. 
 
UPDATE: This document from fedora wiki seems to be more descriptive, refer this as well while installing. 
 

Shrinking Ibdata1 file and using Multiple Table spaces


The ibdata1 file usually grows up to a very large size. The bad new is that the  ibdata1 file can´t actually be shrunk unless you delete all databases, remove the files and reload a dump.

But you can configure MySQL so that each table, including its indexes, is stored as a separate file. In that way ibdata1 will not grow as large.
It was a while ago I did this. However, to setup your server to use separate files for each table you need to change my.cnf in order to enable this:
[mysqld]
innodb_file_per_table


In this regard I found the following excerpt from the Mysql documents very enlightening:


"You can store each InnoDB table and its indexes in its own file. This feature is called “multiple tablespaces” because in effect each table has its own tablespace.
Using multiple tablespaces can be beneficial to users who want to move specific tables to separate physical disks or who wish to restore backups of single tables quickly without interrupting the use of other InnoDB tables.

To enable multiple tablespaces, start the server with the --innodb_file_per_table option. For example, add a line to the [mysqld] section of my.cnf:
[mysqld]
innodb_file_per_table
 
With multiple tablespaces enabled, InnoDB stores each newly created table into its own tbl_name.ibd file in the database directory where the table belongs. This is similar to what the MyISAM storage engine does, but MyISAM divides the table into a tbl_name.MYD data file and an tbl_name.MYI index file. For InnoDB, the data and the indexes are stored together in the .ibd file. The tbl_name.frm file is still created as usual.

You cannot freely move .ibd files between database directories as you can with MyISAM table files. This is because the table definition that is stored in the InnoDB shared tablespace includes the database name, and because InnoDB must preserve the consistency of transaction IDs and log sequence numbers.
If you remove the innodb_file_per_table line from my.cnf and restart the server, InnoDB creates tables inside the shared tablespace files again.

The --innodb_file_per_table option affects only table creation, not access to existing tables. If you start the server with this option, new tables are created using .ibd files, but you can still access tables that exist in the shared tablespace. If you start the server without this option, new tables are created in the shared tablespace, but you can still access any tables that were created using multiple tablespaces.
Note
InnoDB always needs the shared tablespace because it puts its internal data dictionary and undo logs there. The .ibd files are not sufficient for InnoDB to operate.
To move an .ibd file and the associated table from one database to another, use a RENAME TABLE statement:
RENAME TABLE db1.tbl_name TO db2.tbl_name;
 
If you have a “clean” backup of an .ibd file, you can restore it to the MySQL installation from which it originated as follows:
  1. Issue this ALTER TABLE statement to delete the current .ibd file:
    ALTER TABLE tbl_name DISCARD TABLESPACE;
    
  2. Copy the backup .ibd file to the proper database directory.
  3. Issue this ALTER TABLE statement to tell InnoDB to use the new .ibd file for the table:
    ALTER TABLE tbl_name IMPORT TABLESPACE;
    
In this context, a “clean.ibd file backup is one for which the following requirements are satisfied:
  • There are no uncommitted modifications by transactions in the .ibd file.
  • There are no unmerged insert buffer entries in the .ibd file.
  • Purge has removed all delete-marked index records from the .ibd file.
  • mysqld has flushed all modified pages of the .ibd file from the buffer pool to the file.
You can make a clean backup .ibd file using the following method:
  1. Stop all activity from the mysqld server and commit all transactions.
  2. Wait until SHOW ENGINE INNODB STATUS shows that there are no active transactions in the database, and the main thread status of InnoDB is Waiting for server activity. Then you can make a copy of the .ibd file.
Another method for making a clean copy of an .ibd file is to use the commercial InnoDB Hot Backup tool:
  1. Use InnoDB Hot Backup to back up the InnoDB installation.
  2. Start a second mysqld server on the backup and let it clean up the .ibd files in the backup.

Mysqldump All Databases Separately


The below shell script can be put together as a command and made into an executable (chmod+x) and kept in the /usr/bin to be readily available. Make sure to edit the paths and credentials before using.


#!/bin/bash
# Backup each mysql databases into a different file, rather than one big file
# Optionally files can be gzipped (dbname.gz)
#
# Usage: scriptname [ -u username -o output_dir -z ]
#        
#    -u username to connect mysql server
#    -o [output_dir] optional the output directory where to put the files
#    -z gzip enabled
#
# Note: The script will prompt for a password, you cannot specify it as command line argument for security reasons
#

PROG_NAME=$(basename $0)
USER=""
PASSWORD=""
OUTPUTDIR=${PWD}
GZIP_ENABLED=0
GZIP=""

MYSQLDUMP="/usr/bin/mysqldump"
MYSQL="/usr/bin/mysql"

while getopts u:o:z OPTION
do
    case ${OPTION} in
        u) USER=${OPTARG};;
        o) OUTPUTDIR=${OPTARG};;
        z) GZIP_ENABLED=1;;
        ?) echo "Usage: ${PROG_NAME} [ -u username -o output_dir -z ]"
           exit 2;;
    esac
done

if [ "$USER" != '' ]; then

echo "Enter password for" $USER":"
oldmodes=`stty -g`
stty -echo
read PASSWORD
stty $oldmodes

fi

if [ ! -d "$OUTPUTDIR" ]; then
    mkdir -p $OUTPUTDIR
fi

# get a list of databases
databases=`$MYSQL --user=$USER --password=$PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema)"`

# dump each database in turn
for db in $databases; do
    echo $db
    if [ $GZIP_ENABLED == 1 ]; then
        $MYSQLDUMP --force --opt --user=$USER --password=$PASSWORD --databases $db | gzip > "$OUTPUTDIR/$db.gz"
    else
        $MYSQLDUMP --force --opt --user=$USER --password=$PASSWORD --databases $db > "$OUTPUTDIR/$db.sql"
       fi  
done


##EOF

And this one below is a simple one to put in a backup cron:


#!/bin/bash
# backup each mysql db into a different file, rather than one big file
# as with --all-databases - this will make restores easier

USER="root"
PASSWORD="secret"
OUTPUTDIR="/var/lib/bacula"
MYSQLDUMP="/usr/bin/mysqldump"
MYSQL="/usr/bin/mysql"

# clean up any old backups - save space
rm "$OUTPUTDIR/*bak" > /dev/null 2>&1

# get a list of databases
databases=`$MYSQL --user=$USER --password=$PASSWORD \
 -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`

# dump each database in turn
for db in $databases; do
    echo $db
    $MYSQLDUMP --force --opt --user=$USER --password=$PASSWORD \
    --databases $db > "$OUTPUTDIR/$db.bak"
done


  

Mysqldump Single or Multiple Tables of a Database


If we are looking to dump just some tables of a database which have a common text string in their table names we can use this command:


mysql databasename -u [root] -p[password] -e 'show tables like "com_usr_%"' | grep -v Tables_in
| xargs mysqldump [databasename] -u [root] -p[password] > [target_file]
 
It will grep all tables containing com_usr_*” and dump this into the target file.

If we want to dump a just a handful of tables then we can use this easier one:

mysqldump -u{user} -p {database} {table1} {table2} {table3} > target_file.sql 

 And finally a single table dump would simplify to this:

mysqldump -u{user} -p {database} {table} > target_file.sql

 

Mysqldump: Structure Only , Data Only , With Views

For a normal Mysqldump we use the following snippet:

mysqldump -h hostname -u root -pmypassword databasename > dumpfile.sql


For a database dump with 'Views' we use the following:

mysqldump --routines -h hostname -u root -pmypassword databasename > dumpfile.sql

For a dump with Structure Only we use this:

mysqldump -d -h hostname -u root -pmypassword databasename > dumpfile.sql

For a list of INSERT statements (i.e data only) apply this:

 mysqldump  --skip-triggers --compact --no-create-info -h hostname -u root -pmypassword databasename > dumpfile.sql

Note:
–no-create-info skips dumping the create table and view parts for me. Just good plain data:
mysqldump –no-create-info –extended-insert=FALSE
–compact -uUSER -pPASS DBSchema > insert_data.sql
Option –extended-insert=FALSE disable multi value inserts, which results in single value inserts. Each row has it’s own insert statement.

Mysqldump Database Excluding a Table


Many a times you would like to dump a database and move it elsewhere but there is/are a certain table(s) which is very big and is causing the backup/restore a time killing job. In such a case you can invariably resort to this command snippet:
 
mysqldump -u username -ppassword -h localhost --ignore-table=my_db_name.my_table_name my_db_name 


You can use the '--ignore-table' switch multiple times to have multiple tables ignored.

Reset Mysql Root Password

This has happened to me many times in the past, and every time I would google for a solution, so here's the way out borrowed from the Mysql stable :)


If you set a root password previously, but have forgotten it, you can set a new password. The following sections provide instructions for Windows and Unix systems, as well as generic instructions that apply to any system.
Resetting the Root Password: Windows Systems
On Windows, use the following procedure to reset the password for all MySQL root accounts:
  1. Log on to your system as Administrator.
  2. Stop the MySQL server if it is running. For a server that is running as a Windows service, go to the Services manager: From the Start menu, select Control Panel, then Administrative Tools, then Services. Find the MySQL service in the list and stop it.
    If your server is not running as a service, you may need to use the Task Manager to force it to stop.
  3. Create a text file containing the following statements. Replace the password with the password that you want to use.
    UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
    FLUSH PRIVILEGES;
    Write the UPDATE and FLUSH statements each on a single line. The UPDATE statement resets the password for all root accounts, and the FLUSH statement tells the server to reload the grant tables into memory so that it notices the password change.
  4. Save the file. For this example, the file will be named C:\mysql-init.txt.
  5. Open a console window to get to the command prompt: From the Start menu, select Run, then enter cmd as the command to be run.
  6. Start the MySQL server with the special --init-file option (notice that the backslash in the option value is doubled):
    C:\> C:\mysql\bin\mysqld-nt --init-file=C:\\mysql-init.txt
    
    If you installed MySQL to a location other than C:\mysql, adjust the command accordingly.
    The server executes the contents of the file named by the --init-file option at startup, changing each root account password.
    You can also add the --console option to the command if you want server output to appear in the console window rather than in a log file.
    If you installed MySQL using the MySQL Installation Wizard, you may need to specify a --defaults-file option:
    C:\> "C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqld-nt.exe"
             --defaults-file="C:\\Program Files\\MySQL\\MySQL Server 5.0\\my.ini"
             --init-file=C:\\mysql-init.txt
    
    The appropriate --defaults-file setting can be found using the Services Manager: From the Start menu, select Control Panel, then Administrative Tools, then Services. Find the MySQL service in the list, right-click it, and choose the Properties option. The Path to executable field contains the --defaults-file setting.
  7. After the server has started successfully, delete C:\mysql-init.txt.
You should now be able to connect to the MySQL server as root using the new password. Stop the MySQL server, then restart it in normal mode again. If you run the server as a service, start it from the Windows Services window. If you start the server manually, use whatever command you normally use.
Resetting the Root Password: Unix Systems
On Unix, use the following procedure to reset the password for all MySQL root accounts. The instructions assume that you will start the server so that it runs using the Unix login account that you normally use for running the server. For example, if you run the server using the mysql login account, you should log in as mysql before using the instructions. Alternatively, you can log in as root, but in this case you must start mysqld with the --user=mysql option. If you start the server as root without using --user=mysql, the server may create root-owned files in the data directory, such as log files, and these may cause permission-related problems for future server startups. If that happens, you will need to either change the ownership of the files to mysql or remove them.
  1. Log on to your system as the Unix user that the mysqld server runs as (for example, mysql).
  2. Locate the .pid file that contains the server's process ID. The exact location and name of this file depend on your distribution, host name, and configuration. Common locations are /var/lib/mysql/, /var/run/mysqld/, and /usr/local/mysql/data/. Generally, the file name has an extension of .pid and begins with either mysqld or your system's host name.
    You can stop the MySQL server by sending a normal kill (not kill -9) to the mysqld process, using the path name of the .pid file in the following command:
    shell> kill `cat /mysql-data-directory/host_name.pid`
    
    Use backticks (not forward quotation marks) with the cat command. These cause the output of cat to be substituted into the kill command.
  3. Create a text file containing the following statements. Replace the password with the password that you want to use.
    UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
    FLUSH PRIVILEGES;
    Write the UPDATE and FLUSH statements each on a single line. The UPDATE statement resets the password for all root accounts, and the FLUSH statement tells the server to reload the grant tables into memory so that it notices the password change.
  4. Save the file. For this example, the file will be named /home/me/mysql-init. The file contains the password, so it should not be saved where it can be read by other users. If you are not logged in as mysql (the user the server runs as), make sure that the file has permissions that permit mysql to read it.
  5. Start the MySQL server with the special --init-file option:
    shell> mysqld_safe --init-file=/home/me/mysql-init &
    
    The server executes the contents of the file named by the --init-file option at startup, changing each root account password.
  6. After the server has started successfully, delete /home/me/mysql-init.
You should now be able to connect to the MySQL server as root using the new password. Stop the server and restart it normally.
Resetting the Root Password: Generic Instructions
The preceding sections provide password-resetting instructions for Windows and Unix systems. Alternatively, on any platform, you can set the new password using the mysql client (but this approach is less secure):
  1. Stop mysqld and restart it with the --skip-grant-tables option. This enables anyone to connect without a password and with all privileges. Because this is insecure, you might want to use --skip-grant-tables in conjunction with --skip-networking to prevent remote clients from connecting.
  2. Connect to the mysqld server with this command:
    shell> mysql
    
  3. Issue the following statements in the mysql client. Replace the password with the password that you want to use.
    mysql> UPDATE mysql.user SET Password=PASSWORD('MyNewPass')
        ->                   WHERE User='root';
    mysql> FLUSH PRIVILEGES;
    
    The FLUSH statement tells the server to reload the grant tables into memory so that it notices the password change.
You should now be able to connect to the MySQL server as root using the new password. Stop the server, then restart it normally (without the --skip-grant-tables and --skip-networking options).

Command-line Calculator in Linux


I was toying around with the lesser used commands in Linux like the 'cal' (calendar) and then 'dc' (desktop calculator) which doesn't live up to its name and finally 'bc' which really does a good job because of its capability of handling floating point numbers. However, the problem was that its syntax is not straight-forward and doesn't simply run out-of-the-box, the way newbies and laymen would want it to be. So here's a way to create your own calculator command (we call it 'calc') and use it !

We run all the commands as root. Also we assume that the Linux box has 'bc' pre-installed in it. (Most linux distributions have 'bc' pre-installed you can check the path by the command #which bc)

Then follow these steps:
1. Create a little bash script and name it 'calc'. (Use vi / vim / nano / pico/ gedit or whatever you want to toy with)
2. Put the following contents in the script:

#!/bin/sh

bc << EOF
scale=4
$@
quit
EOF

3. Give executable permissions to the script:
#chmod +x calc


4. Move the file to /usr/bin to make it available in the shell as a command:
# mv calc /usr/bin

That's it ! The calculator is ready.

Now you can start doing calculations as in the following examples:


# calc 560 + 990
1550

#calc 10/3
3.33333 (approx)


Command-line is still interesting isn't it ? How many yeps !?

Installing Sugarcrm on Nginx over CentOS / Redhat (LEMP)

 
 


Here is how to get sugarcrm working on Nginx Server. Nginx is a great apache alternative and is much faster in certain terms. It serves php pages with php-fpm and this makes thing much lighter and faster. 

We will get this thing installed in a Centos or Redhat Box, because that is where I had tried it. This will also allow you to learn the basics to create a LEMP (Linux + 'e'Nginx + Mysql + Php) server, contrary to the LAMP (Linux + Apache + Mysql + Php) ones you have been doing all these days. You can use this to host your other php applications also. So lets get started.  

Creating the LEMP Instance:

Launch a clean CentOS / Redhat box and start with instaling Nginx.

CentOS does not offer the latest version of Nginx in its default YUM repository. To install it, you need to add the EPEL (Extra Packages for Enterprise Linux) repository, which is part of the Fedora Project.

1. Install the EPEL repository:
# rpm -Uvh http://download.fedora.redhat.com/pub/epel/5Server/x86_64/epel-release-5-3.noarch.rpm

(Note: This url may change depending upon the Redhat/CentOS version you may be using please do the neccessary corrections accordingly. In case you are stuck ask me via the comments and I'll help you out)

2. Install nginx
# yum install nginx

3. You will be asked to install the gpg-key for EPEL. Answer yes

4. Start Nginx

# /etc/init.d/nginx start

5. Check the installation by going to your web server’s default site, either using your ip address or domain name. And you will see a "Welcome to Nginx" page.




The following are the configuration files for Nginx which we may need later:
  • Default config file: /etc/nginx/nginx.conf
  • Default SSL config file: /etc/nginx/conf.d/ssl.conf
  • Default virtual hosting config file: /etc/nginx/conf.d/virtual.conf
  • Default documentroot: /usr/share/nginx/html

Configure PHP As FastCGI

To install php5 with other modules we will first have to add the correct php repository:
I tried this php 5.2.10 which is available in the Cent-OS Testing repository.
1. To add the CentOS-Testing repository create a file c5-Testing.repo in /etc/yum.repos.d/ and add the following lines in it
 
[c5-testing]
name=CentOS-5 Testing 
baseurl=http://dev.centos.org/centos/$releasever/testing/$basearch/
enabled=0
gpgcheck=1
gpgkey=http://dev.centos.org/centos/RPM-GPG-KEY-CentOS-testing
 
Install php and all other required extensions via this command:  

# yum install php-pear-Net-Socket php-pear php-common php-gd php-devel php php-mbstring php-pear-Mail php-cli php-imap php-snmp php-pdo php-xml php-pear-Auth-SASL php-ldap php-pear-Net-SMTP php-mysql

If some things fail to install use the --enablerepo=c5-Testing switch with yum and you can also exclude php-readline extension by using the switch --exclude with yum because this too causes dependency issues at times.
You can check if php is working fine by using a phpinfo file. Create /usr/share/nginx/html/phptest.php as follows:
 
<?php
     phpinfo();
?>

Hit the url http://your IP or domain/phptest.php  It should show you the php info page.



Now that the webserver is ready we will go ahead and install the Mysql database. And I must say that Mysql installation is pretty easy with yum and we will use these steps:


  1. Install MySQL
    yum install mysql-server mysql php-mysql

Configuring MySQL

  1. Set the MySQL service to start on boot
    chkconfig --levels 235 mysqld on
  2. Start the MySQL service
    service mysqld start
  3. Log into MySQL
    mysql -u root
  4. Set the root user password for all local domains
    SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new-password')
    ;
    SET PASSWORD FOR 'root'@'localhost.localdomain' = PASSWORD('new-password');

    SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('new-password');
  5. Drop the Any user (for security)
    DROP USER ''@'localhost';
    DROP USER ''@'localhost.localdomain';

  6. Exit MySQL
    exit
This will set the Mysql root user password and get things started. Now we are pretty close to install the sugarcrm application over it.

Before installting sugarcrm we will need to make certain changes in the nginx configuration file. 


A sample Nginx configuration for Sugarcrm :-
Step 1: Open the file : /etc/nginx/nginx.conf and enter the following configuration (I just replaced the default nginx.conf with this after backing up the original one):



server {
listen 0.0.0.0:80;
server_name testsugarcrm.com;
gzip off;
set $root_path /usr/share/nginx/html/sugarcrm;

location / {
root $root_path;
index index.php;
error_page 404 = /index.php;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?$1 last;
break;
}
}

location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $root_path$fastcgi_script_name;
}


}

Step 2: Execute the command /etc/init.d/nginx restart in shell.
Step 3: Add the following entry in /etc/hosts file:-
127.0.0.1 testsugarcrm.com



Step 4:  Download suagarcrm comminity edition from here.
Now unzip the package in nginx webroot (/usr/share/nginx/html or whatever it may be in your case) and rename the folder  'sugarcrm'
I tried this exercise with sugarcrm 5.2.0 which I had with me. However, I believe this would work with other versions also.

Now, check the url http://testsugarcrm.com in your browser. This will give you the sugarcrm installer page. Thereafter, the installation is pretty self-explanatory will complete without hiccups :) However you can visit the sugardocs in case you need help.

One last thing I would like to mention here is, some instances which I installed had a strange error of the page going blank after I would press 'Next' in the sugarcrm installation wizard. To resolve this issue, just put the 'display_errors' 'On' in your php.ini file and restart Nginx and rerun the wizard. Now you will see a fatal error about the "Session directory" not getting enough "Permissions" with the directory path mentioned there. You can just go ahead and set the permissions to that directory by the following command:


# chmod -R 777 /usr/share/sessions         (your directory will be differrent, I have just mentioned an arbitrary directory path).

That is it now your sugar is being served by Nginx !

Fun Page

Well, System Administration is a serious job. But Binpipers have fun with their boxes when they find time ! Here's a Fun Page I have created in this site which will have all the funny stuff with Linux / Unix / Servers and other technology grail. You are welcome to read and share such stuff with us. After all " All work and no play makes Jack a dull boy" .. what say ?



Happy Piping ||.|.|

Click to go to the 'FUN' page