Friday, June 5, 2009

find and perl as alternative for sed

In continuation of my migration, some directories with HTML contents their are manually inputted by strings that would let a typical user to edit manually all the HTML files for just a sake of changing a text. However, Perl as an alternative for sed is quite helpful.

The command goes like this:

perl -e "s/www\.hawaiifsbos\.com/test\.hawaiifsbos\.com/g;" -pi $(find . -type f)

-p = will do a loop sequence. For more info, read the page in "Using the -n and -p option"
-i = will modify file in-place. providing after the "i" letter with a word, will be used as a back-up suffix of a file. (Ex. -pi.bak)
-e = means execute

This is the same thing as

find . -name "*.*" -type f -exec sed -i 's/' {} \;

The above works in my Slack, but it seems weird it doesn't do in FreeBSD, but the perl is there as my alternative.

rsync: avoid overwriting of files

I was migrating this stuffs we have from an old FreeBSD 4.0 Server to a new FreeBSD 6.0 Server hosted at Verio. I was fumbled and tumbled when I use this rsync tool by migrating the files to our new server.

The situation is that, in every directories we have being assigned in a virtual host and its DocumentRoot are having different owners and group assigned. The old server we have is having a version of an old Apache. Now, in order to achieve the exact ruling or permission, rsync is a tool that enables you to preserve ownership as well as its permission of the files/directories that you're going to copy--or shall we use the term "migrate".

My mistake was that, migrating with files using rsync is a little bit tricky. Ending a copy like this, for example:

rsync -r -e "ssh -l ahkmed" localhost:/home/ahkmed/documents /www/documents

is a different story than

rsync -r -e "ssh -l ahkmed" localhost:/home/ahkmed/documents/ /www/documents

Note the ending has slash in it while the first one has no slash in it at "/home/ahkmed/documents/".

The first one will copy the "documents" folder and its contents to /www/documents. So this will result into /www/documents/documents. If you're intending to grab the "documents" folder and put it in /www/documents only the contents of that "documents" folder, you need to use the second rsync command that I have shown above, i.e. with trailing slash (/) at the end.

You should really be aware of the differences between the two because you might end up fumble and tumble as what I did. The story goes like this. I have the files in /usr/local/apache/htdocs from an old server and I made the stupid thing when I issue like:

rsync -r --delete -e "ssh -l ahkmed" ../

where the current directory I am in the new server is at /usr/local/apache2/htdocs/. This means that, delete all the files if not existent at /usr/local/apache2/in the new server. Dang, it really works deleting it, but it's a mistake! However, I do make backups of course.

Now, here's another stuff what I learned from rsync also, you can do filter files like you don't want to be deleted or updated.

rsync -o -p -g -v -r -a -u --update --exclude={camping,journey,files.txt} -e "ssh -l ahkmed" localhost:/home/ahkmed/Desktop/pics .

-u = will tell that the receiver has the updated file
--exclude = will do a filter, excluding a directory name (whether it's a subdirectory of a directory). Appending lots of filters, I suggest to you --exclude-file=FILE_FILTERS.

This prevents an rsync copy that has contents been modified and also a directory that you don't want to update anymore, somewhat like the subdirectory is a final copy, while others needs to be copied if newer files has been copied.