Updating WordPress? Backup first!

I’m sure I’m not the only one who has been a little slack on occasion when updating their version of WordPress and not doing a backup. It sounds such a stupid thing to do, especially when you think about the time you’ve spent working on the site and the time you may well need to spend fixing it!

One of the things that I hated was doing an ftp download of the site. I never got on with backup utilities very well as I wanted to be in control. So, a few years back I realised there was a far simpler method to maintain backups using the Terminal in Mac OSX.

ssh username@yourdomain.com 
cd httpdocs // or whatever your WP root directory is
mysqldump -u username -p database > backupdatabse.sql
[Enter password]
tar -vcf backupfilename.tar .

And that’s pretty much it. It creates two files in your WordPress root. You can secure copy (scp) through the terminal if you want or download using ftp. Either way, the above takes very little time to do and gives you the opportunity to restore things to how they were if it does go horribly wrong.

Create MySQL Backup

There are a number of ways to backup a MySQL database. For my local environment I like a quick way to take a backup before trying something new. Rather than go through the cumbersome process of logging into Terminal, navigating to the MAMP /bin directory and then entering a very long command, which I would easily forget, it’s simpler to create a shell script and then invoke it as an Application by using AppleScript.

First, create your shell script by opening a text editor, eg TextWrangler (and not an RTF editor), and type:

#!/bin/bash

On the next line type in the commands you would normally do in Terminal. In my case, I wanted to navigate to the MAMP /bin where mysqldump lives. Then, I wanted to use this to dump a sql file of a specific database within my sites directory.

cd /Applications/MAMP/Library/bin/
./mysqldump -a -uroot -pPASSWORD mydb1 > ../../../../Users/[username]/Sites/Backups/db1.sql;

Finally, close it with

exit

You then save this in a logical place (ie somewhere you can find it!), in my case /Users/[username]/Sites/Backups/scripts/. Next go to Application > Utilities and launch AppeScript Editor. Type the following into the edit pane.

do shell script "/Users/[username]/Sites/Backups/scripts/backup_nsdaukcom_263323_db1.sh"

Save this to either Applications or a sub-directory within Applications. When you change the File Format to Applications before saving.

Once done, all you need to do to take a backup is double-click the new application you’ve created and your previous backup is overwritten with a new one.

Remove System Mail

The other day I was in Terminal when I noticed for the first time it said You’ve got mail. at the top. Knowing I didn’t have any new mail I typed mail to see a whole load of system mail from previous web development work.  I’m no command line expert so I checked on macrumors.com and quickly found out what to do. Here’s the original post.

Go to the directory (=folder) where the system level email files for all users are stored, and list them:

cd /private/var/mail/; ls -l

Display the email(s) for the user which you fill into <username>. Either use your username (without the angle brackets) or use the character * (asterisk) if you want all emails from users listed:

cat <username> | less

Finally erase your mail file, in case you do not need it anymore.

sudo rm /private/var/mail/<username>

Obviously, seeing as you’re deleting mail from your mac make sure you’re certain before doing so. Also, the sudo rm command has no safety net. Once something’s gone it’s gone so if you’re unsure, don’t do it.

Resetting Root Password in MAMP

I struggled for a while to do this as I expected MAMP to automatically update the root password after I’d changed it in the mysql.user table. To be clear, it does not. And so, here’s a brief guide to sorting out you MAMP woes.

First, connect to mysql using Terminal. MAMP is automatically set to use root as the user account to connect to mysql. Funnily enough, root is also the password!

/Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot
You’ll be logged into mysql and will see the following.
Last login: Wed Dec 28 14:48:04 on ttys001
ZoooJs-MacBook-Pro:~ zoooj$ /Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 38
Server version: 5.1.44 Source distribution

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
 The next thing to do is reset your password. Type the following replacing your-new-password with your new password. You should see the Query OK message.
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('your-new-password');
Query OK, 0 rows affected (0.00 sec)
Once that’s done you need to edit a number of files in the /Applications/MAMP/ directory. I still can’t quite believe that you have to do this in so many places. You’d think they would have a single file that has the master password set. Hey ho, maybe not.
So, find and edit these files using a plain text editor, not TextEdit, Word, OpenOffice, etc. Use TextWrangler or another decent Mac IDE. TextWrangler is free. You’ll need to find the line that has -proot and change it to read -pyour-new-password. Note that there is no space between -p (stands for password) and the password itself.

/Applications/MAMP/bin/phpMyAdmin/config.inc.php
/Applications/MAMP/bin/checkMysql.sh
/Applications/MAMP/bin/quickCheckMysqlUpgrade.sh
/Applications/MAMP/bin/repairMysql.sh
/Applications/MAMP/bin/stopMysql.sh
/Applications/MAMP/bin/upgradeMysql.sh
/Applications/MAMP/bin/MAMP/index.php

Restart MAMP and all should be well.

Transferring files between host servers

If you’ve ever moved websites between hosts or wanted to set up a development version of a client site then you’ll have copied files from one and made them available to the other. The basic method for doing this is to ftp into the source server, download the files to your computer, ftp to the new server and upload the files there. The problem with this is that it takes ages to do when you’ve got large websites.

Despite having been doing web development for a number of years I’ve never thought about this before as I would always follow this process. However, given that I run a number of Magento sites for clients now and that there are a lot of pressure on my time I need a quick way to do this. So, after about 20 minutes research I realised there is a really easy way.

First on the source system compress the file into a .zip or .tar.gz file. Most hosts provide tools to do this. I know CPanel and DirectAdmin do. Next, download this. As long as you’ve a fast broadband connection this won’t take long. Next upload to the new host server. This took about 45 minutes for me. However, I set it up and just left it running. Last, after checking I have shell access with my user on the new host server, I use Putty to do a SSH session and then just run the following command: tar -zxvf yourfile.tar.gz. This unzips everything into the folder you leave the file.

All done in under an hour with about 10 minutes of my actual time.