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

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

Editing

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

Moving

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

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

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)

PHPUnit 4 released

A few days ago Sebastian Bergmann released version  4.0.0 of PHPUnit. I was expecting more new features due to this major version, but it did not happened. I will describe some of my favourite changes in this new version.

- Implemented #813: Added @before, @after, @beforeClass and @afterClass annotations.

@after
The @after annotation can be used to specify methods that should be called after each test method in a test case class.

@afterClass
The @afterClass annotation can be used to specify static methods that should be called after all test methods in a test class have been run to clean up shared fixtures.

- Implemented #834: Added the @requires OS annotation. ( A regexp matching PHP_OS )

Some considerations before migrating version:

- A test will now fail in strict mode when it uses the @covers annotation and code that is not expected to be covered is executed.

- All relative paths in a configuration file are now resolved relative to that configuration file. When upgrading, you may need to update relative paths for the following configurations: testSuiteLoaderFile, printerFile, testsuites/file, testsuites/exclude.

Changes in PHPUnit_MockObject:

- A more useful error message is now displayed when the stubbing or mocking of a method fails because a class or interface that is used in the method’s signature is not loaded.
- Implemented #108: Improved error reporting when invocation parameters don’t match.

Changes in PHP_CodeCoverage:

- Migrated the HTML report from Bootstrap 2.3 to Bootstrap 3

#1 Challenge – 150 KM running in 30 days

I will start a series of  30 days challenges to improve the quality of life (Life hacking)  and I will comment on the blog my victories and defeats.

Why 30 days?

How to choose a challenge?

The idea is to start with small challenges knowing your own limitations and trying to beat them. Determination is the key to success. I’ll add a reward to help in the hardest moments.

My first challenge is to run 150KM in the gym in 30 days. This will help to have more agility (I’d run a half marathon this year), lose some weight and to have a strong routine to go to the gym. My reward will be a nice apartment in Prague for 2 weeks in June.

Get ready for the challenge

Before you start a challenge, be clear with what you want to achieve and what you need. If you do not have what you need (in this case, a gym, running clothes), you may not be able to complete it and you will be frustrated.

Give thanks for what you are now, and keep fighting for what you want to be tomorrow.

Fernanda Miramontes-Landeros

My First Post

There are many reasons for starting this blog. Firstly as a way to keep track of all my tips, tutorials, code snippets and various web projects. I like to be active in the world of web development and have been knocking out sites and libraries for years.

Secondly I wanted somewhere to document all my trips and experiences around the world and also all my life hacking improvements.

Finally, it will be a convenient place to dump my thoughts on PHP, MySQL, nginx, JS, and the world of computers in general.

So welcome, and thanks for reading.