October 12, 2007

How to Backup and Restore Large Databases in MySQL

Filed under: MySQL — Matt @ 6:15 am

Most who use MySQL have a nifty program called PhpMyAdmin. There is a good chance that if you are a user of that program, you’re here because you tried to backup or restore a large database through the web based interface and failed. This is a common situation, lucky for us there’s a very easy, fast, and reliable way to do this through the command line! If you’re a Linux user who has SSH access, follow the steps below.

Backing up the Database
Fire up SSH and type the following. Replace USER with your MySQL username, DBNAME with the name of the database you want to back up, and /path/to/backupname.sql to where you want the database backup file to be saved (directory must exist).

mysqldump -a -u USER -p DBNAME > /path/to/backupname.sql

You’ll be prompted for your MySQL password. It’ll get to work after you type that in and press enter, it may take a few moments (or longer if your database is really large).

When you’re returned to the command line you now have a backup file where you specified above. This file is a complete copy of the database at the time you ran the command, the second your MySQL table is updated your backup will be out of date. I suggest you take backups at regular intervals depending on your situation (I will write a guide on how to automatically do backups one day).

Restoring Your Database Backup
Follow these 3 commands to restore your database easily.

First we need to drop your current database so there are no conflicts when we’re restoring

mysqladmin -u USER -p drop DBNAME

Now we’ll remake the database, because it doesn’t exist anymore

mysqladmin -u USER -p create DBNAME

And finally, we’ll populate the fresh new database with your backup file.

mysql -u USER -p DBNAME < /path/to/backup.sql

The variables you should change above are pretty straight forward. If you get errors importing your data, it’s most commonly because your max_allowed_packet isn’t set high enough in /etc/my.cnf - if you have root access, you can go in and make that variable higher. If you don’t have root access, seek support from your web host.

How to Tar / Untar From the Command Line

Filed under: Linux — Matt @ 5:50 am

A simple guide for those who are as helpless on the Linux command line as I am. When managing websites, you’ll often need to compact, move and extract a large number of files, here’s how to do it on your Linux box.

Initial Steps
First SSH into your web server and move to the directory of the files you wish to tar up, since tarring an absolute path will save the folder structure as well which is bad (ie: /home/yoursite/public_html/backupthis/ will save the folders home -> yoursite -> public_html -> backupthis). You can get to the folder you need to by typing:

cd /path/to/your/stuff

Creating a Tar
Now that we’re at the directory we want to tar, or the sub-directory, you can do one of two commands depending on what you need.

If you want to save every file / folder in your current location into a file called backup.tar.

tar -cvf backup.tar *

If you want to save a tar named backup.tar with the folder “somefolder” and its contents.

tar -cvf backup.tar somefolder/

You can test/view your tars with the following command:

tar -tvf backup.tar

Extracting the Tar
When you need to extract that tar, the following command will be suffice:

tar -xvf backup.tar

How to Protect Your PHP Code With Encryption

Filed under: PHP — Matt @ 4:42 am

This was an article I wrote for Web Hosting Talk back in April 2004. I’ve made a few revisions to it and felt it would be worthwhile to share here as well.

This guide is intended for programmers who frequently take on freelance projects, those contracted over the internet by strangers to make a PHP script. Due to the nature of the web, it’s very easy to get scammed in this exchange, and there usually isn’t much you can do about it if it happens. Luckily for you, you’re 1 step ahead of the scammers by reading this guide before you sent them your script (hopefully).

Scenario: You are paid $1,500 to write a PHP script for Joe Montana. Joe pays you, you send him the script, all is well, until Joe reverses the charges, leaves you out in the cold while you just wasted several weeks and he gets a free script. Now you have to stress yourself with tracking him down to press charges, which most of the time never happens. Debating intangibles over the internet is a sticky situation for the seller, aka service provider, aka you. I just read the same story on another forum, which is all too common, and this prompted me to share this with hopes of protecting your time and profit.

Step 1
Open the file that is critical to the script’s operation and won’t require modifications by the client at any point in time. You’ll want to pick a file that if you take it out of the picture, it will cripple the operation of his script.

Step 2
Paste this snippet at the top of the file, before the script’s operations are run.

$lines = file('http://www.example.com/joemontana.txt');
foreach ($lines as $line_num => $line) {
$license = htmlspecialchars($line);
if ($license == "invalid") {
exit("License Invalid - Please contact THECOMPANY");
}
}

Now do the following:
- Create a .txt file, call it something unique, preferably the name of the project/client.
- Upload the .txt file somewhere accessible from the web, preferably on your server where only you can write to it.
- On Line 1 of the code above: Change example.com/joemontana.txt to your website and path to the text file you uploaded.
- On Line 5 of the code above: Change YOURCOMPANY to its respective variable. You can edit this message to say whatever you want.

Now if at any time you have a reason to disable his script, all you need to do is open that text file, and type the word: invalid

After you save that file, his script will not function. This method is safe for your client; if your server is down, the file doesn’t exist, or anything other than the word “invalid” is in the document, it will function properly.

Step 3
Now you’re thinking, “That’s good Matt, but what if the client isn’t an idiot and he goes in and deletes my protection”. This is where encoding comes into play. Pick your favorite one, if you don’t have one in mind, I suggest IonCube. Encrypting a file with them through their Online Encoder costs 50 cents, which is peanuts when you consider the sheer satisfaction you’ll receive if a client does scam you and you’re able to pull the rug from under them.

After your file is encrypted, open it up in a text editor, you should see a bunch of nonsensical data in there. Pack it up and ship it to your client! It’s important to remember that you’re not safe from being scammed, always exercise caution when doing business over the Internet. You’ll at least have a lot more control over the situation using these methods.

I suggest that you inform your client that you will be encrypting the file. Include instructions and offer to install the IonCube loaders on their server. Make sure to tell them to upload the encrypted file in BINARY mode, as it will not work otherwise. You should send them the unencrypted file after a couple months from their payment (or whenever you feel it has fully cleared and you’re safe) for their convenience, people don’t like being under the gun forever.

These instructions are provided without warranty. Any damage or loss, yadda yadda yadda *insert long disclaimer here*, is your own fault.

How to Display Server Load in PHP

Filed under: PHP — Matt @ 3:35 am

As the inaugural post on my new blog, I figured I’d start with something I put to good use today. When developing a PHP application or monitoring a web server, it’s important to know what your server load is to properly identify that there is a problem. This can be achieved through most control panel software (like WHM) or by typing in “uptime” in your SSH command prompt.

For those interested, there is a very easy way to output your load in PHP using the exec function. See the code below.

$load = exec(”uptime”);
$load = split(”load average:”, $load);
$load = split(”, “, $load[1]);
$load = $load[0];
echo “Current Load: $load”;

Place that snippet wherever you want in your PHP application to output the current server load. If you want to display something like “03:55:48 up 49 days, 13:36, 0 users, load average: 0.04, 0.12, 0.10″ instead of just “0.04″, you can delete lines 2-4 of the code.

Of course, since “uptime” is a Unix command this won’t work on Windows servers, and from what I understand, there’s no easy solution for those users.

« Previous Page