How to move a large database from one Linux server to another

Suppose you have a virtual server with a massive database that you need to move from one Linux server to another. Let’s also assume that you don’t have copious amounts of bandwidth at your disposal to download and upload the SQL files. I have found that the simplest method to move large files between two servers, is by using an application called rsync.

rsync is a software application and network protocol for Unix, Linux and Windows systems which synchronizes files and directories from one location to another while minimizing data transfer using delta encoding when appropriate.

Simply follow the steps below to transfer your database to a new Linux server.

 

1. Backup your database.

mysqldump -u root -p database_name > dumpfilename.sql

Once complete, type “ls -al” to view the details of your SQL file for interest’s sake.

ls -al

2. Compress your file.

gzip dumpfilename.sql

Take a look at this site for more information on compressing files in Linux

3. Install rsync on BOTH servers.

sudo yum install rsync

4. Transfer the file to your new server.

rsync -v -e ssh dumpfilename.sql.gz root@ipaddress_of_your_server:~

“dumpfilename.sql.gz” represents the zipped file that we just compressed using step 2 above.
“~” represents the home directory.
“root” represents your SSH username
“ipaddress_of_your_server” represents the, you guessed it, IP address of your server.

5. Decompress the file on the new server

gzip -d dumpfilename.sql.gz

6. Create your mysql database on the new server
Remember to keep the name the same as the old server’s database as we will be attempting to import it to a database with the same name.

7. Import your SQL file

mysql -u root -p  dbname < dumpfilename.sql

Your database move is complete!

Let me know if you need help or get stuck by using the comment section below.

Cache certain areas of your page using PHP

Sometimes it’s necessary to cache only certain elements or areas of your dynamic website to speed up the load times. I recently needed to cache two DIV’s that were being dynamically generated on one of my websites.

Sometimes it’s necessary to cache only certain elements or areas of your dynamic page to speed up the load times. I recently needed to cache two DIV’s that were being dynamically generated on one of my websites. They were each taking about 10 seconds to load which as you know, can be detrimental to your SEO efforts. Here’s the steps I followed in order to reduce my page load times from 20 seconds to under 1.5 seconds.

Step 1: Separate the areas

First of all, you would need to make the areas you would want to cache separate from the page. You can do this by creating new files for these areas and using the “include (‘page_name.php’);” function. This allows us to better control that specific file for caching purposes.

Code:

<div id="pane_popular">
<? 
      // this is one of the divs we want to cache
      include('div_top_articles.php'); 
?>
</div>

Step 2: Create the cache directory
Create a directory called “cache” on your web server. We will reference this in step 3.

Step 3: Insert the PHP cache code

Insert the following script at the top of the included file (in this case it’s the ‘div_top_articles.php’ file).

Code:

<?
    $cachedir = 'cache/'; // Cache directory
    $cachetime = 600; // Seconds to cache files for
    $cacheext = 'cache'; // Extension to give cached files (usually cache, htm, txt)
 
    // Script
    $page = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $cachefile = $cachedir.md5($page).'.'.$cacheext; // Cache file to either load or create
 
    $cachefile_created = ((@file_exists($cachefile))) ? @filemtime($cachefile) : 0;
    @clearstatcache();
 
    // Show file from cache if still valid
    if (time() - $cachetime < $cachefile_created) {
 
        //ob_start('ob_gzhandler');
        @readfile($cachefile);
        //ob_end_flush();
 
    }
    else {
    // If we're still here, we need to generate a cache file
 
        ob_start();
 
        // <CONTENT THAT YOU WANT CACHED-->
        // ----------------------------------------------------
        // ----------------------------------------------------
 
        show_latest_articles(10,"latest");
        echo "<small><small>Cached on: ".date("Y-m-d H:i:s")."</small></small>";
 
        // ----------------------------------------------------
        // ----------------------------------------------------
        // 
 
        // Now the script has run, generate a new cache file
        $fp = @fopen($cachefile, 'w'); 
 
        // save the contents of output buffer to the file
        @fwrite($fp, ob_get_contents());
        @fclose($fp); 
 
        ob_end_flush();
    }
 
?>

Code thanks to AddedBytes, modified slightly.

That’s all there is to it.

Redirect all pages of a domain except for one page with .htaccess

I had a really tough time this morning trying to figure out how to redirect all pages of one domain to another domain, but stop one (or two) of the pages on the old domain from redirecting.

The solution is as follows:

 
RewriteEngine on
#Note: do not include a "/" in the beginning of the path, this is automatic
RewriteCond $1 !^enquiry_control/enquiry4.php
RewriteCond $1 !^enquiry_control/capt.php
RewriteRule (.*) http://talooma.com/$1 [R=301,L]

Yup, its as easy as that – typical.

WordPress 3 error: Warning: Cannot modify header information

This error message has the ability to drive some developers completely insane. However, the solution to this error is A LOT easier than you think.

This error message has the ability to drive some developers completely insane. However, the solution to this error is A LOT easier than you think.

The error:

Wordpress 3 error - Cannot modify header information

What to do

  • Identify the file where the error originates from. This is stated in the actual error. In this case it is “functions.php”, not “theme-editor.php”
  • Remove the white spaces before the “<?php” or “<?” tags as well as after the “?>” tag.

Believe it or not, you are done. Easy as pie.

Extracting a domain name with PHP and Regular Expressions

As most of your know, regular expressions can be a nightmare if you don’t know much about the subject. Here is a quick tutorial on how you can extract the domain name from any URL using regular expressions and PHP. This includes http:// and https:// domain names.

 
<?php 	
$link1 = "http://nickduncan.co.za/";
$link2 = "http://nickduncan.co.za";
$link3 = "http://www.nickduncan.co.za/";
$link4 = "http://www.nickduncan.co.za";
 
$link5 = "https://nickduncan.co.za/";
$link6 = "https://nickduncan.co.za";
$link7 = "https://www.nickduncan.co.za/";
$link8 = "https://www.nickduncan.co.za";
 
$link9 = "http://www.nickduncan.co.za/index.php";
$link10 = "http://www.nickduncan.co.za/index.php?id=34&that=this";
$link11 = "http://nickduncan.co.za/php-header-include-%E2%80%93-saving-development-time/";
 
echo return_domain($link1)." - ".$link1;
echo "<br />".return_domain($link2)." - ".$link2;
echo "<br />".return_domain($link3)." - ".$link3;
echo "<br />".return_domain($link4)." - ".$link4;
echo "<br />".return_domain($link5)." - ".$link5;
echo "<br />".return_domain($link6)." - ".$link6;
echo "<br />".return_domain($link7)." - ".$link7;
echo "<br />".return_domain($link8)." - ".$link8;
echo "<br />".return_domain($link9)." - ".$link9;
echo "<br />".return_domain($link10)." - ".$link10;
echo "<br />".return_domain($link11)." - ".$link11;
 
 
function return_domain($link) {
    $domain = preg_match('@^(?:https?://)?([^/]+)@i', $link, $matches);
    return $matches[1];
}
?>

Another easy method is to use the PHP function called parse_url which returns certain elements of a URI that you choose. For examples on this function, consult the PHP manual which has more than enough examples.