Recursive find and replace

From time to time I find myself wanting to replace a bit of text, in multiple files, throughout a huge directory tree (particularly in the case of a WordPress migration).

Here’s a method I frequently use (other examples I’ve seen out there make use of perl, xargs, grep, etc.):

user@host:~$ cd Directory_To_Start_From
user@host:~$ find . -type f -exec sed -i 's/Text_To_Find/Replacement_Text/g' {} ;

The find command will return a list of all the files in the directory tree and execute the sed command on each one it locates. In the example below, I added the “-name” option to the find command to allow for working only on files with the .js extension:

user@host:~$ cd website
user@host:~$ find . -type f -name "*.js" -exec sed -i 's/var pageName="example page";/var pageName="Final Page";/g' {} ;

In the next one, the text to find and replace has special characters (in this case the ‘/’ in the URL).

user@host:~$ cd website
user@host:~$ find . -type f -exec sed -i 's/http://www.example.com/old_directory//http://newsubdomain.example.com/new_directory//g' {} ;

See the following links to read more about escaping special characters:

Sloppiness at 1and1

So I have been noticing issues with 1and1 again. Yesterday when trying to get into my hosting control panel at admin.1and1.com, I received a “HTTP Error 503 Service unavailable” error, and thought to myself… really, on the admin panel… really?

Today while checking out my own site, I noticed that all of the PHP files I tried browsing to returned “500 Internal Server Error” errors.

Now this has gone too far… so I called them up. At first the person I talked to explained that I needed to force PHP5 using a .htaccess file, and proceeded to tell me that they could make the change for me. Calmly, I responded by saying that I did not want a change, and that it had been working fine the way it was for months.

She put me on hold, and while I waited… it started working. She told me that the shared hosting server “had a glitch”.

Afterwards, I ssh’ed into my server. Look what I found:

(uiserver):uXXXXXXXX:~ > ls -al
total XX
-rw-r--r--  1 uXXXXXXXX XXXXXXXX   16 Feb  3 14:06 info.php

I thought, “weird, I didn’t put that there”.

(uiserver):uXXXXXXXX:~ > cat info.php 
<? phpinfo(); ?>

Shame on you 1and1, for not cleaning up after yourself. Well, I guess you get what you pay for.

Databases on 1and1

After wanting to test WordPress 3.0 on a 1and1 hosting account, I ran into the following issue… the particular setup I was dealing with had only one database available, and it wasn’t MySQL 5, but instead MySQL 4… furthermore, it was a version lower than 4.1.2, which more recent versions of WordPress require as a minimum.

So after doing a bit of research on what others had done, I went about fixing the problem. Interestingly enough, 1and1 could have made some money in this situation if they would allow customers to simply purchase another database to add to their packages, but no, they want an entire upgrade to be purchased.

The first step was to backup the website and database. I ssh’ed into the web host:

username@localhostname:~$ ssh $username@example.com

Then I backed up the database. The command below will dump all of the databases on the host (in this case, only one MySQL 4.x database) into a dated bzip file, in the home directory.

username@remotehostname:~$ mysqldump -C -A -u $databaseUsername -h $databaseHostName -p$databasePassword | bzip2 -cq9 &gt; ~/`date +%F-%I-%M-%p`-db-backup.sql.bz2

Now to backup the site itself (not only in case disaster struck, but this would get a local copy of the sql dump I just made as well). On a local OS X workstation (or Ubuntu, if that suits your tastes) I ran something similar to the following:

username@localhostname:~$ mkdir ~/website_backups
username@localhostname:~$ rsync -avz --exclude="logs" $username@example.com: ~/website_backups/

From there, I was able to log into the 1and1 control panel and delete the existing database. This allowed me to setup a new one, and in particular, choose MySQL 4 or 5 as the type.

Back to the 1and1 hosting account:

username@localhostname:~$ ssh $username@example.com

I uncompressed the sql dump:

username@remotehostname:~$ bunzip2 2010-01-14-01-59-PM-db-backup.sql.bz2

and was greeted with the raw sql in the file, “2010-01-14-01-59-PM-db-backup.sql”. From here it was only a hop skip and a jump away to restoration. It was necessary to edit the sql file in order to have it restore properly to the newly created database that was just created:

username@remotehostname:~$ nano -w 2010-01-14-01-59-PM-db-backup.sql

And I changed the $oldDatabaseName to $newDatabaseName.

--
-- Current Database: `$oldDatabaseName`
--
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `$oldDatabaseName`;
USE `$oldDatabaseName`;

ctrl+o, enter, and ctrl+x, to save the file and exit nano. The sql dump was now ready to restore to the new database.

username@remotehostname:~$ mysql -u $databaseUsername -h $databaseHostName -p$databasePassword $databaseName &lt; 2010-01-14-01-59-PM-db-backup.sql

The only other things to do were to update any existing applications that needed the new database name, username, hostname, and password.

As a note, if you are updating a WordPress install to point to a new database, this info can be changed in the file, wp-config.php.

1and1 Horror Story

Our host (1and1) has finally decided it was time to deliver a backup of our site from a date before they had issues with their servers. The email came in at 1:32PM EDT explaining it was available.

The short of the whole story is (yes, it is much longer than this):

1) They had hardware troubles.

2) They put the filesystem hosting our website as read-only for about 48 hours as they checked it for errors (meaning nothing changes until they are done).

3) They refused to put our site on a different server. I could only contact technical support, not the actual admins, and they kept telling me to email complaints@1and1.com. They said it was not their call to put our account on another server. One of their technical support representatives admitted that if I went to their website and resigned up (a.k.a. pay more cash), that I could get service.

4) After the filesystem check was done, and our site was again read/write… several of their technical support representatives refused to admit that our site was having troubles, or that it was their fault… Until Denise showed them missing pictures, failing links, and corrupt PHP code.

5) Several of their technical support representatives said that they would email us with status updates. We received 0 emails until we called and spoke with a manager saying that we have received nothing in our inbox. We then received 2 emails regarding our “missing images” and during one of our calls to them, a technical support representative requested that we “cooperate with him” and email him the missing pictures from our site so that he could fix it for us… (you’d think I’d know how to upload my own pictures already, seeing how I put this whole thing together, huh??? And what about the corrupt PHP code and failing links?)

6) Two different people on their technical support team refused to admit that there was a problem with our site even though it was obviously malfunctioning…

7) After 6 more calls to the Philippines (nothing against the Philippines), regarding the corrupt and deleted files, a manager finally admitted that during filesystem checks, files can become corrupt and/or deleted (I already knew this). Compensation for service I paid for, that I did not get??? The manager told me to email complaints@1and1.com.

I had already done this from before, and we still have not heard back from them. Next stop… Billing department. ;-)

I have two quotes from friends that explained it very well:

1) You get what you pay for.

2) When competing on price, something has got to give.

Well said.