Site Migration from Server1 to Server2: WordPress and Drupal friendly

This post was borne out of our mass migration to Rackspace. Naturally, it gets into some advanced, hairy stuff. However, when fully dialed in, this method can take less than 5 minutes, even for large WordPress or Drupal sites. In order to complete these tasks, you will need privileged shell access to both servers (including mysql access), you will need to have rsync installed and configured, and you will need DNS access to the destination server (server2).
* Note: The following steps contain some powerful commands. Please be careful! And don’t execute any command if, after reading the details, you are still unsure what will happen.

1. Export your MySQL Database using mysqldump. This is the only step that must be done while logged into server1.
More Details

1-2. Edit db_backup.sql. This only needs to be done if you plan to create a fully-functional development version of your site. Open db_backup.sql in your editor of choice. Find & Replace all old URLs (http://old_site.com) to the new URLs (http://dev_site.server2.com). Rename the .sql file & save it.

*Remaining steps will be done from server2.

2. Create a DNS entry for the site’s new home.

3. Establish the document root for your site’s new home – i.e. create the new site directory.
[code]user@server2:~$ mkdir /path/to/new_site_dir[/code]

4. Use rsync to migrate your site directory. Our example will transfer files from old_site_dir on server1 to new_site_dir on server2.
[code]user@server2:/path/to/new_site_dir$ rsync -avz user@server1:/path/to/old_site_dir/ .[/code]
More Details

4-2. Check owner/group of new directory. Be sure it is consistent with working sites.

5. Create MySQL database, database user, database password, and privileges.
[code]mysql> create database db_name; 
mysql> create user db_user;
mysql> grant all on db_name.* to ‘db_user’@’localhost’ identified by ‘db_password’;[/code]
More Details

6. Upload the db_backup.sql. This will be imported into your newly created database (from previous step).
[code]
user@server2:~$ mysql -u mysql_user -p db_name < db_backup.sql [/code] More Details

7. Update your site’s database settings. For WordPress, update wp-config.php with your info from step 5. For Drupal, update settings.php with your info from step 5.
More Details

8. Add the VirtualHost record to your apache2 configuration file.
8-2. Restart Apache once the new VirtualHost record has been added.
More Details

How to export your MySQL database using mysqldump

Use your command line to backup your mysql database by exporting it to an .sql file. All you need to complete this task is shell access & a mysql account.

A major use for this command is to easily migrate your sites from one server to another. Since this is often part of a larger process, I like to decide ahead of time which directory will contain this file. So I’ll start by navigating to my desired directory and scoping it out:
[code]user@server:~$ cd /path/to/dir/; ls -al[/code]
Now that you’re sure about using this directory. Here is the basic mysqldump command:
[code]user@server:/path/to/dir$ mysqldump db_name > db_backup.sql[/code]
But, depending on your level of access, you may need to modify it:
[code]user@server:/path/to/dir$ mysqldump -u mysql_user -p db_name > db_backup.sql
Enter password:[/code]
After entering your mysql password, a file called db_backup.sql will be created in the default location, which is your current directory (three cheers for planning ahead!).
Of course, we could have just as easily added a location statement within the command:
[code]user@server:~$ mysqldump -u mysql_user -p db_name > /path/to/dir/db_backup.sql[/code]
But, you’re going to want to cd there anyways, just to verify that everything’s in order 🙂

Be sure to browse our related posts describing how to get migrate files from one server to another, and how to import an .sql file into an empty MySQL databse.

wget the latest WordPress installed – pronto!

It’s so flippin’ fast! Forget about FTP-ing…

wget it
[code]user@server:~$ wget http://wordpress.org/latest.tar.gz[/code]
This command will place the file in your current directory. You may plan on unpacking & installing in multiple directories, so you may want to cd to the parent directory of those locations. Plus, if you choose this method to get the next latest version, you can wget it in the same directory and override the previous version.

Copy it to your desired directory
[code]user@server:~$ cp latest.tar.gz /path/to/your/directory;[/code]

Go to that directory
[code]user@server:~$ cd /your/directory[/code]

Verify that it’s there
[code]user@server:~/your/directory$ ls
latest.tar.gz[/code]

Unpack the tar ball
[code]user@server:~/your/directory$ tar -xvf latest.tar.gz[/code]
*Note: -xvf combines 3 options. -x extracts the file from the archive. -v verbosely lists the files processed. -f uses the archive file.

Verify that the wordpress directory is now there…
[code]user@server:~/your/directory$ ls
wordpress[/code]

Presto! Now just set up the mysql database, edit wp-config.php, and go to http://yourdomain.com/wordpress/wp-admin/install.php to complete the final steps.

Create a MySQL Database, Username, Password, and Privileges from the command line

Step 1: Login to MySQL ( you will need an account )
[code]user@server:~$ mysql -u mysql_user -p
Enter password:[/code]

Step 2: Create the Database
[code]mysql > create database db_name;[/code]

Step 3: Verify that it’s there
[code]mysql > show databases;[/code]

Step 4: Create the User
[code]mysql > create user db_user;[/code]

Step 5: Grant privileges while assigning the password
[code]mysql > grant all on db_name.* to ‘db_user’@’localhost’ identified by ‘db_password’;[/code]
*Note: The localhost field usually doesn’t have to be edited, but you can set it to the specific address.

The above example grants all privileges, obviously. But you will likely want to limit privileges under many circumstances. These parameters include select, insert, and delete.
Choose all that apply and separate by comma:
[code]mysql > grant select, insert, delete, update on db_name.* to ‘db_user’@’localhost’ identified by ‘db_password’;[/code]