Lsyncd is tool for automatically live mirroring as daemon. Here are Lsyncd usage examples of Syncing and how to configure the excellent tool. The new users who are not aware what are daemons can read this article. On the guide to Setup Separate Database Server on Rackspace Cloud, we have upgraded from usual one server setup used for developmental works. Forget about Database Server for now. There are multiple ways to use multiple main server to balance the load on individual server, like using Pacemaker Heartbeat for High Availability Cloud, using a fully separate server for WP-Admin and so on. Approaches are varied and tailored to custom need.
Lsyncd is a Live Syncing or Mirroring Daemon, after proper configuration; the tool watches a directory, aggregates and combines events for a few seconds and then spawns process(es) to synchronize the changes. Lsyncd is a light-weight live mirroring tool suitable for advanced usage on server. Official repository lives here :
1 2 3 4 | # goggle code https://code.google.com/p/lsyncd/ # github https://github.com/axkibe/lsyncd |
Lsyncd : Examples of Syncing With Cloud Server
Automation is becoming an important service and we can combine it with Chef Software and OpenStack Cloud, here is cookbook :
---
1 | https://github.com/dgivens/chef-lsyncd |
On Rackspace Deployments, there is ready made blueprint which uses Lsyncd. Only if the reader wants to learn or do manual configuration, these commands are needed on Rackspace. Do not run commands blindly, please read the full article first at least once.
Traditionally we can install lsyncd with the following commands and read the examples provided :
1 2 3 4 5 6 7 8 9 10 11 12 | # We are assuming that you are logged in as root # or has root privilege, do an update if not done recently apt-get update # and install lsyncd apt-get install lsyncd # docs are here cd /usr/share/doc/lsyncd/examples && ls # output lbash.lua lgforce.lua lpostcmd.lua lrsyncssh.lua lecho.lua limagemagic.lua lrsync.lua # open the lrsync.lua file nano lrsync.lua |
The above is very painful way to use lsyncd as an automated service which will run on the master server / main server and copy things to the slave server(s). So practically, we will not use apt-get install lsyncd
command but will wget the latest version (this is what recommended by Rackspace) and build from source :
1 2 3 4 5 6 7 8 9 10 11 12 13 | # We are assuming that you are logged in as root # do an update if not done recently apt-get update # install dependencies apt-get install -y lua5.1 liblua5.1-dev pkg-config rsync asciidoc cd /var/tmp # please check the latest version manually and wget wget http://lsyncd.googlecode.com/files/lsyncd-2.1.5.tar.gz # 150 kb download, do not worry tar -xzvf lsyncd-2.1.5.tar.gz cd lsyncd-2.1.5 export CFLAGS="-march=native -O2" ./configure && make && make install |
While writing the guide, I actually used Mac to test, so the screen shot is of OS X 10.9.x’s local computer not of server. I wget-ed it Safari ( pun intended) to test the link of tar ball, so view this one :
We deliberately used the same method described by Rackspace to avoid inconvenience by Rackspace (for managed) if you are playing for some reason. Only we used different versions. There is no configuration file in the package, we need to create it :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | nano /etc/init/lsyncd.conf # example # copy below this line description "lsyncd file syncronizer" start on (starting network-interface or starting network-manager or starting networking) stop on runlevel [!2345] expect fork respawn respawn limit 10 5 exec /usr/local/bin/lsyncd /etc/lsyncd.lua # stop copying at the above line # symlink to make it auto start ln -s /lib/init/upstart-job /etc/init.d/lsyncd |
We will create a configuration file called “lsyncd.conf.lua” :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | nano /etc/lsyncd.conf.lua # example code after this line taking you are using Ubuntu 14.04 cat << EOF > /etc/lsyncd.lua settings { logfile = "/var/log/lsyncd/lsyncd.log", statusFile = "/var/log/lsyncd/lsyncd-status.log", statusInterval = 20 } sync { default.rsync, source="/var/www/html", target="your.rackspace.service-net.ip.address:/var/www/html", rsync = { compress = true, acls = true, verbose = true, rsh = "/usr/bin/ssh -p 22 -o StrictHostKeyChecking=no" } } EOF |
Lua is Language :
1 | http://www.lua.org/start.html |
We need to create one more file for Log Rotation at /etc/logrotate.d/lsyncd
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | nano /etc/logrotate.d/lsyncd # copy after this line cat << EOF > /etc/logrotate.d/lsyncd /var/log/lsyncd/*log { missingok notifempty sharedscripts postrotate if [ -f /var/lock/lsyncd ]; then /sbin/service lsyncd restart > /dev/null 2>/dev/null || true fi endscript } EOF |
Start the service :
1 2 3 4 5 | start lsyncd # may be you want to reboot softly reboot # touch it to make working touch /var/www/html/* |
But, understand why your other server will accept files? It is the same situation like we had to do for SSH to Server Without Entering Password From Mac (OS X). So, practically do it in cool head :
1 2 | # on main server ssh-keygen -t rya |
Frankly, working on command line is pathetic, login to the main server using FileZilla, cd to ~/.ssh/ id_rsa, copy id_rsa.pub
to your local computer. Again, open another session on Filezilla to login to the slave server, upload the file /root/.ssh/
and change the name to master.pub
– Slave will have Master pub file on root. SSH to slave server, and run this :
1 | cat ~/.ssh/master.pub >> ~/.ssh/authorized_keys |
Now, exit from all sessions. Freshly open one terminal (to avoid human error due to complex steps), ssh to the master server. From the master server, ssh to the slave server – you will get the prompt to accept key, type yes and hit enter.
Lsyncd : Examples of Syncing With Cloud Server with Local Computer
Basic principle is same but the configuration of Log Rotation and other configuration will change slightly as probably most of us do not use our computers as servers!
Tagged With example lsync d conf , lsyncd autostart , Lsyncd Manual , lsyncd version 2 1 5 configuration file examples , paperuri:(cdd46801cf929c62b8cdb0e0db95d61c)