1

I'm building a site that will be used as a template site for new users. Once someone signs up, they should receive a copy of the original master template site, including their own SQL database.

Ideally, the new sub-site would need to receive updates from the master site somehow.

Is there a way to copy and entire site when someone registers? Can this be accomplished with Multi-sites?

1 Answer 1

0

Yes, using hook_user_insert() to start the process off. From a custom module that implements hook_user_insert() you can run any code you want. However to pull it off you're going to have to write quite a bit of custom code.

Your hook_user_insert() function would need to do quite a lot of steps:

  1. Fetch the database connection information defined in settings.php
  2. Run mysqldump to make a .SQL dump of the database
  3. Run a tar command to make a .tar.gz file of the Drupal codebase (PHP files) and files directory.
  4. Zip or tar or something the sql file, the .tar.gz file, and probably some kind of instructions
  5. Present the file for download. This is made trickier because you don't want to have the file inside your codebase.. or each time you clone the database you'll pick up all the .zip files from previous users in the next user's .zip file. You either need to serve the file directly, exclude the zip files from future codebase archives, or serve the zip files out of a directory that won't get zipped into the zip file.
  6. (optional) Clear the user out of the master site so that it doesn't exist for future signups.

You ask about Multi-Sites, that's only relevant if you plan to immediately start hosting the cloned site yourself. If your plan is to have the user download their files and .SQL database, you don't need to worry about that. (The user will have to worry about loading the SQL dump and setting up a database username and password though, which you'll need to babysit them through or it will never end up working out).

If your plan is to clone the site on your server, then you could do it with a multi site. Your steps would then be:

  1. Fetch the database connection information defined in settings.php
  2. Run mysqldump to make a .SQL dump of the database
  3. Create a new MySQL database to hold the new cloned site 4. There are security implications here.. if your webserver is allowed to create mysql databases, is it really secure from hackers?
  4. Create a new sites/new.example.com folder for the new site (if it were named new.example.com)
  5. Create a new settings.php file for the new site's database in sites/new.example.com
  6. Load the SQL dump from step 2 into the new database (using the command line mysql command)
  7. Redirect user to the new site. You will need to use Wildcard DNS (ie. *.example.com woudl match site1.example.com, site2.example.com, etc. and you wouldn't need to add DNS entries for each new site).

While this plan is not impossible, it will be quite hard to get working well. Your biggest challenge will be helping the users get through the complicated task of creating a new database, setting up username and password for it, loading the SQL dump, configuring their webserver etc etc etc.

3
  • Thanks for the information. Ideally, I'd like it to be as automated for the user as possible, so once they register, it would create a whole new setup as a sub-site, then redirect them to it. They'd then use their login info to access it from then on. Does that sound possible?
    – Timmah
    Commented Jan 26, 2015 at 4:28
  • One major security risk here: with these instructions you would be exposing your admin and database password/username. If anyone is doing this please at least sanitize your settings.php and your database to remove or replace any passwords. Commented Jan 26, 2015 at 5:43
  • Exposing the credentials in settings.php is only a major security risk if you allow external access to your database somehow (a bad idea). For extra points you could sanitize settings.php in the zip archive, setting the database credentials to random values and providing instructions in the zip on how to create the database. Commented Jan 26, 2015 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.