Using find and sed to update paths

Sometimes, we need to migrate a site to another server, with different directory layout, i.e. In our original server we had “/var/www/mysite/htdocs” and in the new server “/home/mysite/public_html”

There are some programmers that are used to hardcode paths inside the applications, which could represent an issue if we don’t know how to update them in an easy way, without having to open files one by one.

For doing this, I suggest to make a backup first, this can be done by doing:

cp -ar /home/mysite/public_html /home/mysite/public_html.orig

Then, we need to locate the corresponding files in our document root, so let’s start steping in.

I asume you have transferred all the files from the original server to the new one, perhaps using rsync, scp, ftp, or something like that. So let’s start:

1) cd /home/mysite/public_html

2) Now we want to locate all the php files in all the folders behind this point in the tree, for doing this, we can do:

find -name “*.php”

You will get a list of php files. Those are the files we want to change, in case they have the paths hardcoded.

Now, would be interesting to search the string and replace it with the correct one in a single command, hu?

find -name “*.php” -exec sed -i s/”\/var\/www\/mysite\/htdocs”/”\/home\/mysite\/public_html”/g “{}” \;

that’s it =)

It will automagically search & replaceĀ  recursively the first string with the second one for all the php files in your document root, that’s what we needed.

You can find more information about sed and find consulting their man pages (man sed and man find)

Comments

Use rsync for backups,migrations and mirroring

Usually, we need to make backup of sites, copy files and folders from one server to another, or sync files between two versions of the same site (mirroring).

To accomplish all this tasks, we can use rsync through ssh. For a detailed explanation of the rsync application, please run “man rsync“.

How to transfer files and folders from one location to another, recursively, and saving bandwidth using rsync?

rsync takes basically 3 arguments:

1. Options
2. SOURCE
3. DESTINATION

Click to continue reading “Use rsync for backups,migrations and mirroring”

Comments

MySQL logs for debugging applications

Sometimes we have to face annoying issues with applications that communicate with MySQL (to authenticate users, get configuration settings, save data, etc). On some cases, these applications are written in a language which we are not familiar with, or perhaps it’s just not our job to “fix” them in case of any issue with the programming code.

However, we are asked to get them installed, upgraded, migrated, etc. And for some reason it just does not work. Users are not recognized, settings are not updated, and the application does not report any error.

In those cases, the best thing we can do (rather than quitting and letting the customer call the developer) is to analize the communication between the application and the MySQL Database.

How do we do this? we have two options for checking the mysql general query log.

Click to continue reading “MySQL logs for debugging applications”

Comments

Bash-based simple remote monitoring tool

I’ve developed this script with the intention to have a quick-and-dirty custom remote monitoring tool.

It takes the services from a local file (called monitor-remote.conf)

which would have this format:
[servername]:[ipaddress]:[servicename]:[port]:[protocol]
one per line.

it checks if it’s up and listening for connections, trying to open a connection using netcat

If the server where this script is running, and the monitored one has a valid ssh shared-key implementation, it will try to restart the service automatically.

Click to continue reading “Bash-based simple remote monitoring tool”

Comments






  • Services

  • My Posts

  • Contact


  • Advertisement

  • About Juan

  • Projects

  • Job Advertisements

  • Archives

  • Meta

    Sitemap
    Entries RSS
    Comments RSS