Sync folders between servers

In this small tutorial I will explain how you can have folders synchronised between different computers using lsyncd for MacOsX

What is lsyncd?

When managing a web server or an application, there are many situations that require synchronization between directories. While a number of tools can help you accomplish this, lsyncd is a strong candidate.

This article will discuss how to configure lsyncd to mirror changes between directories on a single machine and how to mirror between remote hosts.

For this guide, we will be using an OSX 10.9.2, but any modern distribution should function in a similar way.

How to Install lsyncd

Fortunately, Brew repo for OSX includes lsyncd.

We can install lsyncd with the following commands:

This will install lsync, but it will not provide you with a default configuration. We will create one later in the article.

How to Find lsyncd Configuration Examples

While lsyncd does not provide a configuration file by default, it does include some examples that we can look at to get ideas.

See the examples by checking out the files in the “/usr/local/share/doc/lsyncd” directory:

You can look at these text files to get a sense of what can be done with configuration.

The lsync configuration file is written in the Lua programming language. You can learn more about Lua here.

Setting Up the Environment

We can create a log directory and some files for lsyncd to use:

Next, we can create the lsyncd configuration directory:

We will create a configuration file inside of this directory called “lsyncd.conf.lua”, with vim:

How to Configure Remote Syncing with lsyncd

With a few changes to our configuration file, we can configure remote syncing.

First we need to be able to sign into the mirroring machine from the original machine through password-less ssh. How to Log Into the Remote Machine with SSH Keys

Make a key pair for your root user, since the lsyncd service will run as root. You can then copy the key file to the remote mirror machine with the following commands:

Your key file should now allow you to log into the remote mirroring server as the root user.

Try it now so to test that it works and to create a destination directory on the remote host:

We can open the lsyncd configuration file again with the following command:

Save the file and exit.

Exclude directories

Sometimes you will want to exclude some directories. You can create the file lsyncd.dev.exclude and add exclusions per line like this example:

Testing Remote Syncing

We can now start the lsyncd service by issuing the following command:

If we ssh into our remote mirror, we should be able to see the changes in the remote “/var/www/lighttpd” directory:

You should see all of the files that we added to the local “/Users/cesar/PhpstormProjects/YourProject” directory.

Taking it Further

The lsyncd service is a great way to synchronize files across directories or systems. Due to its Lua-based configuration files, it can be very powerful and flexible. The Lua documentation and the lsync documentation are great resources that will allow you to develop more complex syncing operations.

Check out some of the other examples in “/usr/local/share/doc/lsyncd” directory for more ideas.

More info: Official github documentation

Useful Vim (vi) commands

Basic

[table]command, action
ESC, Leave edit mode and return to command mode
Cntl-F, Foreward (down) one page
Cntl-B, Back (up) one page
Cntl-L, Refresh page
G, Move cursor at the end of the file
1G or gg, Move cursor at the beginning of the file
$, Move cursor at the end of the line
0, (zero) Move cursor at the beginning of the line[/table]

Editing

[table]command, action
a, Insert text after the cursor [TEXT]
i, Insert text at the current cursor position [TEXT]
A, Insert text at the end of the line [TEXT]
I, Insert text at the beginning of the line [TEXT]
o, Add a line below the cursor [TEXT]
O, Add a line above the cursor [TEXT]
u, Undo the last change
:red, Redo the undone changes with ”u” or ”:u”
x, Remove a character (and automatically put in the vim-clipboard)
D, Remove the rest of the line (after the cursor)
dw, Remove a word (until the next white blank space)
dd, Remove the line
8x, Remove 8 characters
2dw, Remove 2 words
7dd, Remove 7 lines
p, Insert the content of the vim-clipboard after the current cursor
P, Insert the content of the vim-clipboard before the current cursor
s, Replace a character for text [TEXT]
15s, Replace 15 characters for text [TEXT]
r, Replace a single character for other one without enter in text editor mode
R, Overwrite [TEXT]
J, Join the line before and the current line
i[ENTER], Break a line in two
., Repeat the last change done
yy, Insert the current line in the vim-clipboard
5yy, Insert five lines in the vim-clipboard
:w, Save in the file the changes made
:wq, Save and exit
:q, Exit without save changes
:q!, Force exit without save changes
[/table]

Moving

[table]command, action
+, Go to the next line
-, Go to the previous line
:+8, Go 8 lines down
:-9, Go 9 lines up
:6, Go to line number 6
ma, Mark a line with the tag ‘a’. (Only can use one letter)
‘a, Go to the line with the tag ‘a”.

Searching

[table]command, action
/text/, Search from where there cursor is the word “text”.
?text?, Search backward the word “text”.
n, Continue searching in the chosen direction.
N, Continue searching in the opposite direction.
/i.ad/, Search iXad where X is any character
/i..ad/, Search iXXad where XX are two any characteres
:set ic, Case insensitive for search and replaces
:set noic, Case sensitive[/table]

Vagrant environment for Symfony2

If you’re a developer, A virtual machine can isolate dependencies and their configuration within a single disposable, consistent environment, without sacrificing any of the tools you’re used to working with (editors, browsers, debuggers, etc.).

For this reason I’m going to explain how to setup a vagrant vm to work with your Symfony2 project.

What is Vagrant?

Vagrant is free and open-source software for creating and configuring virtual development environments. It can be considered a wrapper around virtualization software such as VirtualBox and configuration management software such as Chef, Salt and Puppet.

Installation

Once you have imported the project as a submodule (or just clone it if you dont want to add it as a submodule)

The first time can take a couple of minutes because you will download the operative system ISO for the VM.
When the VM is done setting up, point your browser towards http://192.168.33.10 and there you have it: Symfony2.

Setting sshd timeout

Here’s something that I usually forget to change from the default and then get annoyed when my terminal hangs.

As root open your sshd_config file in an editor.

Add the following lines:

Reload the sshd daemon.

Done. That will keep you logged in for 1 hour with no activity. (1200 seconds x 3)