09 Jul

1st Post and a Tip Keeping files sync on multiple Linux Servers

Welcome to my blog its been a hot minute since i rambled about something so i leave you a cool system tool/trick of the trade..

Over the years i’ve always had issues with syncing data between multiple webserversand keeping it all in-sync with each other many people would say why not do some type of network storage like NFS/GlusterFS/GFS2 or even S3 but sometimes you just want that fast direct storage speed.  In the past i would just rig up Inotifyd to listen to folder and push the changes using Unison or even Just old Rsync which worked well but i recently came across Csync2 from the fine Folks at Linbit aka DRBD people.  Here’s detail description of what it does under the hood .. ” Csync2 keeps a little database (sqlite as default) which contains the state of each file. This means that whenever it gets invoked, it first updates the database – and only starts to connect to the nodes in case any files were added, modified or deleted. A massive win in the number of connections it needs to make to the nodes, as most of the time there won’t be any new files. And It’s also a lot faster in checking than a Rsync.”  

Installation and configuration

The installation should be easy in most of the Linux distributions, csync2 is included in the repository of Debian, Ubuntu, Fedora, Gentoo and is also available in external repository for Centos and red Hat Enterprise, so in general an install with your package manager should be enough to have it installed.

To have a good starting point for the configuration i suggest to read the linbit paper about csync2, this will give you all the info you need to manage and configure csync2.

But let’s see now what to do once you have the package installed on your nodes, in this examples I’ll use the path of a Debian distribution, if you have a different distribution they could change slightly.

1) Pre-shared Keys

Authentication is performed using the IP addresses and pre-shared-keys in Csync2 . Each synchronization group (a group of hosts that have one or more file in sync) in the config file must have exactly one key record specifying the file containing the preshared-key for this group. It is recommended to use a separate key for each synchronization group and only place a key file on those hosts which actually are members in the corresponding synchronization group.

The key file can be generated with the following command on your first node:

csync2 -k /etc/csync2.key

2) SSL certificate
Next you need to create an SSL certificate for the local Csync2 server. On your first node give these commands:

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) Csync2 configuration file

On your first node create the file /etc/csync2.conf, in this example i want to keep in sync just 1 directory of 2 servers (node1 and node2):

group mycluster
{
        host node1;
        host node2;

        key /etc/csync2.key;

        include /www/htdocs;
        exclude *~ .*;
}

Host lists are specified using the host keyword. You can eighter specify the hosts in a whitespace seperated list or use an extra host statement for
each host. The hostnames used here must be the local hostnames of the cluster nodes.

4) Now copy all the files from the first node (node1) to the other with :

scp /etc/csync2* node2:/etc/

And restart on both nodes inetd (or xinetd if you use it) with the command:

 /etc/init.d/openbsd-inetd restart

5) First Sync

Start synchronization first on node1 then on node2, afther this you can setup a cronjob to do a periodic sync.

csync2 -xv

If you get conflicts or errors use -f option

This setup is enough to have 2 nodes and 1 directory in sync, you’ll have to put on the crontab of both nodes something like this :

*/2 * * * * csync2 -x

 

Actions following a sync

Each synchronization group may have any number of action sections. These action sections are used to specify shell commands which should be
executed after a file is synchronized that matches any of the specified patterns.The exec statement is used to specify the command which should be executed. Note that if multiple files matching the pattern are synced in one run, this command will only be executed once.

The special token %% in the command string is substituted with the list of files which triggered the command execution.

Example:

group g1 {
  host node1 node2;                          # hosts list
  key /etc/csync2.key_g1;                  # pre-shared key

  include /etc/xinetd.d;

  action {                                 
    pattern /etc/xinetd.d;
    exec "/etc/init.d/xinetd restart";
    logfile "/var/log/csync2_action.log";
  }

In this example every time a file in the path /etc/xinetd.d is changed we run the command /etc/init.d/xinetd restart

Common tasks of csync2

These are some common options and tasks that you can use from the command line:

Synchronize

csync2 -x

force local file to be newer (has to be followed by csync2 -x for synchronisation)

csync2 -f filename

Test if everything is in sync with all peers.

csync2 -T

As -T, but print the unified diffs.

csync2 -TT

verbose flag for all commands: -v, i.e.

csync2 -xv

dry-run flag for all commands: -d, i.e.

csync2 -xvd

Conclusions

Csync2 is a great tool if you want to keep filesystems in synchronization asynchronously, there are many other options, like declaring an host as slave only or using or not SSL in the connection between the nodes.

 

Leave a Reply

Your email address will not be published. Required fields are marked *