Moving sites from one host to another

=Intro=

Please note that any path I use here is based on the Drupal Content Management System. If you're using any other PHP project, just change the paths accordingly. The rest of the steps should still apply.

Sometimes you need to get an update of an online project and put it locally so that you can work on it and vice versa. The easiest way is to compress everything and send it over.

Steps:


 * Get a copy of the SQL database
 * Compress everything and send it over
 * Extract and update SQL database.

First of all, SSH into your host, and change directory into the htdocs/ or public_html/ folder.

To quickly find the information you need for the SQL database, get it from the settings.php file on Drupal's CMS, or any other project's settings file.

grep mysqli sites/default/settings.php

and the output should look like $db_url = 'mysqli://username:password@host/database'; or as an example $db_url = 'mysqli://john:123pass@76.12.73.20/mydb';

=Get a copy of the sql database=

mysqldump -h 76.12.73.20 -u john -p mydb > 20080301-myproject.sql

Notice that i used the YYYYMMDD- date format. It helps to organise.

=Compress everything and send it over=

cd out of your htdocs and tar cjf 20080301-myproject.tbz htdocs to compress..

WARNING: For Drupal users, If you're using a module like backup_migrate, and it creates some 1000 backups that take a lot of space, you can exclude them from the archive. To do so, run the following from outside the htdocs/ folder instead of the above line:

ls -R htdocs/bk > tar-exclude tar cjf 20080301-myproject.tbz htdocs -X tar-exclude

What it does is list all the files that are in the bk backup folder that you don't want to include, and saves the list into a file called tar-exclude. Afterwards, you tar normally, but giving the -X parameter to exclude what's in the file you created.

Then download everything, and when finished downloading, don't forget to delete the .sql file from inside htdocs/. It's dangerous to keep it there.

Note: Just to remember, when using tar, c = create archive x = extract archive

j = use BZIP2 compression z = use GZIP compression

f = force

v = verbose - meening show lots of output, otherwise it'll only show errors.

Also, you can name the extension whatever you want.. if .tar.gz or .tar.bz2 are too long, use .tgz and .tbz instead.

So, to create / extract with bzip2, use tar cjf foo.tbz folder/ tar xjf foo.tbz

To create / extract with gzip, use tar czf foo.tgz folder/ tar xzf foo.tgz

If you're confused to which one to use, always use bzip2. It has better compression, and anyway, you can open all formats on windows using WinRAR

=Extract and update sql database=

So now all you have to do is extract the archive, and overwrite your local copy. If you're on MAC OSX, be sure to use cp -R when overwriting, not mv or drag and drop, otherwise you will delete any local changes and .svn folders.

cp -R htdocs/* /var/www/myproject/

or wherever the destination is.

Update your local mysql copy too mysql -u admin -p fartpass < tar cjf 20080301-myproject.sql

And change the mysqli line in sites/default/settings.php to use your local database if you're on Drupal, otherwise find your project's config file, and update its uername/password settings.

Finally, give write permission to your files/ and tmp/ directories if need be. chmod -R 777 files tmp

That's it!

=If using SVN=

If you're using SVN repositories, and you copied the online project to your local repository in order to do your changes, when done, don't forget that you SHOULDN'T UPLOAD THE LOCAL REPOSITORY YOU USE... If you upload your local copy, then you're also uploading the .svn/ folders in it, which is very bad and a security breach. Instead, SVN EXPORT a copy and upload that.. Don't forget to upload the new database if it has any critical changes too. And make sure nothing big has happened in the online site that would be lost.