Monday, December 9, 2013

Real Time Synchronization in Debian Squeeze


1. First on both machines install openbsd & csync2 by entering the following command:

    # apt-get install openbsd-inetd csync2
    # apt-get install openssl ssl-cert
2.Authentication is performed using the IP addresses and pre-shared-keys in Csync2:
    # csync2 -k /etc/csync2.key
    # openssl genrsa -out /etc/csync2_ssl_key.pem 1024
    # openssl req -batch -new -key /etc/csync2_ssl_key.pem -out /etc/csync2_ssl_cert.csr
    # openssl x509 -req -days 3600 -in /etc/csync2_ssl_cert.csr -signkey /etc/csync2_ssl_key.pem -out /etc/csync2_ssl_cert.pem
   
3. Now we will configure csync2 through /etc/csync2.cfg file:
    # vim /etc/csync2.cfg

In vim editor the file will look like this....(here sync will done locally...so we use no ssl parameter...)

-----------------------------------------

ignore uid gid mod;
nossl * *;
group mycluster
{
        host web1;
        host web2;
        key /etc/csync2.key;
        include /var/www;
        exclude *~ .*;
}

------------------------------------------

4. Now copy all the files from the first node (web1) to the other node (web2) :
    # scp /etc/csync2* web2:/etc/    [Here web2 is another host]


5. Restart on both nodes inetd (or xinetd if you use it) with the command:

    # /etc/init.d/openbsd-inetd restart


6. Final Step: (to synchronize data on the node.)
    # csync2 -xv or
    # csync2 -x


====================================================================
Linux incrond inotify: Monitor Directories For Changes And Take Action (Only on first node or master node)
====================================================================
1. Install incron:
    # apt-get install incron

2. Modify the Main incron configuration file to specify the editor option
    # vim /etc/incron.conf
---------------------------------------------------------
uncomment the last line and replace vim instead of nano
---------------------------------------------------------

3. Enable user to edit incrontab: (suppose we enable root & mehedi user)
    # vim /etc/incron.allow

------------
root
mehedi

------------
4. How Do I Run csync2 Command To Replicate Files For /var/www/ Directory?

    ~ Type the following command:
    # incrontab -e


Append the following line:
===================================================================
/var/www/ IN_CREATE,IN_DELETE,IN_MODIFY,IN_MOVED_FROM,IN_MOVED_TO,IN_CLOSE_WRITE /usr/sbin/csync2 -xv
===================================================================

Now, wherever files are uploaded, modified or deleted in /var/www/ directory,
csync2 will be executed to sync files to the other server's /var/www/ directory.



Example-2 (This command will work in the upcoming version not current version)
---------------------------------------------------------------------------------------------------

This command is to check Subdirectories Recursively.
===========================================================
find /var/www/ -type d -print0 | xargs -0 -I{} echo "{} IN_CLOSE_WRITE,IN_CREATE,IN_DELETE /usr/sbin/csync2 -xv " > /etc/incron.d/recursive.conf
===========================================================

Above command will create a file named recursive.conf in /etc/incron.d/ directory.
And the folders listed in the file will rsync automatically.





5. Finally restart the service....

    # /etc/init.d/incron restart


incron Syntax:
~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<directory> <file change mask> <command or action>  options
/var/www/html IN_CREATE /root/scripts/backup.sh
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Use the following file change masks:
-----------------------------------

    IN_ACCESS - File was accessed (read)
    IN_ATTRIB - Metadata changed (permissions, timestamps, extended attributes, etc.)
    IN_CLOSE_WRITE - File opened for writing was closed
    IN_CLOSE_NOWRITE - File not opened for writing was closed
    IN_CREATE - File/directory created in watched directory
    IN_DELETE - File/directory deleted from watched directory
    IN_DELETE_SELF - Watched file/directory was itself deleted
    IN_MODIFY - File was modified
    IN_MOVE_SELF - Watched file/directory was itself moved
    IN_MOVED_FROM - File moved out of watched directory
    IN_MOVED_TO - File moved into watched directory
    IN_OPEN - File was opened
    The IN_ALL_EVENTS symbol is defined as a bit mask of all of the above events.



Important:
~~~~~~~~~
Configuration Files

    /etc/incron.conf - Main incron configuration file
    /etc/incron.d/ - This directory is examined by incrond for system table files.
    /etc/incron.allow - This file contains users allowed to use incron.
    /etc/incron.deny - This file contains users denied to use incron.
    /var/spool/incron - This directory is examined by incrond for user table files.



How Do I Troubleshoot Problems?
-----------------------------------------

You need to see /var/log/cron log file:
    # tail -f /var/log/cron
    # grep something /var/log/cron

No comments:

Post a Comment