User login

Creating a new site from an old site

Note: I think NancyW has a post on Drupal.org about how to do this also, but I can't find it right now.

If it weren't for the promise of autopilot coming out and helping with all this, I would be trying to figure out how to grep settings.php to find the real $db_url = line (which follows, rather than being in, /* */ C-style comments).

Problem: how do we automatically rename all database table prefixes and the field prefixes within the sequences table?

This is based in large part on our old Backing up, restoring, making test databases with MySQL command line documentation. Another source drawn on was old documentation by Kieren Lal on Drupal.org Copying your live site to a test site (command line).

The *%&@^#$! percent in the password got me again! Still couldn't figure out how to escape that character or quote the password as a string, so used the mysql root user and password.

Resolution

Doing this from my user's home directory, since it's all DB stuff and making a temporary copy of the database file.

To find out what we're dealing with:

vi /var/www/drupal-5_2-live/sites/example.com/settings.php

And inside vi to find the string with the database names and password:

/db_url =

mysqldump -u root -p existing_live > existing_copy.sql

mysqladmin -u root -p create newsite_live

mysql -u root -p

Now you're in MySQL monitor:

mysql> GRANT ALL PRIVILEGES ON newsite_live.* TO newsite_live@localhost IDENTIFIED BY 'a.secret.password';
mysql> flush privileges;
mysql> \q

Now fill it up with the old site's content:

mysql -u newsite_live -p newsite_live < nthogen_copy.sql

Enter what you have for a.secret.password when prompted "Enter password". Congratulations, you did it!

OK, time to move the site files and such. We'll be putting it into our repository structure at the same time.

mkdir newsite
mkdir newsite/trunk
sudo cp -R /var/www/drupal-5_2-live/sites/existing.com/ newsite.com

Next time- use an svn export command, not a copy. Instead I went through quickly and deleted the .svn folders, but a theme with lots of directories would get tedious quickly.

Also, I renamed the theme folder to reflect its new home, as it will be customized to branch off from the original.

cd newsite/trunk/newsite.com/themes/
sudo mv oldthemename newthemename
sudo vi template.php

And in template.php, rename all theme_ functions:

:%s/olthemename_/newthemename_/g

You can shift-ZZ to save and get out of vi.

Now replace the settings.php with Agaric's modified one:

cd ~
sudo rm newsite/trunk/newsite.com/settings.php
cp /var/www/drupal-5_2-test/example.com/settings.php newsite/trunk/newsite.com/
rm -rf newsite/trunk/newsite.com/files

And the payoff! Importing this thing in!

sudo svn import -m "Site created by copying existing site nthogen." newsite file:///srv/svn/agaric/agaric-sites/5/newsite/

Now that it's in the repository, you can delete it from your user folder. Just make sure you are in your user folder.

cd ~
sudo rm -rf lindsay

sudo svn checkout file:///srv/svn/agaric/agaric-sites/5/newsite/trunk/newsite.com/ /var/www/drupal-5_2-test/sites/newsite.com

cd /var/www/drupal-5_2-test/sites/
sudo cp /var/www/drupal-5_2-test/sites/example.com/db.php newsite.com/
mkdir newsite.com/files
# since settings.php and db.php won't be edited by drupal
# should they be set to some other ownership for extra security?
sudo chown www-data:www-data newsite.com/settings.php
sudo chown www-data:www-data newsite.com/db.php
sudo chown www-data:www-data newsite.com/files

svn propset svn:ignore $'db.php\nfiles' newsite.com
svn commit -m "Final commit for making a copy of a site." newsite.com

Note: You can use sudo svn propedit svn:ignore newsite.com to check out the results of your propset command, or to enter it in a text editor interface rather than on a single line.

Now put in your database information for the user and database you created earlier:

sudo vi reedlindsay.com/db.php

TO DO: turn all of this into a script.

You'll have

BONUS! You know how annoying it is that when you change a theme like we did in this example, you lose all your blocks settings? Here's the mysql that fixes that:

UPDATE blocks SET theme = 'newtheme' WHERE CONVERT( blocks.theme USING utf8 ) = 'oldtheme'

TO DO: create a copy blocks settings module, to allow for blocks to follow a renaming of a theme or to be copied to a similar theme (there is a reason blocks are theme-specific, so this will really only work when you're forking a theme, say).

Note that once you copy a site and then go inside and delete the guts of it, that's a great time to make an installation profile from it.

Searched words: 
mysql copy database command line drupal create new site from old site

Comments

WARNING! Be sure to change

WARNING!

Be sure to change the files directory to the new site also-- otherwise it will point to the old site and you could DESTROY EVERYTHING.

WARNING!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.